PHP mail using GMail SMTP and PHPMailer

|
I have been a fan of PHPMailer for quite some time. It is a very easy to use class that offers much more power than the regular PHP mail function. Although one problem I have had PHPMailer have been its rather poor documentation. Methods and attributes are split up with examples and tutorials seemingly overlapping but disconnected as well. However this is a small fault for such a great contribution to the PHP development community.

A thing I found hard to find was a specific example of how to set up PHPMailer to work with a GMail SMTP server. The results are actually quite simple and I managed to get it working with only a few tries. However if you are reading this you probably do not want to waste that 10 minutes, so I'll just show you the sample code below.

$pm = new PHPMailer();
$pm->IsSMTP(true);
$pm->Host = 'ssl://smtp.gmail.com:465';
$pm->SMTPAuth = true;
$pm->Username = 'yourusername@gmail.com';
$pm->Password = 'yourpassword';

The above code shows all the configuration settings that need to be set. I will not reproduce the normal message attribute settings needed for sending mail in PHP with PHPMailer as you can find them at the PHPMailer website.

4 comments:

VisualiseIT said...

I run a large number of websites and wanted to have the PHPMailer available on my 'global webserver'. I've read that you can 'require' a script via a url. However, when I do this with PHPMailer it tells me the class doesnt exist?

Any ideas?

Jinu said...

Hii..While executing the code,i get message as Mail has been sent but I am not able to receive any mail send through php mailer.

Marcus Uy said...

I didn't like PHPMailer because it just seems so messy -- if you know what I mean. I also didn't find it reliable for some reason. I ended up rolling my own SMTP class. Check it out:

http://www.virtualthinking.com/loadhtml.php?where=scripts&what=art_show.php&db_target=00000000023

Shital Thakker said...

Yeah… Now Gmail doesn’t allow you anymore to do tricks like this with their email service. They realized that a lot of spammers do this and they introduced extra security measures with the HELO relays. Now they allow only FQDNs.

Post a Comment