I use phpMailer, but I have a problem: when the recipient is Gmail or Yahoo my email ends up in the spam folder. How is it possible? The code is this:
$mail->AddCustomHeader('Reply-to:[email protected]');
$mail->From="[email protected]";
$mail->FromName="MY NAME";
$mail->IsSendmail();
$mail->AddReplyTo("[email protected]");
$mail->From = '[email protected]';
$mail->FromName = 'My name';
$mail->AddAddress($to, $toName);
$mail->Subject = $subject;
$mail->AltBody = "To view the message, please use an HTML compatible email viewer!";
$mail->WordWrap = 80; // set word wrap
$body->'Hello';
$mail->MsgHTML($body);
$mail->IsHTML(true); // send as HTML
This is a complex question. There are a lot of things that can go wrong, but here's a few things you should think about:
Where you send your email from matters. Don't expect to be able to run this with good results from your home ADSL line, because most mail servers, look to see if the mail comes from netblocks assigned for that sort of use. Most mail coming from such sources is spam. Look up your IP with tools like http://www.anti-abuse.org/multi-rbl-check/ and http://spameatingmonkey.com/lookup.
Behave yourself. It takes a while to build a good reputation as a mail sender, and if you misbehave you'll lose it. If your server is hacked and used to send spam, that won't help much either.
Sign your emails. This is a big topic, but the keywords you need are SPF and DKIM. EDIT: And now also DMARC.
Mark bulk email as bulk (eg Header "Precedence: list")
Provide an unsubscribe mechanism for your users, and advertise it in a "List-Unsubscribe" header.
If possible, Look at what gmail says about the messages marked as spam, within the web interface of the recipient. Look at their "Authentication-Results" header to check that your SPF and DKIM signing is working. Read all the suggestions on google and yahoo's sites about how not to get treated as spam. eg follow the link from on of your messages in a gmail mailbox.
If you don't want to deal with all of this, consider sending your mail through a reputable smtp server. If you're just sending a little bit of email, your hosting provider (or your isp if you're doing this from home) might provide a suitable smtp server, which you can access with suitable authentication. You might even have a small enough volume to send through something like gmail's servers (though they will rewrite your From address to match your gmail account). For commercial volumes you might want to consider using a commercial smtp service provider that specialises in this sort of thing. Besides helping you stay on top of all the things you need to do to get a good delivery rate, they'll also help a lot with tools for tracking how many of your users are actually opening emails and clicking on links, and for removing bad email addresses from your lists.