PHP has several method to send the mail and sending the mail using SMTP settings is one of them.
Before some days I have posted one article which shows how to send the mail using SMTP. In this post I will going to explain Send Mail using PHPMailer with SMTP settings.
Earlier post was about send mail using SMTP with PEAR::Mail package.
Let’s see how you can send mail using SMPT with PHPMailer:
[cc lang=”php”]
# Include PHP Mailer Class
require_once(“class.phpmailer.php”);
# Create object of PHPMailer
$mail = new PHPMailer();
// Inform class to use SMTP
$mail->IsSMTP();
// Enable this for Testing
$mail->SMTPDebug = 2;
// Enable SMTP Authentication
$mail->SMTPAuth = true;
// Host of the SMTP Server
$mail->Host = “host.smtpserver.com”;
// Port of the SMTP Server
$mail->Port = 25;
// SMTP User Name
$mail->Username = “[email protected]”;
// SMTP User Password
$mail->Password = “user_pass”;
// Set From Email Address
$mail->SetFrom(“[email protected]”, “From Name”);
// Add Subject
$mail->Subject = “PHPMailer SMTP Testing”;
// Add the body for mail
$body = “This is the mail body”;
$mail->MsgHTML($body);
// Add To Address
$to “[email protected]”;
$mail->AddAddress($to, “SMTP Test”);
// Finally Send the Mail
if(!$mail->Send())
{
echo “Mailer Error: ” . $mail->ErrorInfo;
}
else
{
echo “Message sent Successfully!”;
}
[/cc]
As I have used the both [code]PEAR::Mail[/code] and [code]PHPMailer[/code], and I found that [code]PHPMailer[/code] is very easy to learn and use.
You can download PHPMailer from here.
[…] I have posted about the send mail using SMTP with PHPMailer. Before this I have posted about the send mail using SMTP with core PHP. For sending the mails […]
[…] Send mail via SMTP using phpmailer (0 visite) […]
…or you could try SMTP4PHP ;)
Hello,
your’s emails when are send to hotmail, they go to the spam?
Thanks
ok will look into it…
Ok thanks, and post the solution =D
no solution?
Passing from very busy schedule, not found time for the same.
is it possible with youe code to set unsubscribe link in the mail code .and when reciever click on that link then he gets unsubcribed.i have problem with your code when i used them for unsubscribe link.plz help me ..
It’s totally dependent on your project to how you manage your subscription. Above code is just to send the email..
[cc lang=”php”]
ini_set(‘max_execution_time’, 300);
/*echo $_SESSION[“Fromemail”];
echo “”;
echo $_SESSION[“pwd”];
echo “”;*/
$date=date(“Y-m-d”);
include_once(“class.php”);
$c = new connection();
$c->connect();
include(‘PHPMailer_v5.1/class.phpmailer.php’);
$mail = new PHPMailer();
$mail->IsSMTP(); // telling the class to use SMTP
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->SMTPSecure = “ssl”; // sets the prefix to the servier
$mail->Host = “smtp.gmail.com”; // sets GMAIL as the SMTP server
$mail->Port = 465; // set the SMTP port for the GMAIL server
$mail->Username = $_SESSION[“Fromemail”]; // GMAIL username
$mail->Password = $_SESSION[“pwd”]; // GMAIL password
for($i=0; $iSetFrom($_SESSION[“Fromemail”]);
$mail->AddReplyTo($_SESSION[“Fromemail”]);
$mail->Subject = “Do not Miss it,You have great Offer at Pricepointshop”;
$mail->AltBody = “Mail contents”;
$mail->MsgHTML($_SESSION[“Messege”].”If you would like to stop receiving these emails please Click Here Unsubscribe“);
$to = $_REQUEST[“checkbox”][$i];
$mail->AddAddress($to);
}
if(!$mail->Send())
{
echo “Mailer Error: ” . $mail->ErrorInfo;
}
else
{
echo “Message sent Successfully!”;
}
[/cc]
this is my code when reciever clicks on unsuscribe click then the last person to whom mail sent wil get unsuscribe .this happens with all recivers.your help will be appreciated.thank u
At first look you Mail sending code (below), needs to be inside the for Loop.
[cc lang=”php”]
if(!$mail->Send())
{
echo “Mailer Error: ” . $mail->ErrorInfo;
}
else
{
echo “Message sent Successfully!”;
}
[/cc]
Send mail by using PHPMailer via smtp, basic with authentication and attachments
Many of times php mail function does not send mail from our website to customer that time
we use an other option, SMTP (Simple Mail Transfer Protocol) for sending mail. It is very easy to configure and integrate with our site.
http://codelibrary.googleplus.co.in/send-mail-by-using-phpmailer-via-smtp-basic-with-authentication-and-attachments/