Wrox Programmer Forums
Go Back   Wrox Programmer Forums > PHP/MySQL > Beginning PHP
|
Beginning PHP Beginning-level PHP discussions. More advanced coders should post to the Pro PHP forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Beginning PHP section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old June 23rd, 2005, 04:25 PM
Authorized User
 
Join Date: Dec 2003
Posts: 17
Thanks: 0
Thanked 0 Times in 0 Posts
Default Newline in mail function

I can't get \n or \r\n to work in the mail body on either my Windows localhost or the Linux web server. Here's the meat of the php code:

------------

   $selections='Quiz Results: \r\n';
   $selections.='Birth Month: January \r\n';
   $selections.='Favorite Breed: Schnauzer';

   $mail_to='[email protected]';
   $mail_subject='Orientation Quiz';
   $mail_body=$selections;

   if (@mail($mail_to,$mail_subject,$mail_body))
      echo 'Your quiz results have been submitted.';
   else
      echo '(Sorry, there has been an error...)';

------------

The email goes out just fine, but the message body reads:

"Quiz results: \r\nBirth Month: January \r\nFavorite Breed: Schnauzer"

If I change "\r\n" to "\n" I get the same result with just "\n", and changing it to "<br />" just replaces it with "<br />".

Every book and posting I've found says this should be so simple...

Thanks for your help!
 
Old June 25th, 2005, 01:45 AM
Friend of Wrox
 
Join Date: Mar 2004
Posts: 357
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to anshul
Default

FROM PHP Manual
Quote:
quote:/* To send HTML mail, you can set the Content-type header. */
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";

/* additional headers */
$headers .= "To: Mary <[email protected]>, Kelly <[email protected]>\r\n";
$headers .= "From: Birthday Reminder <[email protected]>\r\n";
$headers .= "Cc: [email protected]\r\n";
$headers .= "Bcc: [email protected]\r\n";

/* and now mail it */
mail($to, $subject, $message, $headers);
 
Old June 25th, 2005, 08:47 AM
Authorized User
 
Join Date: Dec 2003
Posts: 17
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks for the workaround. The problem was in the body, not the headers, and I was trying to avoid sending html, but that at least gives me a way to add line breaks to the email message.
 
Old June 25th, 2005, 11:06 AM
Authorized User
 
Join Date: Dec 2003
Posts: 17
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Well, there's still one question. When I send it through the localhost Windows machine, the html message comes through just fine. However, when I go through the Linux webserver, it comes through as plain text as follows:

------------

Content-type: text/html; charset=iso-8859-1 Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: BASE64

U3RldmUgU21pdGggYXQga2M1ZkBiZWxsc291dGgubmV0IGhhcy B0YWtlbiB0aGUgT3JpZW50YXRp b24gUXVpeiB3aXRoIHRoZSBmb2xsb3dpbmcgcmVzdWx0czo8Yn IgLz48YnIgLz5CaXJ0aCBNb250 aDogSmFudWFyeTxiciAvPkZhdm9yaXRlIEJyZWVkOiBDb2xsaW U8YnIgLz5GYXZvcml0ZSBDaXR5 OiBDaGFybGVzdG9u

------------

The headers include the following:

------------

Return-Path: <[email protected]>
...
Date: 25 Jun 2005 15:57:55 -0000
Message-ID: <[email protected]>
From: [email protected]
To: [email protected]
Subject: Orientation Quiz

------------

Could there be a setting at the webserver I need to ask to have changed?

Tnanks again!
 
Old June 27th, 2005, 06:48 AM
Friend of Wrox
 
Join Date: Mar 2004
Posts: 357
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to anshul
Default

I think you need just to specify HTML MIME in header of e-mail sent as my previous reply. ( When sending as an HTML message, we use <br> for newline. \r\n is for plain text ). It's not problem of server: Linux or Windows.

http://www.mediasworks.org/webstorie...ghost/home.htm
 
Old June 27th, 2005, 08:25 AM
Authorized User
 
Join Date: Dec 2003
Posts: 17
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks, Anshul, but I had cut-and-pasted your reply and used it after changing the appropriate email addresses and adding the $to, $subject and $message fields:

------------

/* To send HTML mail, you can set the Content-type header. */
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";

/* additional headers */
$headers .= "To: Me <[email protected]>\r\n";
$headers .= "From: Me <[email protected]>\r\n";
$headers .= "Cc: [email protected]\r\n";

/* email details */
$to = "[email protected]";
$subject = "PHP Mail Test from manual";
$message = "This is from a message on the p2p forum<br />";
$message .= "This should be the second line<br />";

/* and now mail it */
mail($to, $subject, $message, $headers);

------------

Sent from localhost, the message came through fine:

------------
This is from a message on the p2p forum
This should be the second line
------------

Sent through the webserver, the message came through:

------------
Content-type: text/html; charset=iso-8859-1 To: Me <[email protected]>
From: Me <[email protected]>
Cc: [email protected]
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: BASE64

VGhpcyBpcyBmcm9tIGEgbWVzc2FnZSBvbiB0aGUgcDJwIGZvcn VtPGJyIC8+VGhpcyBzaG91bGQg YmUgdGhlIHNlb25kIGxpbmU8YnIgLz48YnIgLz48aHIgLz4=
------------

However, when I looked at the headers I noticed:
"Message-ID: <[email protected]>"

The PHP manual states, "If you use another mail program, such as qmail or postfix, be sure to use the appropriate sendmail wrappers that come with them," so it is a difference in servers. The web server is apparently using qmail, and I need to start looking at qmail "sendmail wrappers".

Anyway, maybe this will be of use to someone else with the same problem.





Similar Threads
Thread Thread Starter Forum Replies Last Post
Send mail and attachments with PHP mail function Lofa Beginning PHP 1 June 2nd, 2008 03:24 PM
Mail function Rebel BOOK: ASP.NET 2.0 Instant Results ISBN: 978-0-471-74951-6 39 November 14th, 2007 06:00 AM
mail function remo Beginning PHP 0 July 16th, 2007 05:45 AM
mail function sunsetbay Beginning PHP 5 July 3rd, 2005 08:59 PM
mail () function Mark200408 BOOK: Beginning PHP, Apache, MySQL Web Development ISBN: 978-0-7645-5744-6 1 September 30th, 2004 07:59 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.