Can't send multiple PHP mail() headers in WordPress

I am using Gravity Forms to handle forms and after the form submission I have coded a simple mail() function to send an email to the user. The headers work fine individually:

$headers = 'From: MyName\r\n';

// or 

$headers .= 'Content-type: text/html; charset=iso-8859-1\r\n';

mail('[email protected]', 'My Subject', 'My Content', $headers);

but together in either order there is an issue"

$headers = 'From: MyName\r\n'; // works fine
$headers .= 'Content-type: text/html; charset=iso-8859-1\r\n'; // In this case the body is not rendered as HTML
// or
$headers = 'Content-type: text/html; charset=iso-8859-1\r\n'; // renders as HTML
$headers .= 'From: MyName\r\n'; // This now gives "unknown sender"

Any ideas?

Upvotes: 0

Views: 87

Answers (1)

rnevius
rnevius

Reputation: 27092

Line breaks need to be double-quoted in order for "\r\n" to be respected. As the headers are currently defined, '\r\n' is being treated as literal text, not a line break.

$headers = "From: MyName\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\r\n"; 

Upvotes: 1

Related Questions