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.
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');
$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;
?>
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;
?>
...
ReplyDeleteI LIKE THIS BLOG...
Thanks.
is it better than phpmailer?
ReplyDeletethanks my brother
ReplyDeleteyou are the best
the $message is html or text
ReplyDeleteAnother great handler ! Thnx ! One question: is it possible to send personalized mail, for example to put array of mail adress in bcc ?
ReplyDeleteHola! 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.
ReplyDeletealguien me podría ayudar?
Thanks for this tutorial.
ReplyDeleteHowever 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..
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.
ReplyDeleteThanks 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
ReplyDeleteI hope it just like PHP Mailer ony if I am right
ReplyDeletethere are some error in this string:
ReplyDeleteecho "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...)???
really is a powerful tutorial..thank you for sharing
ReplyDeleteIts nuseful post great work.........Thanks
ReplyDeletehelpful tut, for PHP beginners like me :)
ReplyDeleteI was in search of this code from last many years for a client having PHP website.
ReplyDeleteThanks for sharing
can we send html emails with this
ReplyDeleteHi how to send text-massages to mobile using php
ReplyDeleteI am getting an error like this
ReplyDeleteWarning: 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
I'm getting this error. Rectify soon please
ReplyDeleteCound 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.
Hi!
ReplyDeleteI 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!
I found that the plain_sasl_client.php file is missing, but got it there and it now works fine:
ReplyDeletehttp://www.phpclasses.org/browse/file/7492.html
can you also include your sasl.php and smtp.php in your post?
ReplyDeleteHi please help i get this error "Cound not send the message to [email protected]. Error: server does not require authentication".
ReplyDeleteHi, I have a question.. Where I need define the drivers used in the sasl.php file?
ReplyDeletevar $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" )
);
I'm getting the following error "Error: 550 Access denied - Invalid HELO name (See RFC2821 4.1.1.1)"
ReplyDeletePlease can someone help me regarding this!
Hi ,
ReplyDeleteI 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...
good one :)
ReplyDeleteGood class
ReplyDeleteHow to send email to multiple recipients in Bcc?
ReplyDeletehello sir,
ReplyDeletei 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.
very good form . but i like if you explain line by line this code what that each line doing.
ReplyDeleteHello Sir,
ReplyDeleteHow can we send mail to anyone by this.
plz help me sir
Your code is working perfectly when I am trying to send email to one email stored in database
ReplyDeleteWhat if I want to send emails from a list of emails stored in database.
where to repeat the loop and in which file??
Good tutorial!
ReplyDeleteits not working.
ReplyDeleteNice coding bro..
ReplyDeletesmtp.php missing , where to get it??
ReplyDeleteHow to send email with pdf attachment???
ReplyDeletehi 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.....
ReplyDeleteto Anonymous ^ up
ReplyDeleteyou can send using js by ajax just call the php file that send's messages by ajax
connection refussed error
ReplyDeletepls help me>
Nice information
ReplyDeletethanks a lot for sharin.
Thanks for everything
ReplyDeleteVery nice blog.
Where are those 2 files smtp.php and sasl.php?
ReplyDeletecan send multiple attachment throw smtp
ReplyDeleteCan you teach me to send mail width smtp's gmail ? Thanks
ReplyDeletehow to attach pdf using this? Please guide
ReplyDeleteHow to send Cc using this code.
ReplyDeleteserver does not require authentication, it probably requires starting TLS
ReplyDeleteWhere can I find s suitable login_sasl_client.php file for use with 9lessons SMTP login solution?
ReplyDeleteI 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: "
Hello, you're script work fine but when i use it in a while boucle i have this error message :
ReplyDeleteMessage 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
Hello there,
ReplyDeleteThanks 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.
very good blog, thanks for sharing this post.
ReplyDeleteIt is going to spam or promotions in gmail. How to avoid that
ReplyDeleteThank you very much. :)
ReplyDelete. Error: server does not require authentication, it probably requires starting TLS
ReplyDeleteI am getting above error
my host is smtp.gmail.com
which method is best for sending bulk mail please help
ReplyDeleteError: it was not possible to read line from the SMTP server: data access time out
ReplyDeleteError: it was not possible to read line from the SMTP server: data access time out
ReplyDeleteme too
DeleteWarning: require(/home/oasis/public_html/admin/smtp/plain_sasl_client.php): failed to open stream
ReplyDeletei can get this error
Hi ,
ReplyDeleteI am not able to find smtp.php and sasl.php file. Please help me.
Thanks