tmallen Posted October 3, 2007 Share Posted October 3, 2007 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? Quote Link to comment https://forums.phpfreaks.com/topic/71720-formatting-mails-sent-with-php/ Share on other sites More sharing options...
BlueSkyIS Posted October 3, 2007 Share Posted October 3, 2007 maybe because you're using single quotes which pass the literal \n instead of a line break. i would replace all single quotes with double quotes. in $body, that is. Quote Link to comment https://forums.phpfreaks.com/topic/71720-formatting-mails-sent-with-php/#findComment-361119 Share on other sites More sharing options...
michaellunsford Posted October 3, 2007 Share Posted October 3, 2007 \r \n \t (and a few others, I believe) must be inclued in double quotes, not single ones. so... echo '<img src="image.jpg" alt="">\n'; does not work, but echo '<img src="image.jpg" alt="">'."\n"; does work Quote Link to comment https://forums.phpfreaks.com/topic/71720-formatting-mails-sent-with-php/#findComment-361120 Share on other sites More sharing options...
tmallen Posted October 3, 2007 Author Share Posted October 3, 2007 OK, thanks. I feel silly for that one. What about formatting with HTML? Edit: I tried adding <html> and <body> tags (I closed them too) but that didn't fix things. Quote Link to comment https://forums.phpfreaks.com/topic/71720-formatting-mails-sent-with-php/#findComment-361122 Share on other sites More sharing options...
BlueSkyIS Posted October 3, 2007 Share Posted October 3, 2007 What about formatting with HTML? Do you mean why is the mail displaying HTML code instead of formatting the content? You need to send appropriate content headers to let the email application know that part of the content is HTML (and which part). Quote Link to comment https://forums.phpfreaks.com/topic/71720-formatting-mails-sent-with-php/#findComment-361126 Share on other sites More sharing options...
tmallen Posted October 3, 2007 Author Share Posted October 3, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/71720-formatting-mails-sent-with-php/#findComment-361128 Share on other sites More sharing options...
BlueSkyIS Posted October 3, 2007 Share Posted October 3, 2007 i use a pre-written class htmlMimeMail or something. i found this via google: http://www.phpguru.org/static/mime.mail.html Quote Link to comment https://forums.phpfreaks.com/topic/71720-formatting-mails-sent-with-php/#findComment-361143 Share on other sites More sharing options...
michaellunsford Posted October 5, 2007 Share Posted October 5, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/71720-formatting-mails-sent-with-php/#findComment-362515 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.