ThisisForReal Posted August 11, 2010 Share Posted August 11, 2010 I'm working on a project where I have concatenated a string that lists all of the members of a group and want to email them to the group administrator. An example string may look like this: $string = 'Mark Ruiz | class A | January 7, 2010\n\nJason Adams | class B | January 9, 2010.\n\nDan Hernandez | class A | March 4, 2010.'; When I put that into the message of my email: $body = "Hello $admin,\n\nThis is the list of members to your group:\n$string." The \n\n that separates each member in the string is still considered part of the string, so the email doesn't recognize that it should start a new line. Does anybody know a good work around to this problem? Thanks so much! Link to comment https://forums.phpfreaks.com/topic/210480-new-lines-within-strings-n-and-making-them-appear-in-phpmail/ Share on other sites More sharing options...
TOA Posted August 11, 2010 Share Posted August 11, 2010 If you have your server on a microsoft machine, it must include a carriage return (\r\n) to get a new line. Also, I think \r and \n must be used in double quotes, but I may be wrong :-\ Link to comment https://forums.phpfreaks.com/topic/210480-new-lines-within-strings-n-and-making-them-appear-in-phpmail/#findComment-1098260 Share on other sites More sharing options...
ThisisForReal Posted August 11, 2010 Author Share Posted August 11, 2010 So if when concatenating the $string, if I build it with double quotes that will work? I'll give it a try. Link to comment https://forums.phpfreaks.com/topic/210480-new-lines-within-strings-n-and-making-them-appear-in-phpmail/#findComment-1098279 Share on other sites More sharing options...
ThisisForReal Posted August 12, 2010 Author Share Posted August 12, 2010 No luck on a variety of attempts to populate the string with various forms of \n, inside double quotes, single quotes, with \r, without it. So I'll back up a moment and show the bigger picture and see if someone knows a better solution to my dilemma. Objective: Send an email using php's mail() to a group leader that will show all the members of the group. Since I'm sending a bit more information than just the name of each member, formatting the email is KEY. My Approach: The easiest way to format the letter to be legible is to insert a carriage return or new line after each member. To do that I tried the following: list_of_members = array(); foreach($member_first as $key => $value) $list_of_members[$key] = $value . ' ' . $member_last[$key] . ' | ' . $class[$key] . ' | ' . $join[$key]; $members_string = implode('\n\n',$part_line); //I'm trying to put the carriage new line here Now I have one string that contains all the members with the '\n\n' between each member. When inserting this into the body of the email, I thought the contents would create new lines in the email. // Send Email $to = "$email"; $subject = "Members of your group"; $message = "Hello $admin,\n\nThis is the list of members to your group:\n$members_string" . "\n\nRegards,\r\n Team Leader Alpha\n\n"; $headers = 'From: [email protected]' . "\r\n" . 'Reply-To: [email protected]' . "\r\n" . 'X-Mailer: PHP/' . phpversion(); mail($to, $subject, $message, $headers) or die("Email was not sent"); The reality is that while all other carriage returns work, the email shows the member string verbatim without following the direction of the new line command (example follows:): Hello John, This is the list of members to your group: Mark Ruiz | class A | January 7, 2010\n\nJason Adams | class B | January 9, 2010.\n\nDan Hernandez | class A | March 4, 2010. Regards, Team Leader Alpha What other method could I use to get the formatting in the email to allow for a carriage return between each member? Thanks! Link to comment https://forums.phpfreaks.com/topic/210480-new-lines-within-strings-n-and-making-them-appear-in-phpmail/#findComment-1098330 Share on other sites More sharing options...
PFMaBiSmAd Posted August 12, 2010 Share Posted August 12, 2010 $members_string = implode("\n\n",$part_line); Link to comment https://forums.phpfreaks.com/topic/210480-new-lines-within-strings-n-and-making-them-appear-in-phpmail/#findComment-1098332 Share on other sites More sharing options...
ThisisForReal Posted August 12, 2010 Author Share Posted August 12, 2010 $members_string = implode('\n\n',$list_of_members); //I'm trying to put the carriage new line here Ignore that earlier alternate array name. I try to change names before posting to this forum so that things are easier for you all to follow. Missed that one. Link to comment https://forums.phpfreaks.com/topic/210480-new-lines-within-strings-n-and-making-them-appear-in-phpmail/#findComment-1098333 Share on other sites More sharing options...
ThisisForReal Posted August 12, 2010 Author Share Posted August 12, 2010 I found the answer - use chr(13) and chr (10). See code here: $chr = chr(13) . chr(10); $members_string = implode($chr,$list_of_members); Thanks to Maven1 at php.net. Link to comment https://forums.phpfreaks.com/topic/210480-new-lines-within-strings-n-and-making-them-appear-in-phpmail/#findComment-1098365 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.