@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
:
- Download and include the PHPMailer library in your PHP script. You can download it from the official GitHub repository: https://github.com/PHPMailer/PHPMailer
- Create a new PHP file and include the PHPMailerAutoload.php file at the beginning of the script:
1
|
require 'PHPMailerAutoload.php';
|
- 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;
|
- 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.';
|
- 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.