Send Mail using Gmail SMTP with PHP.
Wall Script
Wall Script
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="[email protected]";
$fn="Fisrt Name";
$ln="Last Name";
$name=$fn.' '.$ln;
$from="[email protected]";
$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="[email protected]";
$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="[email protected]"; // 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;
?>
web notification

62 comments:

  1. ...
    I LIKE THIS BLOG...
    Thanks.

    ReplyDelete
  2. is it better than phpmailer?

    ReplyDelete
  3. thanks my brother
    you are the best

    ReplyDelete
  4. the $message is html or text

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

    ReplyDelete
  6. Hola! lo probé localmente y me sale error al tratar de enviar un correo de prueba: Cound not send the message to [email protected]. 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
  7. 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
  8. 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
  9. 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
  10. I hope it just like PHP Mailer ony if I am right

    ReplyDelete
  11. 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
  12. really is a powerful tutorial..thank you for sharing

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

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

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

    ReplyDelete
  16. can we send html emails with this

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

    ReplyDelete
  18. 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
  19. I'm getting this error. Rectify soon please

    Cound not send the message to [email protected] Error: could not connect to the host "208.43.198.157": No connection could be made because the target machine actively refused it.

    ReplyDelete
  20. 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
  21. 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
  22. can you also include your sasl.php and smtp.php in your post?

    ReplyDelete
  23. Hi please help i get this error "Cound not send the message to [email protected]. Error: server does not require authentication".

    ReplyDelete
  24. 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
  25. 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
  26. 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
  27. How to send email to multiple recipients in Bcc?

    ReplyDelete
  28. 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
  29. very good form . but i like if you explain line by line this code what that each line doing.

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

    ReplyDelete
  31. 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
  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. how to attach pdf using this? Please guide

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

    ReplyDelete
  44. 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
  45. 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
  46. 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
  47. very good blog, thanks for sharing this post.

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

    ReplyDelete
  49. Thank you very much. :)

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

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

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

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

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

    i can get this error

    ReplyDelete
  55. Hi ,

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

    Thanks

    ReplyDelete

mailxengine Youtueb channel
Make in India
X