Tuesday, January 29, 2008

Marked as spam while sending emails using php mail() function to yahoo or hotmail?

It is a common problem for all using php mail function. To solve this there
are so many answers I have seen in the internet and they do not hit problem
correctly.

Actually the problem here is if we send mails using php mail function we do
not have a signature and other mailing systems thinks that we are spamers.


So the solution is using a free remote smtp host like gmail to send our mails.
It is not hard because we have a free php smtp project called PHPMailer. You
can download it from here.
You do not need to install it on your server. Just you have to upload it with your code.


It is very easy to understand how it is used to send mails using examples
zipped with PHPMailer. The following code is to send emails using gmail and
to do that you have to have a gmail mail account. Which can easily be created
by visiting http://gmail.com. Your mails will
send using that mail account and they will never become spams...




<?php


// example on using PHPMailer with GMAIL


include("class.phpmailer.php");

include("class.smtp.php");


$mail=new PHPMailer();


$mail->IsSMTP();

$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


$mail->Username = "yourname@gmail.com"; // GMAIL username

$mail->Password = "password"; // GMAIL password


$mail->From = "replyto@yourdomain.com";

$mail->FromName = "Webmaster";

$mail->Subject = "This is the subject";

$mail->Body = "Hi,<br>This is the HTML BODY<br>"; //HTML
Body

$mail->AltBody = "This is the body when user views in plain text format";
//Text Body


$mail->WordWrap = 50; // set word wrap


$mail->AddAddress("username@domain.com","First Last");

$mail->AddReplyTo("replyto@yourdomain.com","Webmaster");

$mail->AddAttachment("/path/to/file.zip"); // attachment

$mail->AddAttachment("/path/to/image.jpg", "new.jpg");
// attachment


$mail->IsHTML(true); // send as HTML


if(!$mail->Send()) {

echo "Mailer Error: " . $mail->ErrorInfo;

} else {

echo "Message has been sent";

}