In this tutorial, we are going to learn how to Send PHP Email using SMTP service. Some of the companies doesn't provide the php mail function functionality for sending mail. In the Beginning of your project it may work fine but any time it stopped working and customer or client may complaint you about the sending mail problem. Usually Engineer's or developers uses mail function like: -
mail('example@gmail(dot)com', 'Your Subject', 'Header', 'From Email id');
But it doesn't work for a long time or may be deprecated later. So we should Avoid this function. In My project we were also using same mail function mentioned above. but after complaining from the client side. we used the PHP SMTP service or SMTP mail.
Sending mail using SMTP service is quite simple. you just need to create one web mail Email ID for using SMTP Service. if you have mentioned correct Email-id created within web mail of any hosting then your SMTP server email will work correctly without any failure notice. This SMTP service is totally free because its used within your hosting package that is free with your domain name.
In your PHP page you just have to include on class file for sending SMTP mail service using SMTP Mail Server setting.
You need to download PHP mailer class file from the following URL: - Class.phpmailer.php
Just include one PHP class page using Require function like: -
require_once('class.phpmailer.php');
Now add the following page script that is using the SMTP class file for inheriting Simple Mail Transfer Protocol (SMTP)class.
Note Here: - If you want to send same mail to multiple Email ID's then you have to use an array for multiple Email ID for sending mail.
Please Subscribe us with your Email ID for more Coding Stuff or like us on Facebook Page.
mail('example@gmail(dot)com', 'Your Subject', 'Header', 'From Email id');
But it doesn't work for a long time or may be deprecated later. So we should Avoid this function. In My project we were also using same mail function mentioned above. but after complaining from the client side. we used the PHP SMTP service or SMTP mail.
Sending mail using SMTP service is quite simple. you just need to create one web mail Email ID for using SMTP Service. if you have mentioned correct Email-id created within web mail of any hosting then your SMTP server email will work correctly without any failure notice. This SMTP service is totally free because its used within your hosting package that is free with your domain name.
In your PHP page you just have to include on class file for sending SMTP mail service using SMTP Mail Server setting.
You need to download PHP mailer class file from the following URL: - Class.phpmailer.php
Just include one PHP class page using Require function like: -
require_once('class.phpmailer.php');
Now add the following page script that is using the SMTP class file for inheriting Simple Mail Transfer Protocol (SMTP)class.
IsSMTP(); // Its asking for using the class for SMTP $smtp_mail_service->Host = "mail.YourDomainName.com"; // SMTP Server Host Name $smtp_mail_service->SMTPDebug = 2; // enables SMTP debug information (for testing) 1 for error and message 2 for message only $smtp_mail_service->SMTPAuth = true; // enable SMTP authentication $smtp_mail_service->Port = 587; $smtp_mail_service->Username = "info@YourDomainName.com"; // SMTP Username or account name $smtp_mail_service->Password = "your EmailID password comes here"; // SMTP Email Password $smtp_mail_service->SetFrom('info@YourDomainName.com', 'Good Sales Guys'); $smtp_mail_service->AddReplyTo("info@YourDomainName.com","Good Sales Guys"); $smtp_mail_service->Subject = $subject; $smtp_mail_service->AltBody = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test $smtp_mail_service->MsgHTML($body); $name = $auth_person_name; $smtp_mail_service->AddAddress($email, $name); $smtp_mail_service->AddAttachment("examples/images/phpmailer.gif"); //your fisrt attachment path here $smtp_mail_service->AddAttachment("examples/images/phpmailer_mini.gif"); // your second attachment path here if(!$smtp_mail_service->Send()) { echo "Mailer Error: " . $mail->ErrorInfo; } else { echo "Message sent!"; } } ?>In this SMTP Email service, SMTP Address is used for sending mail to recipient Email ID. Using this SMTP service you can also send or attach your Multiple Document of any type.
Note Here: - If you want to send same mail to multiple Email ID's then you have to use an array for multiple Email ID for sending mail.
Please Subscribe us with your Email ID for more Coding Stuff or like us on Facebook Page.
0 comments :
Post a Comment