Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php mail() -> junk, sometimes not even received?

Tags:

php

email

I am using phps mail() function to send confirmation e-mails.

The e-mails get received on gmail and others as far as I'm aware, the only real problems I've had is when sending the e-mails to a Hotmail or Yahoo account.

I have read on the internet and many people suggest PHPmailer

What do you guys suggest to do when sending php mail()? This website I'm creating is for a legitimate business and I need mail to be sent and received no matter whom I send it to.

Below is my code currently, I've used specific headers to try solve the problems. I own my own dedicated server and I'm not blacklisted on any spam lists, all I can think of is it is php itself.

$to = $email;
$subject = "Subject";
$body = "Message";
$headers .= "From: Accounts <[email protected]>\r\n";
$headers .= "Reply-To: <[email protected]>\r\n";
$headers .= "X-Sender: <[email protected]>\r\n";
$headers .= 'X-Mailer: PHP/' . phpversion() . '\r\n'; //mailer
$headers .= "X-Priority: 3\r\n"; //1 UrgentMessage, 3 Normal
$headers .= "Return-Path: <[email protected]>\r\n";
$headers .= "MIME-Version: 1.0\r\n"; 
mail($to, $subject, $body, $headers);

Any help is greatly appreciated.

Regards.

like image 505
Latox Avatar asked Feb 25 '23 16:02

Latox


1 Answers

Using PHPMailer, or swiftmailer or any other library won't increase your delivery rates in most cases: the most likely culprit is your server set up.

Sending email successfully is notoriously difficult, have a read here: http://www.codinghorror.com/blog/2010/04/so-youd-like-to-send-some-email-through-code.html for a basic overview of what's necessary.

Yahoo in particular really wants DKIM set up, and if your ISP hasn't done this for you, or you are on a VPS, can be a real pain.

Sucessfully sending email takes quite a bit more then simply not being on blacklists. The default of many ISPs it so assume you are SPAM until you have things set up that would make them think otherwise. Guilty until proven innocent.

In the codinghorror post, take note of this passage:

Port25 offers a really nifty public service -- you can send email to [email protected] and it will reply to the from: address with an extensive diagnostic!

I had to use an alternate address for the test to work; [email protected]. You can read the details of why to test with each address here: http://www.port25.com/domainkeys/

In the long run, if you're sending transactional email (ie. not mass mailings but email in response to a user action) I recommend a service like postmark (postmarkapp.com) because at 1/10th of a cent per email, you get all the ugly taken care of for you.

like image 119
Erik Avatar answered Mar 05 '23 18:03

Erik