0

I have this code for sending mail in PHP:

$headers = "Content-type: text/html;\r\n";
$headers .= 'From: "Registration" <registration@site.com>' . "\r\n";
$headers .= "Reply-To: registration@site.com\r\n";
$headers .= "Return-Path: registration@site.com\r\n";

$mailresult = mail($to, $subject, $message, $headers, '-f registration@site.com');

The mail sends, the from address is set correctly, and it's working almost perfectly.

The problem is that the mail includes the From:, Reply-To:, and Return-Path: fields in the body of the mail. Here is a snapshot of what it looks like in my Gmail interface:

email_example

Here is what the raw headers look like:

X-PHP-Originating-Script: 33:mailtest.php
Content-type: text/html;
Message-Id: <20130316153738.AA739118073@server.com>
Date: Sat, 16 Mar 2013 15:37:38 +0000 (UTC)
From: registration@site.com

From: Registration <registration@site.com>

Reply-To: registration@site.com

Return-Path: registration@site.com

<html>

The first From: seems to be within the actual headers. The second From:, and everything after that down to to <html> is in the body.

How do I get these header fields out of the email body and into the header where they belong?

Questioner
  • 7,133
  • 16
  • 61
  • 94
  • Try to test by removing `, '-f registration@site.com'` and see what result that gives you. Removing the semi-colon from the other suggestion could be put back the way it was. – Funk Forty Niner Mar 16 '13 at 16:05
  • To add, if possible to post your full code. The problem may lie elsewhere. – Funk Forty Niner Mar 16 '13 at 16:11
  • You're welcome. From another post I noticed, they suggested a `double-quote` - checking all options is worth a shot. Here's the link: http://stackoverflow.com/questions/2014081/problem-with-php-mail-from-header - might be answers there for you. Good luck, sorry I couldn't be more help. – Funk Forty Niner Mar 16 '13 at 16:25
  • I used your `$headers` in one of my email scripts in order to try and replicate the problem and came up empty. I'm starting to think that the problem may not lie in your headers, but elsewhere in your form and/or the process itself. You may have something that is `concatenating` somewhere. – Funk Forty Niner Mar 16 '13 at 16:57

2 Answers2

1

http://php.net/manual/en/function.mail.php

additional_headers (optional)
[...]
Note:
If messages are not received, try using a LF (\n) only. Some Unix 
mail transfer agents  (most notably qmail) replace LF by CRLF 
automatically (which leads to doubling CR if CRLF is used). This 
should be a last resort, as it does not comply with RFC 2822. 
AnFi
  • 10,493
  • 3
  • 23
  • 47
  • This turned out to be my issue. It wasn't stopping my mail from being sent, but it was making it so that only the first custom header (before the first `\r\n`) was being accepted. I have contacted my server admin to see if it can be adjusted to be more standard compliant. – Questioner Mar 17 '13 at 00:32
  • The mail server is Postfix. However, after speaking with my server admin, it seems the issue stems from a custom PHP configuration script he included in the mail routing process, and so it is unlikely to affect other users of Postfix. – Questioner Mar 18 '13 at 00:33
0

Remove the semicolon in the first header (text/html).

silkfire
  • 24,585
  • 15
  • 82
  • 105
  • Thank you for the suggestion, but I just tested this now, and it did not change any results. – Questioner Mar 16 '13 at 15:54
  • @Fred: I changed `"Content-type: text/html;\r\n";` to `"Content-type: text/html\r\n";`. It did not have any effect. – Questioner Mar 16 '13 at 16:00
  • From what I found at http://stackoverflow.com/questions/11353221/php-mail-f-parameter-is-not-working - mentions that `Overriding the bounce address with -f is not allowed on all servers...` may or may not be the problem but worth looking into. – Funk Forty Niner Mar 16 '13 at 16:21