How i can send SMTP mail in php using PHPmailer
How you can send mail through smtp in php using phpmailer ?
For sending mail through SMTP in php first of all we have to download and copy the mailer library in the root directory of our server. We can download mailer library from https://gitlab.com/24May/PHPMailer/tree/phpmailer-5.2.0 . For email through SMTP we will mainly need two files one is class.mailer.php and another is class.smtp.php . By using both files we can send SMTP mail. If we want to write mail in diffrent languages then we will use the language module of phpmailer i.e we want to write email in english then we will use $mail->SetLanguage( ‘en’, ‘phpmailer/language/’ ); in our code, Like this we can write mail in any language.
After copy the both php files we will use the following php code for sending mail through SMTP :-
header('content-type: application/json; charset=utf-8');
$mail = new PHPMailer;
//Enable SMTP debugging.
$mail->SMTPDebug = 1;
//Set PHPMailer to use SMTP.
//Set SMTP host name
$mail->Host = "smtp.gmail.com"; //Here you can also write host name of your SMTP server provided by your Hosting server.
//Set this to true if SMTP host requires authentication to send email
$mail->SMTPAuth = true;
//Provide username and password
$mail->Username = 'Your email address';
$mail->Password = 'Your email address password';
//If SMTP requires TLS encryption then set it
$mail->SMTPSecure = "ssl";
//Set TCP port to connect to
$mail->Port = 465;
$mail->From = "Your email address";
$mail->FromName = "Your Name";
$mail->AddAddress("Email address of receiver", "Name of receiver");
$mail->Subject = "Subject Text";
$mail->Body = "<i>Enter your html message.</i>";
$mail->AltBody = "Your device is not supporting HTML.";
echo "Mailer Error: " . $mail->ErrorInfo;
echo " your email has been sent successfully";
Formatting mail correctly is surprisingly difficult. There are myriad overlapping RFCs, requiring tight adherence to horribly complicated formatting and encoding rules – the vast majority of code that you’ll find online that uses the
mail() function directly is just plain wrong! Please don’t be tempted to do it yourself – if you don’t use PHPMailer, there are many other excellent libraries that you should look at before rolling your own – try SwiftMailer, Zend_Mail, eZcomponents etc.