Email sending requirements is very usual for most of the web applications. Also, PHP give us a very flexibility to send email, a simple mail() function is enough for sending email, where php uses its local server to send email.
But, often a big problem arise that, mails are sent to spam/junk instead of inbox and sometimes even don't reach.
The problem is the server that the email are sending from. Many servers/hosts are one another's black lists why they don't accept email in inbox from those sites. even, if you r sending from a valid host & using "From" attribute to a email domain that's not valid, even then this problem will arise.
The best way is to send emails using smtp servers. Also, sometimes attachments,ssl security, pop3 access to get emails etc are required.
To reduce the coding complexity, a small email library have been developed that is named & known as "phpmailer". You can download from Sourceforge.net web site along with library files & documentations. The documentations are very easy to understand & a lot of examples are also exist. You can also make a custom bulk email sending solution application using this library.
Here is an example code that uses phpmailer class gmail smtp service to send email. For this code to execute you need to have two class files, class.phpmailer.php & class.smtp.php:
include("class.phpmailer.php");
include("class.smtp.php");
$mail = new PHPMailer();
$body = $mail->getFile('templatefile.tpl');
$mail->IsSMTP();
$mail->SMTPAuth = true;
$mail->SMTPSecure = "ssl";
$mail->Host = "smtp.gmail.com";
$mail->Port = 465;
$mail->Username = "yourusername@gmail.com";
$mail->Password = "yourpassword";
$mail->AddReplyTo("yourusername@gmail.com","Name");
$mail->From = "name@yourdomain.com";
$mail->FromName = "First Last";
$mail->Subject = "PHPMailer Test Subject via gmail";
$mail->WordWrap = 50; // set word wrap
$mail->MsgHTML($body);
$mail->AddAddress("whoto@otherdomain.com", "Send to Name");
$mail->IsHTML(true);
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}
Notice that, with the $mail->AddAddress() function, you can add many email adresses to send same email at once.
Subscribe to:
Post Comments (Atom)










1 comments:
I just got a copy of http://ePostMailer.com and I would recommend to anyone who needs to send out an opt-in email mailshot. Its the best free desktop based email marketing software I have used so far.
Post a Comment