I'm using the following to send an email:
<php ....
$message = 'Hi '.$fname.', \r\n Your entries for the week of '
.$weekof.' have been reviewed. \r\n Please login and View Weekly reports to see the report and comments. \r\n Thanks, \r\n'.$myname;
mail($to, $subject, $message, $from);
?>
When the message is received it does not start a new line at the "\r\n" but just prints them as part of the message.
I only tried it in Thunderbird 3, not any other clients.
Try to change your ' to " - php interprets a string inside single quotes as literals, whereas with quotes (") it will expand the \r\n to what you want.
More information: http://www.php.net/manual/en/language.types.string.php#language.types.string.syntax.single
Not an answer to the question, but may be of help to someone.
Send a HTML message, and instead of \n, use <BR>.
And use <PRE></PRE> or CSS for preformatted text, thus translating \n to actual new lines.
$headerFields = array(
"From: who#are.you",
"MIME-Version: 1.0",
"Content-Type: text/html;charset=utf-8"
);
mail($to, $subj, $msg, implode("\r\n", $headerFields));
<?php ....
$message = "Hi ".$fname.", \r\n Your entries for the week of "
.$weekof." have been reviewed. \r\n Please login and View Weekly reports to see the report and comments. \r\n Thanks, \r\n".$myname;
mail($to, $subject, $message, $from);
?>
Related
I am sending emails to sms using php. I am using #mms.att.net instead of #txt.att.net as it allows me to control the name who the text message is from. #txt.att.net uses a random number that cant be changed. Everything sends great however I am having issues getting line breaks to display how I want. I have tried multiple combinations such as \r\n \n\r \r \n however am having no luck. They all return the same result except \n\r which double spaces. It doesn't help to send the message in text/html as the phones sms program wont interpret <br> and shows as chars in the body if I was to use it. I have tried. So I am sending in text/plain The result I am getting is as follows
Tim Smith
Time: 6PM
Location: Somewhere, USA
Phone: 111-111-1111
Status: Open
And what I am trying to achieve is for it to look like this with no spaces between each line.
Tim Smith
Time: 6PM
Location: Somewhere, USA
Phone: 111-111-1111
Status: Open
My code for PHP mail() is
$txt = "Test". "\n".
"$firstname $lastname\r\n".
"Time: $JobTime\r\n".
"Location: $City, $State\r\n".
"Phone: $Phone\r\n".
"Status: $JobStatus";
$to = "1111111111#mms.att.net";
$subject = "Test Message";
$txt;
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/plain;charset=UTF-8" . "\r\n";
$headers .= "From: Test<test#example.com>" . "\r\n";
mail($to,$subject,$txt,$headers,"-f test#example.com");
Might there be a different method to achieve what I want?
I'd like to send email using mail function.
Want to add line breaks as follows.
$msg = 'aaaaa'.'\r\n'.'bbbbbb'.'\r\n';
$headers .= "Content-Type: text/plain; charset=\"utf-8\"\r\n";
$to = 'test#test.com';
mail($to,'Subject',$msg,$headers);
But not working.
How can I send email as follows.
aaaaa
bbbbbb
New line is "\n", not '\n' (double quotes instead of single-quotes).
$msg = 'aaaaa'."\n".'bbbbbb'."\n";
Explanation is here:
http://php.net/manual/en/language.types.string.php#language.types.string.syntax.double
If the string is enclosed in double-quotes ("), PHP will interpret more escape sequences for special characters
To add line break you need to add "\n", not '\n'
You may use html tags in your mail body like this(<br /> breaks to new-line) :
$message = '<html><body>';
$message .= '<h1>Hello, World!</h1><p>Hello<br />This is in a newline</p>';
$message .= '</body></html>';
If you want to send plain text then you may just use \n.
In your case :
$msg = "aaaaa \r\n bbbbbb \r\n";
And your headers :
$headers = "From: Name <info#name.com> \r\n";
$headers .= "MIME-Version: 1.0 \r\n";
$headers .= "Content-Type: text/html; charset=UTF-8 \r\n";
Note: In case of single quoted string,
To specify a literal backslash, double it (\). All other instances of
backslash will be treated as a literal backslash: this means that the
other escape sequences you might be used to, such as \r or \n, will be
output literally as specified rather than having any special meaning.
I'm not sure what I am doing wrong here. In the emails, \r\n keeps showing every time there is a line break. What do I need to modify in this code to fix it.
public function sendSupportEmail($email, $name, $comments)
{
//Wait until Google Apps are configured to accept from this domain
//$to = "test#mail.com";
$to = "test#mail.com.com";
$subject = "Support: Support Inquiry";
//Headers
// To send HTML mail, you can set the Content-type header.
$autoHeaders = "MIME-Version: 1.0\r\n";
$autoHeaders .= "Content-type: text/html; charset=iso-88591\r\n";
$autoHeaders .= "From: Web Bot";
$autoHeaders .= "<webbot#mail.com>\r\n";
$autoHeaders .= "Reply-To: webbot#mail.com\r\n";
$autoHeaders .= "Return-Path: webbot#mail.com\r\n";
$autoHeaders .= "X-Mailer: PHP 5.x\r\n";
//Print the local date
$date = new DateTime('now', new DateTimeZone('America/Denver'));
$datePrint = $date->format('F j, Y, g:i a');
//Create Text Based Message Below
$message = "<h3>Support Inquiry sent on {$datePrint}</h3>";
$message .= "<b>Name:</b><br>{$name}<br><br>";
$message .= "<b>Email:</b><br><a href='mailto:{$email}'>{$name}</a><br><br>";
$message .= "<b>Comments:</b><p>{$comments}</p>";
//Send them the E-Mail
return mail($to, $subject, $message, $autoHeaders);
}
I had same issue, turns out using mysqli_real_escape_string was causing it.
For the headers ($autoHeaders), \r\n is the correct way to separate various fields. But the $comments itself needs to use the HTML <br>-tag to denote a line-break, because you're sending a HTML e-mail (see Content-Type).
You can use the nl2br function for that:
$message = "<h3>Support Inquiry sent on {$datePrint}</h3>";
$message .= "<b>Name:</b><br>{$name}<br><br>";
$message .= "<b>Email:</b><br><a href='mailto:{$email}'>{$name}</a><br><br>";
$message .= "<b>Comments:</b><p>{" . nl2br($comments) . "}</p>";
return mail($to, $subject, $message, $autoHeaders);
In addition you can use htmlentities() on $comments before nl2br to convert other characters to HTML entities (like € for € f.e.):
$message = "<h3>Support Inquiry sent on {$datePrint}</h3>";
$message .= "<b>Name:</b><br>{$name}<br><br>";
$message .= "<b>Email:</b><br><a href='mailto:{$email}'>{$name}</a><br><br>";
$message .= "<b>Comments:</b><p>{" . nl2br(htmlentities($comments)) . "}</p>";
return mail($to, $subject, $message, $autoHeaders);
See Ideone sample
\n\r codes are going to be invisible in an Email client, regardless of whether or not you are rendering HTML or Text-based email. Look at, for instance, the Source Code of this website, and that of Google. Anything that is on a new line, technically, has a \n\r at the end.
\n\r says, to the text renderer "Line-Feed, Carriage Return," which harks back to terminals, and essentially says "Cursor down, Return cursor to start of line," much like on a typewriter.
In straight Text documents, this behaves as you would expect. However, as HTML is a markup-language, there are language codes that do this instead, and inner line-feeds have no effect.
I say all of the above to point out one specific thing: If there are ACTUAL line-feeds and carriage returns in your HTML document, you would not see them, as they are not the actual text '\n\r', \n and \r are textual representations of control characters.
So, all of that said, why would you see these? If you actually had the text '\n\r' in the document, and not the control characters. This can happen a few ways, most notably, misunderstanding the difference between " and '. " interprets the text inside of it, expanding control characters, variable references, etc, while ' does not:
$foo = 'bar';
echo "$foo"; // 'bar'
echo '$foo'; // '$foo'
echo "\n"; // actual line-feed
echo '\n'; // the text '\n'
See here for an example.
It is my guess that the actual contents of $comments contains the actual text '\n\r' and not the Line-feed + Carriage Return control characters. Either the user is actually entering the characters '\n\r' or you are inserting them somewhere. Look in your code and be sure you are using double-quotes anywhere you are trying to use control characters.
While I suggest cleaning your code to ensure that you are not inserting the text "\n\r" yourself, if it is actually the USER inserting these characters, you can clean them using this method.
Using a AS3 aplication, i'm sending the body variable via GET.
$body = $_GET['body'];
Here i'm trying to replace the word 'JUMP' with '\n' to break a line
$body = preg_replace('/JUMP/', '\n', $body);
And here i'm trying to send an email with the lines break.
mail($to, $subject, $body, $from)
But it's not working,
Original message:
Hi! JUMP I'm testing.
E-mail:
Hi! \n I'm testing.
What I need:
Hi!
I'm testing.
can anyone help me?
\n only works when using double quotes around it otherwise it will be printed as defined.
$body = preg_replace('/JUMP/', PHP_EOL, $body);
Predefined Constant: PHP_EOL
Change:
$body = preg_replace('/JUMP/', '\n', $body);
to
$body = preg_replace('/JUMP/', "\n", $body);
I am using PHP mail() function:
$to = 'AAAA <postmaster#xxx.xx>';
$subject = 'BBBB';
$message = "CCCC\r\nCCCC CCCC \r CCC \n CCC \r\n CCC \n\r CCCC";
$headers = 'From: DDD<postmaster#xxx.xx>' . "\r\n";
$headers .= "Content-Type: text/html; charset=\"UTF-8\"; format=flowed \r\n";
$headers .= "Mime-Version: 1.0 \r\n";
$headers .= "Content-Transfer-Encoding: quoted-printable \r\n";
mail($to, $subject, $message, $headers);
When I receive this email it looks like this:
CCCC CCCC CCCC CCC CCC CCC CCCC
I would expect something like this:
CCCC
CCCC CCCC CCC
CCC
CCC
CCCC
It works fine without Content-Type HTTP header. How can I make new lines and still use my "Content-Type" declaration?
You need to use a <br> because your Content-Type is text/html.
It works without the Content-Type header because then your e-mail will be interpreted as plain text. If you really want to use \n you should use Content-Type: text/plain but then you'll lose any markup.
Also check out similar question here.
If you are sending HTML email then use <BR> (or <BR />, or </BR>) as stated.
If you are sending a plain text email then use %0D%0A
\r = %0D (Ctrl+M = carriage return)
\n = %0A (Ctrl+A = line feed)
If you have an email link in your email,
EG
Send email
Then use %250D%250A
%25 = %
You need to use <br> instead of \r\n . For this you can use built in function call nl2br
So your code should be like this
$message = nl2br("CCCC\r\nCCCC CCCC \r CCC \n CCC \r\n CCC \n\r CCCC");
If you use content-type: text/html you need to put a <br> because your message will be threated like an html file.
But if you change your content-type to text/plain instead of text/html you will be able to use \r\n characters.
Using
<BR>
is not allways enough. MS Outlook 2007 will ignore this if you dont tell outlook that it is a selfclosing html tag by using
<BR />
You can add new line character in text/plain content type using %0A character code.
For example:
<a href="mailto:someone#example.com?subject=Hello%20again&body=HI%20%0AThis%20is%20a%20new%20line"/>
Here is the jsfiddle
This worked for me.
$message = nl2br("
===============================\r\n
www.domain.com \r\n
===============================\r\n
From: ".$from."\r\n
To: ".$to."\r\n
Subject: ".$subject."\r\n
Message: ".$_POST['form-message']);
' '
space was missing in my case, when a blank space added ' \r\n' started to work
Another thing use "", there is a difference between "\r\n" and '\r\n'.
"\n\r" produces 2 new lines while "\n","\r" & "\r\n" produce single lines if, in the Header, you use content-type: text/plain.
Beware: If you do the Following php code:
$message='ab<br>cd<br>e<br>f';
print $message.'<br><br>';
$message=str_replace('<br>',"\r\n",$message);
print $message;
you get the following in the Windows browser:
ab
cd
e
f
ab cd e f
and with content-type: text/plain you get the following in an email output;
ab
cd
e
f
for text/plain text mail in a mail function definitely use PHP_EOL constant, you can combine it with too for text/html text:
$messagePLAINTEXT="This is my message."
. PHP_EOL .
"This is a new line in plain text";
$messageHTML="This is my message."
. PHP_EOL . "<br/>" .
"This is a new line in html text, check line break in code view";
$messageHTML="This is my message."
. "<br/>" .
"This is a new line in html text, no line break in code view";
OP's problem was related with HTML coding. But if you are using plain text, please use "\n" and not "\r\n".
My personal use case: using mailx mailer, simply replacing "\r\n" into "\n" fixed my issue, related with wrong automatic Content-Type setting.
Wrong header:
User-Agent: Heirloom mailx 12.4 7/29/08
MIME-Version: 1.0
Content-Type: application/octet-stream
Content-Transfer-Encoding: base64
Correct header:
User-Agent: Heirloom mailx 12.4 7/29/08
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
I'm not saying that "application/octet-stream" and "base64" are always wrong/unwanted, but they where in my case.
$mail = new PHPMailer;
$mail->isSMTP();
$mail->isHTML(true);
Insert this code after working
all html tag like
<br> <p> in $mail->Body='Hello<br> how are you ?<b>';