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";

}



12 comments:

Unknown said...

That's a neat little script you got there. I tried it, and it worked albeit it's a little slow (probably gmail's fault).

Unknown said...

Hi,

I'm getting this error:
Warning: fsockopen() [function.fsockopen]: unable to connect to ssl://smtp.gmail.com:465 (Unable to find the socket transport "ssl" - did you forget to enable it when you configured PHP?) in C:\wamp\www\php_sandbox\class.smtp.php on line 122
Mailer Error: Language string failed to load: connect_host


Can someone help me please!!! Thanks.

Knoll House Interforum said...

I am very skeptical about using code like this. In your example it shows that I have to insert my gmail user name and password. So my question now becomes. If people view my code won't they be able to see my gmail user name and password and by doing so they would be able to hi-jack my entire Google account?

Steve
Knoll House Interforum

Dhanushka said...

As we all know when some one wants to connect MySql server using PHP we should hard code password and user name somewhere in the code. So this is also like that. But you may get this password and user name using a protected database and use it. I recommend that to always create a new gmail account for use with this code.

Anonymous said...

Thanks a lot dude! Great stuff!
It solves a lot of php.mail() problems I'm having lately at once!

Anonymous said...

Sorry, I didn't find a contact form so I'll leave you a comment:

first of all, thanks for the PHP mailer script (I used Gmail for a while too and it works like a charm).
I just wanted to let you know that you have a typo in your page title, because you wrote "the best coRder for anything" - and by the way, advertisements are abbreviated with "ads" ;)

Anonymous said...

hye bro... the code is used for gmail rite! wat about other mail? yahoo and hotmail. different users use different mail.

Anonymous said...

My problem is when receiving, sender's address is Webmaster [mygmailaccount@gmail.com]

not

Webmaster [replyto@mydomain.com]
as desired.

When I reply, the To address is Webmaster < replyto@mydomain.com > but in message body, i have

From: Webmaster [mailto:mygmailaccount@gmail.com]
Sent: Thursday, April 17, 2008 5:04 PM
To: Huy Ho
Subject: This is the subject

Hi,
This is the HTML BODY

Bijay Rungta (rungss) said...
This comment has been removed by a blog administrator.
Unknown said...

I have an error like this. Please help..

Parse error: syntax error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in /home1/kolaydil/public_html/test/class.phpmailer.php on line 53

Dora said...

Hi i have used smtp ,s till one of my mails are going to spam in sify. Can u tell me the reason

http://chandradey.blogspot.com

Dhanushka said...

What is the SMTP server you are using? Is it gmail or any other?