Send email from localhost/online server using php


Sending emails from localhost is one of the most frequently asked question from young learners. Even professionals like us need this script to test emails from our local server (localhost) / online server when developing projects. In this tutorial I will be explaining to send email from localhost/online server using php.

Using Phpmailer library to send emails

Phpmailer library is my personal favorite and in fact loved by estimated 9 million users worldwide. Using phpmailer you can send html emails, text emails, via smtp, qmail. You can also cc and bcc to multiple recipient. You can even download the phpmailer library from github and sourceforge. Have a look at some of the examples here.

Configuring phpmailer to send email via smtp

I am assuming that you have a google email (gmail) account, because this is the easiest way to send email via smtp. If you have accounts in hotmail, rediffmail or any other emails service it can be done too. Follow the steps to configure phpmailer library to send email.

The first step is to download phpmailer library zip file from the phpmailer website/github/sourceforge. Once you are done with that, extract to your project folder which is hosted on local (localhost) / online server.

Include the phpmailer class file in your php file. This will be the page from where you will be sending emails.

Instantiate the phpmailer class i.e. create an object of phpmailer class.

Set the class to use smtp method.

If you want to debug the code or check the outputs after sending mail use this option.

Enable smtp authentication. It is recommended to use it.

Set email sending mode, smtp server and port no. Again I am assuming that you are using gmail credentials.

Give your email address and password. Also set the sender email address and name

If you want to add one or multiple recipient, add this line below.

You can also carbon copy (cc)/ blind carbon copy (bcc) to the recipient.

Add message body. You can use simple text or any formatted html template.

If you want to attach any files you can do the same using this code below.

Triple check your configuration and send the mail now.


If you have any suggestion/problem regarding this tutorial, comment in the form below.


About Author

Entrepreneur & Dreamer

I am a passionate Software Professional, love to learn and share my knowledge with others.

Software is the hardware of my life.


  1. Hi ,

    Firstly thanks for sharing .
    I tried the above code updated the credentials for in page “testmail-gmail-smtp.php” and it worked fine .

    But sending mail to multiple people does not works even thought I give my my mail ID and username .

    Please help thanks

  2. i have download the code and configure it as you explained
    but it gives following error

    Fatal error: Class ‘SMTP’ not found in C:\wamp\www\coding-cage\step-blogging\PHPMailer-master\PHPMailer-master\class.phpmailer.php on line 1290

  3. i have tried it and provide same gmail and password but it gives following error even my gmail id or password is correct …

    2015-07-23 17:57:50 CLIENT -> SERVER: EHLO localhost 2015-07-23 17:57:50 CLIENT -> SERVER: AUTH LOGIN 2015-07-23 17:57:51 CLIENT -> SERVER: cHJhZGVlcGtob2RrZTRAZ21haWwuY29t 2015-07-23 17:57:52 CLIENT -> SERVER: cHJhZGVlcGtob2RrZTIwMTU= 2015-07-23 17:57:53 SMTP ERROR: Password command failed: 534-5.7.14 Please log in via your web browser and 534-5.7.14 then try again. 534-5.7.14 Learn more at 534 5.7.14 br14sm5536461pdb.47 – gsmtp 2015-07-23 17:57:53 SMTP Error: Could not authenticate. 2015-07-23 17:57:53 CLIENT -> SERVER: QUIT 2015-07-23 17:57:54 SMTP connect() failed. Message could not be sent.Mailer Error: SMTP connect() failed.

      • Still its not working

        $mail->SMTPAuth = true;
        $mail->SMTPDebug = 1;
        $mail->Username = “”;
        $mail->Password = “mygmailpwd1254”;
        $mail->SMTPSecure = “ssl”; // sets the prefix to the servier
        $mail->Host = “”; // sets GMAIL as the SMTP server
        $mail->Port = 465;

  4. 2016-02-12 07:35:39 CLIENT -> SERVER: EHLO 2016-02-12 07:35:39 CLIENT -> SERVER: AUTH LOGIN 2016-02-12 07:35:39 CLIENT -> SERVER: dGVzdC5kZXZlbG9wZXJzMjAxMkBnbWFpbC5jb20= 2016-02-12 07:35:39 CLIENT -> SERVER: bm9wYXNzMTIzNA== 2016-02-12 07:35:39 SMTP ERROR: Password command failed: 534-5.7.14 Please log in via your web browser and 534-5.7.14 then try again. 534-5.7.14 Learn more at 534 5.7.14 y196sm1107704oia.24 – gsmtp 2016-02-12 07:35:39 CLIENT -> SERVER: QUIT SMTP Error: Could not authenticate.

    getting this error

  5. 1. I used it on localhost, it worked fine and sent message to the mail provided but I tried hosting the demo on an online server, it is saving the data in online database but it is given the error SMTP connect() failed and not sending the message.


    Its not working at all……..!
    SMTP could not authenticate and I am not doing any mistake. I checked StackOverFlow too nothing working.

  7. Does this code only works with the gmail service?? If want to implement with other hosting service it possible in PHPMailer??
    Thanks in advance


    I WANT TO ADD MORE FIELDS BUT I DON’T KNOW HOW TO PRINT ALL DATA OF FIELDS AND IF I WANT TO PRINT THE php form DATA IN $message=” SECTION IT got Parse error: syntax error, unexpected ‘”‘, expecting T_STRING or T_VARIABLE or T_NUM_STRING error.
    so tell me how to send the data of more than five or six fields.

  9. Sir, I downloaded source file and changed username and password but i am getting SMTP connect() failed error.

    I need solutions.

    SMTP ERROR: Failed to connect to server: Connection timed out (110) SMTP connect() failed

Leave A Reply

CommentLuv badge

Get more stuff like this
in your inbox

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.