How to send mail with smtp authentication in php?

Member

by jerad , in category: Third Party Scripts , 16 days ago

How to send mail with smtp authentication in php?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by denis , 15 days ago

@jerad 

To send mail with SMTP authentication in PHP, you can use the PHPMailer library which provides a simple and easy-to-use way to send emails via SMTP with authentication. Here's a step-by-step guide on how to send mail with SMTP authentication in PHP using PHPMailer:

  1. Download and include the PHPMailer library in your PHP script. You can download it from the official GitHub repository: https://github.com/PHPMailer/PHPMailer
  2. Create a new PHP file and include the PHPMailerAutoload.php file at the beginning of the script:
1
require 'PHPMailerAutoload.php';


  1. Initialize the PHPMailer object and set the necessary properties for sending the mail:
1
2
3
4
5
6
7
8
9
$mail = new PHPMailer;

$mail->isSMTP();
$mail->Host = 'smtp.example.com';
$mail->SMTPAuth = true;
$mail->Username = '[email protected]';
$mail->Password = 'your_email_password';
$mail->SMTPSecure = 'ssl';
$mail->Port = 465;


  1. Set the sender and recipient email addresses, subject, and body of the email:
1
2
3
4
$mail->setFrom('[email protected]', 'Your Name');
$mail->addAddress('[email protected]');
$mail->Subject = 'Test email with PHPMailer';
$mail->Body = 'This is a test email sent with PHPMailer using SMTP authentication.';


  1. Finally, send the email using the send() method:
1
2
3
4
5
6
if (!$mail->send()) {
    echo 'Message could not be sent.';
    echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
    echo 'Message has been sent';
}


That's it! You have successfully sent an email using SMTP authentication in PHP with the help of the PHPMailer library. Just make sure to replace the SMTP server, email addresses, and other credentials with your own.