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
Share or Bookmark This Post With :
|
|
Posted On January 16th, 2008
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 |
|
|
Posted On March 1st, 2008
require(“phpmailer/class.phpmailer.php”); |
|
|
Posted On July 16th, 2008
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. |
|
|
Posted On January 6th, 2009
yup..thanks a ton..its helped me out of a week-long struggle.. |
|
|
Posted On January 16th, 2009
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 |
|
|
Posted On February 12th, 2009
How do I incorporate this with phpmailer-FE as an html form. |
|
|
Posted On February 12th, 2009
Just create form, it’s an example (all code): You looking for that solution? Please tell me If I’m right |
|
|
Posted On February 12th, 2009
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. |
|
|
Posted On February 16th, 2009
Hi rob!! You can see below link, It might be useful for you. |
|
|
Posted On February 17th, 2009
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. |
|
|
Posted On February 17th, 2009
To be more specific. I have a working file named mail.php with this code: if(!$mail->Send()) 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. |
|
|
Posted On February 17th, 2009
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 |
|
|
Posted On February 17th, 2009
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. }else{ 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) |
|
|
Posted On March 13th, 2009
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 I followed the all the steps but it still not working to me. Please advice. Thanks |
|
|
Posted On March 13th, 2009
Hello, maybe you enter wrong path to class.smtp.php? It’s located usually in phpmailer/ drawer. Check it. |
|
|
Posted On May 10th, 2009
I’m trying with gmail server and with other (no ssl) and I have the next error: where is the problem? |
|
|
Posted On May 11th, 2009
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? |
|
|
Posted On May 12th, 2009
No !!! I found the problem. phpMailer V2.3 |
|
|
Posted On May 12th, 2009
It’s good to know that, thanks! I was using version 5 for a long time. It’s great that working for You! |
|
|
Posted On May 28th, 2009
I tried the above code. But it does not work. Can anyone tell me wats the problem? |
|
|
Posted On October 8th, 2009
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. |
|
|
Posted On October 8th, 2009
Hi Darren, You can check out below link - http://www.askapache.com/php/phpfreaks-eric-rosebrocks-phpmailer-tutorial.html |
|
|
Posted On November 18th, 2009
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!! |
Leave a Reply
- Tweetie – Twitter client for iPhone
- PDFCreator – free tool to create PDF files f...
- Pidgin – the universal chat client
- Skyfire – Free & Fastest mobile web bro...
- Google Apps phasing out support for Microsoft Inte...
- Poedit – cross-platform gettext catalogs (.p...
- PDFmyURL.com – Convert and save pdf from any...
- Gallery – open source web based photo album ...
- ShoZu – Social Media Hub for Your Mobile
- Widsets is now OVI store
- Send emails using PHPMailer an...
- Firefox Addons Essential for S...
- URL Rewriting for PHP Web Appl...
- Free SEO Tools From SEOMoz.org...
- Multiple Instances of Tomcat w...
- Apache 2.x + Tomcat 4.x + Load...
- Pagination in JSP/Java
- PHP – Mysql Open Source ...
- java.lang.OutOfMemoryError: Ja...
- HOW TO Subversion+Apache on Fe...





