Hi,
I am having a problem with my PHP mail function. I am trying to send an email from . This code is fine:

mail($to, $subject, $body,
    "From: user@site.com\n" .
    "MIME-Version: 1.0\n" .
    "Content-Type: multipart/alternative;\n" .
    "     boundary=" . $mime_boundary_header) or die('Mail Error');

however this does not

mail($to, $subject, $body,
    "From: Mr. User <user@site.com>\n" .
    "MIME-Version: 1.0\n" .
    "Content-Type: multipart/alternative;\n" .
    "     boundary=" . $mime_boundary_header) or die('Mail Error');

The exact error returned is

[Sat Sep 26 21:08:11 2009] [error] [client 127.0.0.1] PHP Warning:  mail() [<a href='function.mail'>function.mail</a>]: SMTP server response: 501 &lt;&lt;TubeTool&gt; user@site&gt;: missing or malformed local part in [removed for security]\\scripts\\0\\saccount\\signup.php on line 168, referer: [...]

Please note that the &lt; and &gt; are exactly that in the error log not < or >
Regards,
Sam Rudge

Dani AI

Generated

Short expert note: the SMTP 501 "missing or malformed local part" when switching from a bare address to a display-name + address almost always means the mail headers (or the envelope address) are being sent in a format the SMTP server can't parse. Common causes are wrong line endings, HTML-encoded angle brackets, an unquoted display name, or a mismatch between the message From: header and the SMTP envelope sender.

A compact checklist and safe fixes:

  • Make every header line end with CRLF (\r\n) on Windows. ( was right to suggest CRLF.)
  • Quote the display name so the address parser can't be confused: use a header like From: "Mr. User" <user@example.com>.
  • Quote the boundary value if it contains non-alphanumeric chars: boundary="...". ( pointed this out implicitly.)
  • Ensure the header string is not run through htmlspecialchars() / htmlentities() — literal &lt; or &gt; in the header will break parsing (the OP's log showing &lt; is a strong hint of this).
  • On Unix/sendmail hosts set the envelope sender with the 5th mail() parameter (-f...); on Windows set sendmail_from in php.ini (Windows does not honor -f).

Example header construction (use as a template, not copied from previous posts):

$headers  = "From: \"Mr. User\" <user@example.com>\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: multipart/alternative; boundary=\"{$mime_boundary}\"\r\n";
mail($to, $subject, $body, $headers, "-fuser@example.com");

Debug tips: dump the exact header bytes to a log/file before calling mail() (e.g., error_log() or file_put_contents) to confirm there are no HTML entities, extra bytes (BOM), or stray angle brackets. If these checks don't reveal the problem, switch to a tested library that handles headers and envelopes (as suggested) so the SMTP exchange is correct and easier to inspect.

Recommended Answers

All 4 Replies

Perhaps this might be better:

mail($to, $subject, $body,
    "From: Mr. User <user@site.com>\r\n" .
    "MIME-Version: 1.0\r\n" .
    "Content-Type: multipart/alternative;\r\n" .
    "     boundary=" . $mime_boundary_header."\r\n") or die('Mail Error');

Perhaps this might be better:

mail($to, $subject, $body,
    "From: Mr. User <user@site.com>\r\n" .
    "MIME-Version: 1.0\r\n" .
    "Content-Type: multipart/alternative;\r\n" .
    "     boundary=" . $mime_boundary_header."\r\n") or die('Mail Error');

Thanks for the suggestion but same error again :(

mail($to, $subject, $body,
    "From: Mr. User <user@site.com>\r\n" .
    "MIME-Version: 1.0\r\n" .
    "Content-Type: multipart/mixed; boundary=\"----------A4D921C2D10D7DB\"\r\n") or die('Mail Error');
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.