Send PHP mail using SMTP

Hello Friends !! TOday I found that how PHP mail() function works with SMTP.

Actually when your site is hosted on windows server, then there is SMTP used for sending mail. With simple mail() function ,you can’t send mail in SMTP.

You have to authenticate PHP and SMTP. In short, I explain you that how to send PHP mail using SMTP ?

For sending mail in PHP using SMTP, first of all you have to download phpMailer. phpMailer is a class which used to send mails using SMTP.

You can download phpMailer from here.

<?php

//this is a path to PHP mailer class you have dowloaded

include(“class.phpmailer.php”);

$mail = new PHPMailer();

$mail->IsSMTP(); // set mailer to use SMTP

$mail->Host = “SMTP server name “; // specify main and backup server

$mail->SMTPAuth = true; // turn on SMTP authentication

$mail->Username = “SMTP username “; // SMTP username

$mail->Password = “SMTP password”; // SMTP password

$mail->From = “frm Email “; //do NOT fake header.

$mail->FromName = “MailMan”;

$mail->AddAddress(“to email ID “); // Email on which you want to send mail

$mail->AddReplyTo(“Reply to Email “, “Support and Help”); //optional

$mail->IsHTML(true);

$mail->Subject = “Just a Test”;

$mail->Body = “Hello. I am testing <b>PHP Mailer.</b>”;

if(!$mail->Send())

{

echo $mail->ErrorInfo;

}else{

echo “email was sent”;

}

?>

Please save above code as testmail.php or whatever name you want but make it php.

Please make sure that above script is stored in the folder in whu\ich you store class.phpmailer.php and class.smtp.php. These both class files are required for sending mail.

Thats it !! Now you can send PHP mails using SMTP. Feel free to comment on this post. Your feddback is importand to us.

  • carol

    please i need a php mailer which does not use smtp and can send about 1,000 mails at a time. could you pls send me a link to this to my email. Thank you

  • carol

    please i need a php mailer which does not use smtp and can send about 1,000 mails at a time. could you pls send me a link to this to my email. Thank you

  • Mittulchuahan

    i have done all the steps mentioned here .. but m getting this error saying ..
    ————————–
    SMTP Error: The following recipients failed: support@financiallabz.com
    Message was not sentMailer Error: SMTP Error: The following recipients failed: support@financiallabz.comSMTP server error: No such user here

    ————————–

    please reply me why m getting this error .. what are the solutions for this .. reply me on my email at

    mittul at orangewebtech dot com

    looking 4ward to hear from you soon .,.

    thank you ..

  • Ajeesh

    Hi, i am getting this error.. SMTP Error: Could not authenticate. SMTP Error: Could not authenticate., i think its bcz of the port, in your code you did not mentioned the port.

    please help me to add port on this same code.

    Thank you

  • Ajeesh

    Hi, i am getting this error.. SMTP Error: Could not authenticate. SMTP Error: Could not authenticate., i think its bcz of the port, in your code you did not mentioned the port.

    please help me to add port on this same code.

    Thank youplease mail me if any one knows the solution: ajeesh.aa@gmail.com

  • John Doe

    Thanks, it works good

  • sami

    excellent.very useful

  • Arslan

    i got this warning

    warning: stream_socket_enable_crypto(): this stream does not support SSL/crypto in C:xampphtdocs12workclass.smtp.php on line 197

    plz help me
    sheikh.abm@gmail.com

  • kaarthi

    Hi am getting this error while using gmail smtp. how to solve this help me…

    SMTP Error: Could not connect to SMTP host.
    SMTP Error: Could not connect to SMTP host.

  • diego.pk7

    kkk What a shameless copy and paste SHIT
    I bet that you didn’t even read the code.
    Fool the one who writted, and fool the one who is getting errors and don’t even know why.
    STOP BEING LAZY! Read the code properly. The code itself is a tutorial!