@elise_daugherty
To send an email using SMTP in PHP, you can use the PHPMailer
library. Here is an example of how you can send an email using SMTP in PHP:
- First, you need to download the PHPMailer library from https://github.com/PHPMailer/PHPMailer and include it in your PHP script:
1
|
require 'path/to/PHPMailer/PHPMailerAutoload.php';
|
- Next, create a new PHPMailer object and set the necessary SMTP settings:
1
2
3
4
5
6
7
8
|
$mail = new PHPMailer;
$mail->isSMTP();
$mail->Host = 'smtp.example.com';
$mail->SMTPAuth = true;
$mail->Username = '[email protected]';
$mail->Password = 'yourpassword';
$mail->SMTPSecure = 'ssl';
$mail->Port = 465;
|
- Set the email parameters such as the sender, recipient, subject, and message:
1
2
3
4
|
$mail->setFrom('[email protected]', 'Your Name');
$mail->addAddress('[email protected]', 'Recipient Name');
$mail->Subject = 'Subject of the email';
$mail->Body = 'This is the body of the email';
|
- Finally, send the email:
1
2
3
4
5
|
if($mail->send()) {
echo 'Email has been sent';
} else {
echo 'Email could not be sent. Error: ' . $mail->ErrorInfo;
}
|
That's it! You have successfully sent an email using SMTP in PHP using the PHPMailer
library.