Jump to content

new lines within strings \n and making them appear in phpmail()


ThisisForReal

Recommended Posts

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

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: noreply@mysite.net' . "\r\n" .
	    'Reply-To: noreply@mysite.net' . "\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
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.