Send emails using PHPMailer and GMail
November 4th, 200925 Comments
Sending emails using PHPMailer v5.0.0 and GMail
Requirements:
1 : PHPMailer v5.0.0 and above
2 : GMail account
Download the PHPMailer v5.0.0 and above from http://sourceforge.net/project/showfiles.php?group_id=26031&package_id=252700
Now create a file you can call mail.php on your system and open it with your favorite text editor.
Paste below code into it and save.
<?php
date_default_timezone_set(‘Asia/Calcutta’); // to set default timezone
require_once(‘../class.phpmailer.php’);
// optional, gets called from within class.phpmailer.php if not already loaded
//include(“class.smtp.php”);
$mail = new PHPMailer();
$mail->IsSMTP(); // telling the class to use SMTP
$mail->Host = "mail.yourdomain.com"; // SMTP server
// enables SMTP debug information (for testing) // 1 = errors and messages // 2 = messages only
$mail->SMTPDebug = 2;
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->SMTPSecure = "tls"; // sets the prefix to the servier
$mail->Host = "smtp.gmail.com"; // sets GMAIL as the SMTP server
$mail->Port = 587; // set the SMTP port for the GMAIL server
$mail->Username = "yourusername@gmail.com"; // GMAIL username
$mail->Password = "yourpassword"; // GMAIL password
$mail->SetFrom(‘name@yourdomain.com’, ‘First Last’);
$mail->AddReplyTo("name@yourdomain.com","First Last");
$mail->Subject = "PHPMailer Test Subject via smtp (Gmail), basic";
// optional, comment out and test
$mail->IsHTML(true); // send as HTML
$mail->Subject = "This is the subject";
$mail->Body = "Hi,This is the HTML BODY "; //HTML Body
$mail->AltBody = "To view the message, please use an HTML compatible email viewer!";
$address = "towhom@towhomdomain.com";
$mail->AddAddress($address, "First Last");
$mail->AddAttachment("images/phpmailer.gif"); // attachment
$mail->AddAttachment("images/phpmailer_mini.gif"); // attachment
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
}else {
echo "Message sent!";
}
?>
For any ssl exceptions/ errors:
change fgets() to @fgets() to surpass the ssl error
———————–
At class.smtp.php
before line 104 (before #connect), use this lines:
$host = “ssl://smtp.gmail.com”;
$port = 465;
———————–
In the PHP.ini, you need to have this option:
extension=php_openssl.dl
Additional Resources:
http://phpmailer.worxware.com/index.php?pg=examples
http://www.askapache.com/php/phpfreaks-eric-rosebrocks-phpmailer-tutorial.html
Windows environments, this seems to work:
544-configuring-php-under-windows-use-gmail-external-smtp-server-ssl
Wednesday, November 4th, 2009 at 1:08 am
Hi, I’m searching an example of phpmailer with gmail and this it is very good, thank you. But I have an error. Can you help me please?I am so lost :S.
The error is:
Warning: fsockopen() [function.fsockopen]: unable to connect to smtp.gmail.com:465 in /home/vr000479/public_html/mail/phpmailer/class.smtp.php on line 105
Message Sent
Error: SMTP Error: Could not connect to SMTP host.
Wednesday, November 4th, 2009 at 1:08 am
require(“phpmailer/class.phpmailer.php”);
$mail->IsSMTP();
$mail->Host = ‘ssl://smtp.gmail.com:465′;
$mail->SMTPAuth = TRUE;
$mail->Username = ”; // Change this to your gmail adress
$mail->Password = ”; // Change this to your gmail password
$mail->From = “”;
$mail->FromName = “”;
$body=”html content”;
$text_body=”alternate text content”;
$mail->Body = $body;
$mail->AltBody = $text_body;
$mail->AddAddress(“recipient email”, ‘recipient name’);
if(!$mail->Send())
echo “There has been a mail error sending to ” . $row[3] . // Clear all addresses and attachments for next loop
$mail->ClearAddresses();
?>
Wednesday, November 4th, 2009 at 1:08 am
This is an incredible hack! Thanks a lot mate, I spent hours trying to send mail using the PHP mail() function and a mail server. This solution is both simpler and more robust, and was a godsend as I’d almost given up on the idea completely.
Wednesday, November 4th, 2009 at 1:08 am
yup..thanks a ton..its helped me out of a week-long struggle..
Wednesday, November 4th, 2009 at 1:08 am
Thank you very much for this code!! I was trying to do that by myself without results. Sending mails from other servers are very easy, with GMAIL too, but for now
. Best wishes!
Wednesday, November 4th, 2009 at 1:08 am
How do I incorporate this with phpmailer-FE as an html form.
Wednesday, November 4th, 2009 at 1:08 am
Just create form, it’s an example (all code):
submit here
You looking for that solution? Please tell me If I’m right
.
Best wishes
Wednesday, November 4th, 2009 at 1:08 am
Sorry, but I don’t know how to add some code scripts here, enter on my website and write to me or tell me how to put that stuff here.
Wednesday, November 4th, 2009 at 1:08 am
Hi rob!! You can see below link, It might be useful for you.
http://phpmailer.codeworxtech.com/index.php?pg=phpmailerfe
Wednesday, November 4th, 2009 at 1:08 am
BagoZOnde, the link did not work. javatechie, I have already read all the documentaion for phpmailer-FE, but it still does not specifically help me understand wher/how to use this code with the Form Edition of phpmailer. I am definately noobie, so I might require a little more specific instruction. I apologize and thank you for all your assistance.
equire(”phpmailer/class.phpmailer.php”);
$mail->IsSMTP();
$mail->Host = ’ssl://smtp.gmail.com:465′;
$mail->SMTPAuth = TRUE;
$mail->Username = ”; // Change this to your gmail adress
$mail->Password = ”; // Change this to your gmail password
$mail->From = “”;
$mail->FromName = “”;
$body=”html content”;
$text_body=”alternate text content”;
$mail->Body = $body;
$mail->AltBody = $text_body;
$mail->AddAddress(”recipient email”, ‘recipient name’);
if(!$mail->Send())
echo “There has been a mail error sending to ” . $row[3] . // Clear all addresses and attachments for next loop
$mail->ClearAddresses();
?>
Wednesday, November 4th, 2009 at 1:08 am
To be more specific. I have a working file named mail.php with this code:
IsSMTP();
$mail->Username = “*******@gmail.com”;
$mail->Password = “******”;
$mail->From = “******@gmail.com”;
$mail->FromName = “test User Name”;
$mail->Subject = “Test Subject”;
$mail->AddAddress(“******@gmail.com”);
$mail->Body = “Hey buddy, heres an email!”;
$mail->Host = “ssl://smtp.gmail.com”; //
$mail->Port = 465; //
//$mail->Mailer = ’smtp’; //
//$mail->SMTPAuth = true; //
$mail->Send();
if(!$mail->Send())
echo ‘Message has been sent’;
?>
When I call this script as the action of a form, it will obviously only send me subject line Test Subject with body “Hey buddy, heres an email!”. How to I change this to conclude information from a form? As much information as possible would be hugely appreciated.
Wednesday, November 4th, 2009 at 1:08 am
yes, I have solution but I can’t submit that here because all html code is included into this site, not a separate code lines.
So I tell you what you must do without including signs: > and <:
form name=”myform” action=”index.php?go=send” method=”POST”
input name=”datahere” type=”text”
/form
a href=”javascript:document.myform.submit();”>submit here
You looking for that solution? Please tell me If I’m right
.
Best wishes
Wednesday, November 4th, 2009 at 1:08 am
I see a part of PHP is not included on that forum, so here’s a second part of this solution:
$gofor=$_GET['go'];
if (!strcmp($gofor,”send”)){
$mydata=$_POST['data'];
$body = “It’s yours $mydata”;
//here paste all procedure for sending e-mail
//if e-mail was send successfully.
print(“E-mail was send. Thank you!”);
}else{
//here include that form
}
Better way is to send me e-mail on vintage.carousel.design@gmail.com, then I can send you solution because I still don’t know how to submit code here (some parts with > and < are ignored)
Wednesday, November 4th, 2009 at 1:08 am
I encountered the error as below:
Warning: fsockopen() [function.fsockopen]: SSL: connection timeout in C:\wamp\www\Web\class.smtp.php on line 105
Warning: fsockopen() [function.fsockopen]: Failed to enable crypto in C:\wamp\www\Web\class.smtp.php on line 105
Warning: fsockopen() [function.fsockopen]: unable to connect to ssl://smtp.gmail.com:465 (Unknown error) in C:\wamp\www\Web\class.smtp.php on line 105
Message was not sent
Mailer Error: SMTP Error: Could not connect to SMTP host.
I followed the all the steps but it still not working to me. Please advice. Thanks
Wednesday, November 4th, 2009 at 1:08 am
Hello, maybe you enter wrong path to class.smtp.php? It’s located usually in phpmailer/ drawer. Check it.
Wednesday, November 4th, 2009 at 1:08 am
I’m trying with gmail server and with other (no ssl) and I have the next error:
SMTP Error: Could not authenticate
where is the problem?
Wednesday, November 4th, 2009 at 1:08 am
I don’t know this for sure, but it’s something with Your gmail account. Try to find there authentification options and then try to change them. If that won’t help, we’ll find other way. Your code is similiar to mine?
Wednesday, November 4th, 2009 at 1:08 am
No !!! I found the problem. phpMailer V2.3
Change to V5 and OK
Thank’s anyway
Wednesday, November 4th, 2009 at 1:08 am
It’s good to know that, thanks!
I was using version 5 for a long time.
It’s great that working for You!
Best Regards!!
Wednesday, November 4th, 2009 at 1:08 am
I tried the above code. But it does not work. Can anyone tell me wats the problem?
Wednesday, November 4th, 2009 at 1:08 am
Can we manipulate the sender or I mean “From” to change it with another email address rather than the email address set in smtp username? I think this is a bug. In version 5.0.2, no matter how I change “$mail->SetFrom” to another value it still picks up the my gmail account used as smtp.
Wednesday, November 4th, 2009 at 1:08 am
Hi Darren,
You can check out below link -
http://www.askapache.com/php/phpfreaks-eric-rosebrocks-phpmailer-tutorial.html
Wednesday, November 4th, 2009 at 1:08 am
Works like a charm. It first gave SSL errors, but after setting the host and the port before #connect resolved it all. I love this tutorial
Cheers!!
Wednesday, November 4th, 2009 at 1:08 am
It works as well for me, except that the mail is going to gmail, same as Darren’s post above. Did anyone who had similar problem found the solution for this? Thanks a lot in advance!
Wednesday, November 4th, 2009 at 1:08 am
Could you please tell me how can I setup to send the mail confirmation to the recipient, not to my address? Many thanks.