One more example related to PHPMailer, in this tutorial I’ll give an example on How to send email through Gmail SMTP Authentication.
Sending email function with PHP may not work in almost cases such as on your localhost, hosting without SMTP Server available. Therefore go with an external SMTP server like Gmail to relay your emails is not a bad solution.
All you need is a Gmail account and I’ll do the rest
PHP Send Email Through Gmail SMTP Authentication
<?php require_once('class.phpmailer.php'); $mail = new PHPMailer(); $body = file_get_contents('contents.html'); $body = eregi_replace("[\]",'',$body); $mail->IsSMTP(); // telling the class to use SMTP $mail->SMTPAuth = true; // enable SMTP authentication $mail->SMTPSecure = "ssl"; // sets the prefix to the servier $mail->Host = "smtp.gmail.com"; // sets GMAIL as the SMTP server $mail->Port = 465; // set the SMTP port for the GMAIL server $mail->Username = "your-email@gmail.com"; // GMAIL username $mail->Password = "your-gmail-password"; // GMAIL password $mail->SetFrom('from-email@domain.com', 'First Last'); $mail->Subject = "Test PHPMailer via Gmail smtp"; $mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test $mail->MsgHTML($body); $address = "to-email@domain.com"; $mail->AddAddress($address, "To Name"); if(!$mail->Send()) { echo "Mailer Error: " . $mail->ErrorInfo; } else { echo "Message sent!"; } ?> |
Let change your Gmail username and password on line 15, 16.
Download the source code above.
