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
Share on other sites

OK. And how do I declare a header that makes $body HTML code? This is over my head.

 

Note: I'm all for learning, so if you can point me to a relevant link that teaches the specifics of this use of header(Content-type...), that would be just as good.

Link to comment
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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.