Sending emails using PHPMailer_v5.0.2 and GMail
Requirements :
1 : PHPMailer source code
2 : GMail account
Download the PHPMailer source code from http://sourceforge.net/project/showfiles.php?group_id=26031&package_id=252700
Now create a file you call mail.php on your harddisk and open it with your favorite text editor. Paste this code into it :
<?php
//error_reporting(E_ALL);
error_reporting(E_STRICT);
date_default_timezone_set('America/Toronto');
require_once('../class.phpmailer.php');
//include("class.smtp.php"); // optional, gets called from within class.phpmailer.php if not already loaded
$mail = new PHPMailer();
$body = file_get_contents('contents.html');
$body = eregi_replace("[\]",'',$body);
$mail->IsSMTP(); // telling the class to use SMTP
$mail->Host = "mail.yourdomain.com"; // SMTP server
$mail->SMTPDebug = 2; // enables SMTP debug information (for testing)
// 1 = errors and messages
// 2 = messages only
$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 = "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";
$mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
$mail->MsgHTML($body);
$address = "whoto@otherdomain.com";
$mail->AddAddress($address, "John Doe");
$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!";
}
?>

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.
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();
?>
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.
yup..thanks a ton..its helped me out of a week-long struggle..
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!
How do I incorporate this with phpmailer-FE as an html form.
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
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.
Hi rob!! You can see below link, It might be useful for you.
http://phpmailer.codeworxtech.com/index.php?pg=phpmailerfe
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();
?>
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.
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
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)
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
Hello, maybe you enter wrong path to class.smtp.php? It’s located usually in phpmailer/ drawer. Check it.
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?
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?
No !!! I found the problem. phpMailer V2.3
Change to V5 and OK
Thank’s anyway
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!!
I tried the above code. But it does not work. Can anyone tell me wats the problem?
nao deu certo
Hello, just send me an email on vintage.carousel.design@gmail.com if You have some problems with GMAIL. I put some code for another site using GMAIL and it’s still working so I have no problems with it. Everything depends on proper configuration with paths to catalogue where PHPMailer is located.
[...] Next obstacle. So for whatever reason, PHP’s mail() function wasn’t working for me. I decided to use the phpmailer class included with WordPress located at /wp-includes/class.phpmailer.php. Some sample code for PHPMAILER is here. [...]
Amazing!!!
thanks a lot for the great work
Will try it immediately
Dear All,
I’ve tried the code
above but it’s not working,
it says:
SMTP Error: Could not connect to SMTP host.
I belive I am not type anything error in my username or password, do I have to configure my gmail too?
my question is that, can I do this code from
Localhost windows xp apache 2.045.
Thank you
I am facing the same problem. I am using the same above code but getting SMTP Error: Could not connect to SMTP host error. Is anybody tell me how to cater the problem. Is there any way to find the real problem.
It works on Windows but it is not working on hostmonster account. Weird.