Jump to content

Formatting mails sent with PHP


tmallen

Recommended Posts

When I put a newline (\n) into my strings which are later passed onto the $body variable, I actually see the literal "\n" in the email. The same goes for HTML code. Here's my code, note that the indentation looks off but is logical in my document:

<?php # Mail Script v1.0 by {me}

$to			= 'my email address;
$subject 	= 'Request for more information';
$mailheaders= 'From: Client's name-Website';

// Grab the form and assign to variables

$name		=	$_REQUEST['fName'] .' '. $_REQUEST['lName'];
$email		=	$_REQUEST['email'];
$company	=	$_REQUEST['company'];
$address1	=	$_REQUEST['address1'];
$address2	=	$_REQUEST['address2'];
$city		=	$_REQUEST['city'];
$state		=	$_REQUEST['state'];
$zip		=	$_REQUEST['zipCode'];
$phone		=	$_REQUEST['phone'];
$about		=	$_REQUEST['contactAbout'];

// Send the mail

$body = '<div style="font: 10pt arial; color: #333;">'.
		'<p>Someone has requested information from the website.</p>'.
		'<table border="0" cellpadding="3" cellspacing="0" style="font-size: 10pt;">'.
			'<tr><th align="left">Name:</th>		<td> '.$name.' </td></tr>' . 
			'<tr><th align="left">Email:</th>		<td> '.$email.' </td></tr>'.
			'<tr><th align="left">Company:</th>		<td> '.$company.' </td></tr>'.
			'<tr><th align="left">Address:</th>		<td> '.$address1.' </td></tr>'.
			'<tr><th align="left">Address 2:</th>	<td> '.$address2.' </td></tr>'.
			'<tr><th align="left">City:</th>		<td> '.$city.' </td></tr>'.
			'<tr><th align="left">State:</th>		<td> '. $state.' </td></tr>'.
			'<tr><th align="left">Zip:</th>			<td> '.$zip.' </td></tr>'.
			'<tr><th align="left">Phone:</th>		<td> '.$phone.' </td></tr>'.
			'<tr><th align="left">Regarding:</th>	<td> '.$about.' </td></tr>'.
		'</table>'.
	'</div>';

mail ($to, $subject, $body, $mailheaders);

header("Location: ../thanks.php");

?>

And the results:

<div style="font: 10pt arial; color: #333;"><p>Someone has requested information from the website.</p><table border="0" cellpadding="3" cellspacing="0" style="font-size: 10pt;"><tr><th align="left">Name:</th> <td> my name </td></tr><tr><th align="left">Email:</th> <td> my email </td></tr><tr><th align="left">Company:</th> <td> my company  </td></tr><tr><th align="left">Address:</th> <td>  </td></tr><tr><th align="left">Address 2:</th> <td>  </td></tr><tr><th align="left">City:</th> <td>  </td></tr><tr><th align="left">State:</th> <td> Virginia </td></tr><tr><th align="left">Zip:</th> <td>  </td></tr><tr><th align="left">Phone:</th> <td>  </td></tr><tr><th align="left">Regarding:</th> <td>  </td></tr></table></div>

Why is this? What am I doing wrong here?

Link to comment
https://forums.phpfreaks.com/topic/71720-formatting-mails-sent-with-php/
Share on other sites

in the last parameter of mail() you can specify headers. Here's what I use for HTML email:

 

"Content-Type: text/html; charset=utf-8\r\nContent-Transfer-Encoding: 8bit\r\n\r\n";

 

be careful with headers, though. Don't use any raw site-visitor provided data there, or your mail form could become a spam portal.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.