PHP have inbuilt mail() function to send email right from the code. This is easy to use and effective too but it has one serious problem. It gives load to production server and also provide no guarantee about email delivery.
PHPMailer is one of the popular and easy to use email library for php. This solves the issue of production load and guarantee of email delivery by allowing us to configure SMTP server.
How to use it?
For your ease i have created a custom function which of course uses PHPMailer and SMTP. Here is a function.
require_once('class.phpmailer.php');
function sendmail($to,$subject,$message,$name)
{
$mail = new PHPMailer();
$body = $message;
$mail->IsSMTP();
$mail->SMTPAuth = true;
$mail->Host = "smtp.gmail.com";
$mail->Port = 587;
$mail->Username = "youraccount@gmail.com";
$mail->Password = "your gmail password";
$mail->SMTPSecure = 'tls';
$mail->SetFrom('youraccount@gmail.com', 'Your name');
$mail->AddReplyTo("youraccount@gmail.com","Your name");
$mail->Subject = $subject;
$mail->AltBody = "Any message.";
$mail->MsgHTML($body);
$address = $to;
$mail->AddAddress($address, $name);
if(!$mail->Send()) {
return 0;
} else {
return 1;
}
}
?>
To use this you need two files, class.phpmailer.php and class.smtp.php. You can download and copy these two files from link given below.
External link: Download PHPMailer library.
Once download place those two files plus the one contains above function. Here is a link of the file.
Internal link : Download custom phpmailer function.
include("sendmail.php");
$to = "some email";
$subject = "Hello";
$message = "hello how are you.";
$name = "Shahid Shaikh";
$mailsend = sendmail($to,$subject,$message,$name);
if($mailsend==1){
echo '
email sent.
';}
else{
echo '
There are some issue.
';}
?>
Performance study:
Recently i was assigned to one project in my company and in that we were using php mail() function. This function used to take like 30 seconds to send one email. Reason was the heavy production server but obviously this is not what client expects.
So i changed it to PHPMailer and after using the Google app smtp i am able to send email within 3 seconds at top. Reduction of 27 seconds and guaranteed delivery of email.
So when somebody will not be lazy to look at the source of the form for filling. Here you go – my email address and password to get in? Couldn’t you move these sensitive things at least in the other fileslike bla-bla-smtp.php or bla-bla-*mailer.php at least?
thanxxxxxx…
Why is $mail->Host set twice?
Oops. It mistake, thanks for pointing out. Updating it. However code will work because PHP is interpreter and it will take latest value of $host.
Hi, thanks for this tutorial 🙂
What about when you are being contacted from the contact form?
$mail->SetFrom() should contain the email of the internet user, will that still work??
Hi Reda,
I am confused with ” email of internet user ” field ? What you mean by that ?
ps. you are first site where i disabled adblock + 1 click, to earn you some money.
see you.
I appreciate that 🙂
how can i send an email using localhost server by using smtp??
Are you talking about SMTP in your local system ?
this way how do you insert new lines to email just curious?
Use HTML tag
.
Hi.
Thanks for this tutorial.
Can you please tell me how i can make html form & link it with this php mailer code?
I think it’s better to if complete your tutorial with html form example.
Thank you.
Plz can you help me by giving step to step tutorial in building a mailer as a beginner
Plz!!
Thanks for this tutorial.
ne biçim anlatmışsınız kardeşim bi recaptcha uygulamasına bakıyım dedim yarım yamalak anlatılmış bu da öyle eksik !
SMTP Error: Could not connect to SMTP host.
There are some issue.
i m using the filezilla or online server but this error show when i send email using the phpmailer
use require_once(‘PHPMailerAutoload.php’);
replace
” require_once(‘class.phpmailer.php’); ”
with
” require_once(‘PHPMailerAutoload.php’); ”
PHPMailerAutoload.php is in the same library.
With out this you can get SMTP Class error messages.
Additional you can add
” $mail->CharSet = ‘UTF-8’; ”
right after
” $mail = new PHPMailer(); ”
for more İnternational support.
it is not working in ununtu.
it is comming : there are some issue.
please help. Thank You.
it is not woarking in ununtu
send mail function is returning 0
please help . Thank you
Thanks man! Very easy to understand 🙂
If you get the SMTP class error you need to edit code from:
require_once(‘class.phpmailer.php’);
to:
require_once(‘PHPMailerAutoload.php’);
Make sure the path is correct…
What’s the best way to have phpmailer to redirect to another php-page after a success sent message. Before I was using the Sendmail and then I just put i a new header but that’s not possible any longer. I don’t just what to have a white page whit the text “message sent”.
Message could not be sent.Mailer Error: SMTP connect() failed.
I got error like this
I am using XAMPP
If you haven’t solved it yet try setting your debug on
$mail->SMTPDebug = 3;
In this you will the error you are looking for
this code doesn’t work for me:
the value of Port and host they are static ??
if they are not static , so What is the princip of filling this parameters.