This tutorial describes how to use PHPMailer and use of the PHPMailer class functions. Many PHP websites and web applications need to send transactional emails. PHP does have a mail() function you could use to send an email from the web server itself.
For those who have not used PHP’s mail() function before, this section will provide information about email and e-mailing in general, in addition to explaining how to get started with PHPMailer.
1 2 3 4 5 6 | <?php #include the PHPMailer object require("class.phpmailer.php"); $mail = new PHPMailer(); ?> |
Save it in the same directory where you’ve saved class.phpmailer.php. If you switched error messages and warnings off, then go ahead and set it on.
1 2 3 4 5 6 7 8 9 10 11 12 13 | <?PHP $mail->IsSMTP(); // telling the class to use SMTP $mail->SMTPAuth = true; // enable SMTP authentication $mail->SMTPSecure = "tsl"; // sets the prefix to the servier $mail->Host = "ssl://smtp.gmail.com"; // sets GMAIL as the SMTP server $mail->Port = 465; // set the SMTP port for the GMAIL server $mail->SMTPAuth = true; $mail->Password = "Youremail Password"; // GMAIL password ?> |
Step 3. Set the email properties where you want to send email
1 2 3 4 5 6 7 8 9 10 | <?php $mail->Subject = $subject; $mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test $mail->MsgHTML($body); $mail->SMTPDebug = false; $mail->AddAddress($address, "User"); ?> |
Once you set up the way you want it, you call the object’s Send method. If the Send method returns true, then email send successfully to configures email address. If it returns false, then there was a problem in your phpmailer configuration.
1 2 3 4 5 6 7 8 | <?PHP if(!$mail->Send()) { echo "Message not sent!"; echo "Mailer Error: " . $mail->ErrorInfo; } else { echo "Message sent!"; } ?> |
If you like FreeWebMentor and you would like to contribute, you can write an article and mail your article to [email protected] Your article will appear on the FreeWebMentor main page and help other developers.
Article Tags: download phpmailer, HTML 5, PHP, phpmailer, smtp