Send Mail using Gmail SMTP with PHP.
Wall Script
Follow Me:
Monday, July 04, 2011

Send Mail using Gmail SMTP with PHP.

I received a tutorial requests from my reader that asked to me how to send mails to web project users. This post explains you how to send mail using SMTP credentials. I had implemented this mail notification system at labs.9lessons.info using SMTP and SASL classes. It’s simple just few lines of configuration changes.

Send Mail using SMTP and PHP.


Download Script     Live Demo

index.php
Contains PHP code. You have to modify email, name and message
$to="user@gmail.com";
$fn="Fisrt Name";
$ln="Last Name";
$name=$fn.' '.$ln;
$from="support@website.com";
$subject = "Welcome to Website";
$message = "Dear $name,


Your Welcome Message.


Thanks
www.website.com
";
include('smtpwork.php');

smtpwork.php
SMTP configuration file you have to modify host, port and credentials.
<?php
require("smtp.php");
require("sasl.php"); //SASL authentication
$from="support@yourwebsite.com";
$smtp=new smtp_class;
$smtp->host_name="www.website.com"; // Or IP address
$smtp->host_port=25;
$smtp->ssl=0;
$smtp->start_tls=0;
$smtp->localhost="localhost";
$smtp->direct_delivery=0;
$smtp->timeout=10;
$smtp->data_timeout=0;
$smtp->debug=1;
$smtp->html_debug=1;
$smtp->pop3_auth_host="";
$smtp->user="support@website.com"; // SMTP Username
$smtp->realm="";
$smtp->password="password"; // SMTP Password
$smtp->workstation="";
$smtp->authentication_mechanism="";

if($smtp->SendMessage(
$from,
array(
$to
),
array(
"From: $from",
"To: $to",
"Subject: $subject",
"Date: ".strftime("%a, %d %b %Y %H:%M:%S %Z")
),
"$message"))
{
echo "Message sent to $to OK.";
}
else
echo "Cound not seend the message to $to.\nError: ".$smtp->error;
?>
Was this article helpful?
Thanks! Your feedback helps us to improve 9lessons.info


63 comments:

  1. is it better than phpmailer?

    ReplyDelete
  2. thanks my brother
    you are the best

    ReplyDelete
  3. the $message is html or text

    ReplyDelete
  4. Another great handler ! Thnx ! One question: is it possible to send personalized mail, for example to put array of mail adress in bcc ?

    ReplyDelete
  5. Hola! lo probé localmente y me sale error al tratar de enviar un correo de prueba: Cound not send the message to abmtoles@gmail.com. Error: could not connect to the host "192.168.1.XX": No se ha podido establecer conexión ya que el equipo de destino ha denegado activamente dicha conexión.
    alguien me podría ayudar?

    ReplyDelete
  6. Thanks for this tutorial.
    However I wanted to know, how can i send mail from my localhost... what smtp settings i need to make, and how to configure a local smtp server..

    ReplyDelete
  7. Hey srinivas thanx for the great tutorial but can you please take time and post a tutorial to SEND AND RECEIVE emails ??? it could be useful for many applications like inbox ... thanks in advance ... you really rock man.

    ReplyDelete
  8. Thanks for the tutorial...I have concern on how can i send mail from my localhost using Gmail as SMTP server using this tutorial of yours. Is it possible.Please make a tutorial too..thank you

    ReplyDelete
  9. I hope it just like PHP Mailer ony if I am right

    ReplyDelete
  10. there are some error in this string:

    echo "Cound not seend the message to $to.\nError: ".$smtp->error;

    und more...
    what kann i do, if i will E-mail with attaching send (file, video, pictire etc...)???

    ReplyDelete
  11. really is a powerful tutorial..thank you for sharing

    ReplyDelete
  12. Its nuseful post great work.........Thanks

    ReplyDelete
  13. helpful tut, for PHP beginners like me :)

    ReplyDelete
  14. I was in search of this code from last many years for a client having PHP website.
    Thanks for sharing

    ReplyDelete
  15. can we send html emails with this

    ReplyDelete
  16. Hi how to send text-massages to mobile using php

    ReplyDelete
  17. I am getting an error like this


    Warning: require(/home/yourad6/public_html/faster-key/includes/welcomeMailer/plain_sasl_client.php) [function.require]: failed to open stream: No such file or directory in /home/yourad6/public_html/faster-key/includes/welcomeMailer/sasl.php on line 313

    what is plain_sasl_client.php this file doesnt exist and isnt in the download

    ReplyDelete
  18. I'm getting this error. Rectify soon please

    Cound not send the message to raj@gmail.com Error: could not connect to the host "208.43.198.157": No connection could be made because the target machine actively refused it.

    ReplyDelete
  19. Hi!

    I can't send HTML message this. If i send a simple text message the emial arrives. But if i send a HTML message the it doesnt.
    Please help me!

    ReplyDelete
  20. I found that the plain_sasl_client.php file is missing, but got it there and it now works fine:
    http://www.phpclasses.org/browse/file/7492.html

    ReplyDelete
  21. can you also include your sasl.php and smtp.php in your post?

    ReplyDelete
  22. Hi please help i get this error "Cound not send the message to xxxx@gmail.com. Error: server does not require authentication".

    ReplyDelete
  23. Hi, I have a question.. Where I need define the drivers used in the sasl.php file?

    var $drivers=array(
    "Digest" => array("digest_sasl_client_class", "digest_sasl_client.php" ),
    "CRAM-MD5" => array("cram_md5_sasl_client_class", "cram_md5_sasl_client.php" ),
    "LOGIN" => array("login_sasl_client_class", "login_sasl_client.php" ),
    "NTLM" => array("ntlm_sasl_client_class", "ntlm_sasl_client.php" ),
    "PLAIN" => array("plain_sasl_client_class", "plain_sasl_client.php" ),
    "Basic" => array("basic_sasl_client_class", "basic_sasl_client.php" )
    );

    ReplyDelete
  24. I'm getting the following error "Error: 550 Access denied - Invalid HELO name (See RFC2821 4.1.1.1)"

    Please can someone help me regarding this!

    ReplyDelete
  25. Hi ,

    I see everyone is asking about sending HTML mails using above class. The solution is you need to modify the headers. Like:

    "MIME-Version: 1.0",
    "Content-type: text/html; charset=iso-8859-1",
    "From: $from",
    "To: $to",
    "Subject: $subject",
    "Date: ".strftime("%a, %d %b %Y %H:%M:%S %Z")

    Thanks...

    ReplyDelete
  26. How to send email to multiple recipients in Bcc?

    ReplyDelete
  27. hello sir,

    i want script that when user enter his or her detail then after submitting form the password is send to his email address & after adding that password user can login.

    ReplyDelete
  28. very good form . but i like if you explain line by line this code what that each line doing.

    ReplyDelete
  29. Hello Sir,
    How can we send mail to anyone by this.
    plz help me sir

    ReplyDelete
  30. Your code is working perfectly when I am trying to send email to one email stored in database

    What if I want to send emails from a list of emails stored in database.

    where to repeat the loop and in which file??

    ReplyDelete
  31. Nice coding bro..

    ReplyDelete
  32. smtp.php missing , where to get it??

    ReplyDelete
  33. How to send email with pdf attachment???

    ReplyDelete
  34. hi I have one doubt ...... please any one tell me........ that........ is it possible to send a mail using SMTP in javascript...... if it can please tell me how.....

    ReplyDelete
  35. to Anonymous ^ up
    you can send using js by ajax just call the php file that send's messages by ajax

    ReplyDelete
  36. connection refussed error
    pls help me>

    ReplyDelete
  37. Nice information
    thanks a lot for sharin.

    ReplyDelete
  38. Thanks for everything
    Very nice blog.

    ReplyDelete
  39. Where are those 2 files smtp.php and sasl.php?

    ReplyDelete
  40. can send multiple attachment throw smtp

    ReplyDelete
  41. Can you teach me to send mail width smtp's gmail ? Thanks

    ReplyDelete
  42. Error: could not start TLS connection encryption protocol ..
    Please reply fast and must...

    ReplyDelete
  43. how to attach pdf using this? Please guide

    ReplyDelete
  44. server does not require authentication, it probably requires starting TLS

    ReplyDelete
  45. Where can I find s suitable login_sasl_client.php file for use with 9lessons SMTP login solution?

    I now get an error message: "PHP Warning: require(/usr/local/pem/vhosts/111236/webspace/httpdocs/smtp/login_sasl_client.php): failed to open stream: No such file or directory in /usr/local/pem/vhosts/111236/webspace/httpdocs/smtp/sasl.php on line 313, referer: "

    ReplyDelete
  46. Hello, you're script work fine but when i use it in a while boucle i have this error message :
    Message sent to ********************* OK.
    PHP Fatal error: Cannot redeclare class smtp_class in /var/www/html/smtp.php on line 47
    Do you know what's appened ?

    Thank you,
    Séb

    ReplyDelete
  47. Hello there,

    Thanks for the script, it works fine. But I have 2 questions for you, 1. how do we send out an email which is custom drafted in html?
    2. How do we attach files to the email?
    Please guide if there are any class files to be used for the above 2.

    Thanks in advance.

    ReplyDelete
  48. Good one.
    It also can be done very easily using PHPMailer. More information about how to easily configure PHPMail to send emails from localhost using GMAIL SMTP or any other SMTP can be found here http://www.mittalpatel.co.in/php_send_mail_from_localhost_using_gmail_smtp . Hope this helps.

    ReplyDelete
  49. very good blog, thanks for sharing this post.

    ReplyDelete
  50. It is going to spam or promotions in gmail. How to avoid that

    ReplyDelete
  51. Thank you very much. :)

    ReplyDelete
  52. . Error: server does not require authentication, it probably requires starting TLS
    I am getting above error
    my host is smtp.gmail.com

    ReplyDelete
  53. which method is best for sending bulk mail please help

    ReplyDelete
  54. Error: it was not possible to read line from the SMTP server: data access time out

    ReplyDelete
  55. Error: it was not possible to read line from the SMTP server: data access time out

    ReplyDelete
  56. Warning: require(/home/oasis/public_html/admin/smtp/plain_sasl_client.php): failed to open stream

    i can get this error

    ReplyDelete
  57. Hi ,

    I am not able to find smtp.php and sasl.php file. Please help me.

    Thanks

    ReplyDelete

Make in India