Forgot your password?

xtreemhost

How to send e-mails via SMTP with PHPmailer and Gmail?

Introduction

Lately, we’re receiving a lot of support questions regarding the use of Gmail SMTP on the free hosting plan. With Gmail SMTP you can send mails from your website without using the default PHP Sendmail. You can use it for your forums, online contact forms etc… In this tutorial, we will explain you the benefits of using Gmail SMTP and give you a tutorial how you can use it in your PHP pages hosted at XtreemHost!

Why using Gmail for sending mail messages?

First of all it’s FREE! Sure most website owners can use their own SMTP server for sending email messages from their website, but it makes sense even than to use Gmail for sending mail. The chance is big that your websites IP address is on a blacklist if your site is on hosted by a shared web hosting provider. If not or you host your site on your own server, there is always a risk that your IP address get blacklisted. Because of some limitations, the SMTP server from Google is a good choice applications with less than 500 recipients a day, check this information from the Google help pages. Furthermore, our free hosting plan meets all requirements to use Google SMTP without problems.

Tutorial: How to setup SMTP for Gmail?

  1. If you don’t have one, register a Gmail account or setup your domain for Google applications.
  2. Download the most recent version of PHPMailer
  3. Extract the PHPMailer packages to a folder named phpmailer and upload that folder to your XtreemHost hosting account (using FTP)
  4. Include the phpmailer class file in the page that will send an email. Just include following code in your PHP page: require("class.phpmailer.php");
  5. Now, include the below code and edit it with your Gmail account details:
    <?php  
     
    require("class.phpmailer.php"); // path to the PHPMailer class
     
    $mail = new PHPMailer();  
     
    $mail->IsSMTP();  // telling the class to use SMTP
    $mail->Mailer = "smtp";
    $mail->Host = "ssl://smtp.gmail.com";
    $mail->Port = 465;
    $mail->SMTPAuth = true; // turn on SMTP authentication
    $mail->Username = "yourusername@gmail.com"; // SMTP username
    $mail->Password = "yourpassword"; // SMTP password 
     
    $mail->From     = "email address sender";
    $mail->AddAddress("email address receiver");  
     
    $mail->Subject  = "First PHPMailer Message";
    $mail->Body     = "Hi! \n\n This is my first e-mail sent through PHPMailer.";
    $mail->WordWrap = 50;  
     
    if(!$mail->Send()) {
    echo 'Message was not sent.';
    echo 'Mailer error: ' . $mail->ErrorInfo;
    } else {
    echo 'Message has been sent.';
    }
    ?>
  6. Test the page you created and see if the email is sent successfully.

We hope this small tutorial will help everyone that is facing problems with the default PHP Sendmail option that we provide.

This entry was posted in News, Uncategorized and tagged , , , . Bookmark the permalink.
  • http://bajdec-03.co.cc xth_3254275

    Hello!
    Nice tutorial, I’ll use it.
    But I’d like to ask you somethink. Is it possible to connect to pop3 server and read mail from it using PHP? Because I’d like to make app for mobile phone that connect to server and check mail :D I hope you understand.
    Sory for my bad english. I’m from Slovenia and here we speak Slovene :D And I’m 13 years old and still learning English.

  • admin

    In that case, you better try the free edition of Google Apps. It offers POP3, IMAP and SMTP for free :)

  • Jack Ho

    hello! i use your tutorial,.But i got error .”
    SMTP Error: Could not connect to SMTP host. Message was not sent.Mailer error: SMTP Error: Could not connect to SMTP host.”.

  • Jack Ho

    I use wamp.

  • http://refinance-pro.com Gary

    So what do I do if the Gmail ports are blocked by my isp?

    I have checked my Firewall, and they are open on my machine.. I cannot telnet to ports 25,465, 587.

    Do i I need to use an other smpt provider instead of Gmail, or is there some way to redirect the Ports??
    its very techy for me.. having troulble understanding all this.
    Can someone please help
    plz e mail me at gary@4x4u.info with ideas.

    Thanx
    Gary

  • admin

    It doesn’t matter if the Gmail ports are blocked by your ISP. They are not blocked by XtreemHost, so that should be enough…

  • http://don'twantitpublished Alex

    Works great on your host, Thank You!

    But is there any way to send the mail as it came from another email ?

    ex:
    $mail->From=”mailer@example.com”;
    $mail->FromName=”My site’s mailer”;

    …this one doesn’t work.

  • admin

    @ Alex: that’s possible with the free edition of Google Apps.

  • lina

    hello!

    i wanna ask u, how to do step number 3 Extract the PHPMailer packages to a folder named phpmailer and upload that folder to your XtreemHost hosting account (using FTP) ?

    thank you

  • admin

    @ lina: It’s very simple. After you have downloaded phpMailer, just extract it on your local pc and upload everything to a folder inside your htdocs folder called phpmailer. You can do that with every popular FTP client like FileZilla.

  • http://emantisoy.vze.com Websprove

    thanks Sir…..

  • http://emantisoy.vze.com Websprove

    hope the other people will not abuse your hosting…Coz it will use for spamming..

  • xXmikeTotXx

    I’m trying to open forum but sometimes there are no images on it :(

  • http://www.vnbinfotech.com Arivu

    sir! I’m used your tutorial,.But i got error .”
    I have Problem that is”SMTP Error: Could not connect to SMTP host. Message was not sent.Mailer error: SMTP Error: Could not connect to SMTP host.”.
    I use LAMP

  • http://www.angelsofdeath.xtreemhost.com Reaper2794

    I don’t know where to upload the file to… I uploaded the folder but how do I know what page should send the e-mail…

  • doyie

    hi there. I used the codes above, but it says there that SMTP Error: Could not connect to SMTP host. Message was not sent.Mailer error: SMTP Error: Could not connect to SMTP host.”.

    waaah.. i don’t know how to solve it. can u help me pls?? T-T im using wamp server! thanks!:)

  • doyie

    hello there.. i tried the code above, but i always get an error that says ”SMTP Error: Could not connect to SMTP host. Message was not sent.Mailer error: SMTP Error: Could not connect to SMTP host.”. how can i do it? help me pls.. thank you.

  • http://ckap.in Anantha Prasad

    I’m getting the following error.

    SMTP Error: Could not connect to SMTP host. Message was not sent.Mailer error: SMTP Error: Could not connect to SMTP host.

    Is there a solution for this? I’m using WAMP server on windows XP

  • Ashish Jagnani

    I’m getting the following error……………..

    SMTP Error: Could not connect to SMTP host. Message was not sent.Mailer error: SMTP Error: Could not connect to SMTP host.

  • xtreemhost

    This won’t work on all hosts, please check the premium plan at
    http://www.xtreemhost.com/plans which has SMTP, remote SMTP and
    Sendmail enabled.

  • i hate free webhost

    I tried he code at my localhost and it worked as expected, but when I uploaded it to my free web server (i.e. 0fees), I get this error:
    SMTP Error: Could not connect to SMTP host. Mailer Error: SMTP Error: Could not connect to SMTP host.
    please help me.

  • http://profiles.google.com/mmvejdani mohamad mahdi vejdani

    i uploaded it using a ftp software and i placed it in the root folder to test it

  • xtreemhost

    Remote connections are only supported on the premium plan – see
    http://www.xtreemhost.com/plans
    This means you no longer can use SMTP with the free plan.

  • Thebrighter

    I really want to know weather you have blocked or not the SMTP for free hosting plan.

    Straight answer needed 

  • Utila

    Thank you… is working for me

  • Mangesh Desai

    Does xtreemhost support Open SSL for PHP ?

  • Honeyz_235

    tq for this tutorial post..very helpfull..=)

  • Mbitjobs

    if you are unable to send the mail by using above script it means your open_ssl extension is not enable in php.ini file.
    Just uncomment the line in php.ini file then the above script will run perfectly fine. i.e.

    extension=php_openssl.dll

    Thanks for the good script.

  • Akhtar_1989

    keep the nonsence knowledge up till ur blooody shit brain!!!!

  • Rajmohan

    i had followed your steps, but SMTP Error: Could not authenticate.
    Message was not sent.Mailer error: SMTP Error: Could not authenticate. this error occurs. Can you please guide me how to rectify this bug. thanks

  • Gerard

    Thanks worked great!

  • http://www.facebook.com/tarikdemirci1453 Tar?k Demirci

     Tesekkurler. (Helal et lan kulland?k o kadar :))

  • http://twitter.com/RealHyder Real Hyder

    Where is file class.phpmailer.php?? Download not contained.

  • Gretschdrummer

    I’m getting the following error:

    The following From address failed: myaddress@gmail.com

  • http://twitter.com/nadNadi27 My LiFe is Me!

    i got error it show smtp error could not authenticate…

  • http://www.sufalamtech.com/ Web Development Company

    Thanks. At last I’m able to send emails from localhost with XAMPP.
    I know the topic is a bit old, but let me add my few cents – for all those, who have trouble with SMTP errors, remember to uncomment the line
    extension=php_openssl.dll
    in php.ini

  • megha.mdesai

    sir: i tried the above code on my server, got this error

    Message was not sent.Mailer error: The following From address failed: myemailid@gmail.com SMTP server error: Access denied – Invalid HELO name (See RFC2821 4.1.3), plz help me solve it..

  • Sany Ahmed

    Can u tell me please, when i host my site to remote host how can i send mail using this process because then i can’t configure my php.ini file..please reply

  • sudhir

    hi , i am create web application of insert the data after send email at this time error can be occured
    SMTP Error: Could not connect to SMTP host.

  • Ronak Patel

    thank you very much .. i screwed up in this since last 1 month. Thanks a lot..
    It’s working..

  • suganya

    Hi,
    I’m getting an error like “SMTP Error: Could not connect to SMTP host. Message was not sent.Mailer error: SMTP Error: Could not connect to SMTP host.” Please anyone guide me.

  • saminathan

    Hi

    Thanks a lot…Nice tutorial.. Mails are working properly..Great

  • ajoshi31

    Yes same issue and a lot of trouble

  • ajoshi31

    I have hosting in godaddy and need getting error :

    “SMTP -> ERROR: Failed to connect to server: Connection refused (111)

    SMTP Error: Could not connect to SMTP host.
    Message was not sent.Mailer error: SMTP Error: Could not connect to SMTP host.”

    —————————————————————

    I checked by tenet

    It says connection blocked for port 465 ..
    telnet smtp.gmail.com 25 it is working fine and reply response 220 of success
    but smtp.gmail.com 465 is saying connection failed..

    ——————————————————————————————–
    Now if I need to change php.ini file…

    But I am pretty confused I am having a linux hosting and
    when i accessed my server through putty.. I found php.ini php5.ini file in
    /web/conf/

    Ques which one to change:

    ;extension=php_openssl.dll (willl uncommenting this dll change effective in linux hosting)
    or waht to change in linux hosting

  • xtreemhost

    Godaddy might have other SMTP settings (for secureserver.net), the ones we described are for Gmail and the ports are open on our xtreemhost (premium) and Visiba servers.

  • ajoshi31

    how it ran sir .. please help…

  • ajoshi31

    how can we change the values in shared hosting.. the file is read only..

    we tried the run time method
    please check if it is fine:
    if (!extension_loaded(‘openssl’)) {
    ini_set(‘enable_dl’,1);
    if (!dl(‘php_openssl.so’)) {
    exit;
    }
    }

    Localhost it can work but what n server with ssl enabled

  • Haritha

    Hi..did you get the solution?? plz help me..am also facing problem with godaddy.

  • xtreemhost

    Not sure about Godaddy, but this should work without problems with our premium plan – http://www.xtreemhost.com/plans

  • Jay

    The following From address failed: user@gmail.com : Called Mail() without being connected….how to solve this really frustrating

Archives

Premium Info

Did you know the premium plan comes with unlimited features (webspace, bandwidth, email accounts etc.), no restrictions (cURL, Sockets, Sendmail etc.), a Website Builder, Shopping Cart tools and a FREE Domain Name?
Compare our free and premium hosting plan.