PHPMailer is a fully featured email transfer class for PHP and its popularity has grown rapidly over the years. Recently I had to send e-mail messages using the GMail SMTP server for one of my clients.
Up to now I had only sent email messages using SMTP servers provided by the hosting company. The difference here is that I had to use SSL encryption to send emails. Using hosting company SMTP server has its drawbacks as there is a chance that your website IP address is already on a blacklist simply because someone else had abused the service before you.
Requirements
- Latest version of PHPMailer
- A web host having PHP 5 / 6 enabled with Open SSL
- GMail or Google Apps account
Sending Email
- Check with your web hosting provider if port 465 (TCP out) is open, if not check with your hosting provider. Port 465 is needed to connect with the GMail SMT server.
- Include PHPMailer class in your mailing script as follows
require_once('phpmailer/class.phpmailer.php');
- Add the following code to your mailing script
$mail = new PHPMailer();
$mail->IsSMTP(); // telling the class to use SMTP
$mail->Host = “mail.googlemail.com”; // GMAIL SMTP server
$mail->Port = 465; // set the SMTP port for the GMAIL server
$mail->SMTPDebug = 0; // debugging: 0 = none, 1 = errors and messages,
// 2 = messages only
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->SMTPSecure = 'ssl'; // secure transfer enabled and REQUIRED for GMail
$mail->Username = ‘[email protected]’; // SMTP account username
$mail->Password = ‘yourpassword’; // SMTP account password
$mail->AddAddress('[email protected]'); // receiver mail address
$mail->SetFrom(‘your name’, ‘[email protected]’); // from address
$mail->Subject = ‘Test mail’; // mail subject
$mail->MsgHTML(“Message Body”); // HTML message body
if(!$mail->Send())
{
$msg = "Mail could not be sent.
Error Description : " . $mail->ErrorInfo; } else
{
$msg = 'Email Message sent successfully!';
}
echo $msg;
As a final note please do check the GMail SMTP limitations mentioned here.
Comments
No Comments Exist
Be the first, drop a comment!