wantTheBest
wantTheBest

Reputation: 1720

PHP mail() fails if the subject doesn't contain "Subject: "

If I send mail using the PHP mail() function, the mail will not go through unless I prepend the word "Subject: " to the subject line.

Here's the code:

 $from = "[email protected]";
 $subject = urldecode($_GET['theSubj']) . "\r\n";
 $body =  "Here ya go now\r\n";
 $Header = "From: $from \r\n";

 $retval = mail($advertisersEmail, $subject, $body, $Header);

EDIT: the 'retval' is always "1" even when the recipient doesn't get the email.

I even tried this:

 $Header .= "Subject: $subject";

None of the above sends an email successfully to the recipient.

I've tried a lot of permutations on the above code: removing all the "\r", removing all the "\r\n", not using urldecode().

The only way I can get an email sent is if I change the "subject" to this:

 $subject = "Subject: " . urldecode($_GET['theSubj']) . "\r\n";

The problem with this is, the word "Subject: " is showing up in the recipient's email they receive from our site and I don't want that. I've checked the PHP manual for mail and it says nothing about needing to add "Subject:" to the passed-in subject in the 2nd parameter to mail().

What am I missing here?

Upvotes: 0

Views: 56

Answers (1)

Erik Terwan
Erik Terwan

Reputation: 2780

It looks like the mail address is not in quotes, try fixing that first.

Are you on a shared hosting or is this a private server? If it's a shared hosting, try contacting them. I have send a lot of mails via PHP and never endured this problem.

If all is not working, try sending your mail via SMTP. You can use libraries like PHPMailer to make your life easier.

Upvotes: 1

Related Questions