How to send email with smtp in php?

by elise_daugherty , in category: Third Party Scripts , a month ago

How to send email with smtp in php?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by jasen , a month ago

@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:

  1. 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';


  1. 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;


  1. 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';


  1. 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.