PHP line breaks don't seem to work - php

I have the following code that sends a message using the mail() function. Everything works fine except the line breaks at the end of each line don't seem to work. As you can see I am using " \r\n " hoping that this will give me a line break but it doesn't I have added <br> to get the break, but I would rather not use that in case someone doesn't have an HTML email client.
<?php
$to = 'user#example.com'; // Was a valid e-Mail address, see comment below
$name = $_REQUEST['name'] ;
$email = $_REQUEST['email'] ;
$subject = $_REQUEST['subject'] ;
$message = $_REQUEST['message'] ;
$content = 'Name: '.$name."\r\n";
$content .= 'Email: '.$email."\r\n";
$content .= 'Subject: '.$subject."\r\n";
$content .= 'Message: '.$message."\r\n";
$headers = 'MIME-Version: 1.0' ."\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' ."\r\n";
// Additional headers
$headers .= 'To: iVEC Help <help#ivec.com>'. "\r\n";
$headers .= 'From: '.$name.' <'.$email.'>' . "\r\n";
mail( $to, $subject, $content, $headers);
?>
<p> You sent it ... good work buddy </p>
<p> <?php '$name' ?> </p>

You're sending it as HTML - change the content type to text/plain and it should work.

The problem is that you say, in your headers, that the mail is an HTML email:
$headers .= 'Content-type: text/html; charset=iso-8859-1' ."\r\n";
If you want to ensure compability with both HTML and text clients, consider using the Multipart content type. This can be achieved in many different ways, but one simple way is to use a library that can do this for you. For example, you can use PEAR:Mail_Mime

I'm not an expert in this area, but my guess would be that you're setting the content type to text/html, which implies html-rendering (which means line breaks are translated to a space). If you're not using any HTML-elements (which appear not to), try setting the content type to text/plain.

As mentioned before, either set it to text/plain or add a HTML break for line breaks:
$content = 'Name: '.$name."</br>\r\n";

Related

Using mail() to sent UTF8 e-mail

I'm trying to send an e-mail with PHP, including multi-language characters. The whole site (html header, php header) are set to UTF8 aswell as the form charset.
To PHP code I have now is:
$to = "email#email.com";
$subject = "Subject";
$message = "Question is ".$question;
$from = "auto#from.com";
$headers = "From:" . $from . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-UTF-8' . "\r\n";
$sendmail = #mail($to,$subject,$message,$headers);
I supose I'm wrong somewhere ?
I'm getting '?' characters with Japanese letters for examnple.
$headers .= "MIME-Version: 1.0"."\r\n";
$headers .= "Content-type: text/html; charset=utf-8"."\r\n";
should be all you need.
I may be wrong, but as far as I know iso-UTF-8 is not a valid charset, at least it doesn't look like it is.

Outlook receives PHP HTML e-mail as code?

I made an HTML e-mail send with PHP, but my client receives this as pure code. Here is the PHP code to send the mail:
$subject = 'HTML e-mail test';
$message = '<html>
<body>
<h1>TEST</h1>
</body>
</html>';
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
$headers .= "To: <".$to.">\r\n";
$headers .= "From: <".$email.">\r\n";
$mailb = mail($to, $subject, $message, $headers);
It works fine for me, but they receive it as:
Content-type: text/html; charset=iso-8859-1
To: <email#email.com>
From: <email#email.com>
<html>
<body>
<h1>TEST</h1>
</body>
</html>
Is there something wrong with my headers? Or is it their Outlook?
How can I fix this problem?
Thanks in advance!
I see:
Content-typetext/html; charset=iso-8859-1
where I should see:
Content-type: text/html; charset=iso-8859-1
Is that a cut/paste error? In your code, or in your result?
Note that you probably want to include both text/plain and text/html types in a multipart message, since HTML-only mail often gets high spam scores (using SpamAssassin, SpamBouncer, etc).
UPDATE #1:
It appears that the \r\n is being interpreted as two newlines instead of one. Different platforms may have different bugs in their implementation of SMTP. It's possible that you can get away with changing your line endings to just \n. If that works with your system, don't rely on it, as it may not work on a different system.
Also, consider switching to a different method for sending mail. phpMailer and SwiftMailer are both recommended over PHP's internal mail() function.
UPDATE #2:
Building on Incognito's suggestion, here's code you might use to organize your headers better, as well as create a plaintext part:
filter_var($to, FILTER_VALIDATE_EMAIL) or die("Invalid To address");
filter_var($email, FILTER_VALIDATE_EMAIL) or die("Invalid From address");
$subject = 'HTML e-mail test';
$messagetext = 'TEST in TEXT';
$messagehtml = '<html>
<body>
<h1>TEST</h1>
<p>in HTML</p>
</body>
</html>';
// We don't need real randomness here, it's just a MIME boundary.
$boundary="_boundary_" . str_shuffle(md5(time()));
// An array of headers. Note that it's up to YOU to insure they are correct.
// Personally, I don't care whether they're a string or an imploded array,
// as long as you do input validation.
$headers=array(
'From: <' . $email . '>',
'MIME-Version: 1.0',
'Content-type: multipart/alternative; boundary="' . $boundary . '"',
);
// Each MIME section fits in $sectionfmt.
$sectionfmt = "--" . $boundary . "\r\n"
. "Content-type: text/%s; charset=iso-8859-1\r\n"
. "Content-Transfer-Encoding: quoted-printable\r\n\r\n"
. "%s\n";
$body = "This is a multipart message.\r\n\r\n"
. sprintf($sectionfmt, "html", $messagehtml)
. sprintf($sectionfmt, "plain", $messagetext)
. "--" . $boundary . "--\r\n";
$mailb = mail($to, $subject, $body, implode("\r\n", $headers));
I'm not saying this is The Right Way to do this by any means, but if the strategy appeals to you, and you think you can maintain code like this after not having looked at it for 6 or 12 months (i.e. it makes sense at first glance), then feel free to use or adapt it.
Disclaimer: this is untested, and sometimes I make typos ... just to keep people on their toes. :)
One good way to be sure is to change your headers to that of this:
$to = 'bob#example.com';
$subject = 'Website Change Reqest';
$headers = "From: " . strip_tags($_POST['req-email']) . "\r\n";
$headers .= "Reply-To: ". strip_tags($_POST['req-email']) . "\r\n";
$headers .= "CC: susan#example.com\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
Also, double check the rest of your code by comparing to this:
http://css-tricks.com/sending-nice-html-email-with-php/
I would recommend not using PHP's mail() function. Yes, it can send an email, but for anything with any kind of complexity, it just makes things difficult.
I'd suggest using a class such as phpMailer, which will give you a lot more flexibility. It makes it much easier to do things like sending HTML emails, adding attachments, and using alternative MTAs.
I've had this issue myself, it was fixed by using \n instead of \r\n.
I only use \r\n for the last line in the header.
The problem is that some antivirus email scanners treat anything following the mime header as body text.
The fix is to make you mime header the last one.
I just had the same problem with one of my clients and it had me whacked for a while.
This is a problem with Microsoft Outlook (Windows).
The solution is to remove the "\r" at the end of the headers and just use "\n".

HTML not formatting in mail function

I am using the following code:
$message = "Hi ".$user.
",\r\rThe following names are on the guestlist for <b>".$night.
"</b> on ".$date2.":\r\r". $user ."<br>". implode(", ", $names).
"\r\rThank you for using Guestvibe.";
mail($email, "Your Guestvibe list", $message);
The carriage returns in PHP are working fine, but neither the <b> or <br> tags are coming out. Is it just my mail client (Apple Mail) playing up, or is there a fix for this?
You are sending the e-mail as text only not HTML, I believe you need to set these headers:
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=UTF-8' . "\r\n";
as in example #4 of the docs.
PHP's mail function sends plaintext mail by default.
See the nr.4 example at mail's manual page for the correct way to send HTML mail.
You need to add your headers:
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
mail($email, "Your Guestvibe list", $message, $headers);
You should firstly wrap the whole message into:
<html><head></head><body> msg </body></html>
Plus headers from m.edmondson post.

send mail by bbc not working

hay
i used this code
$to = "mial#live.com,mail#yahoo.com";
$subject = "Mini-mass Emailer";
$message = "<a href='#'>Hello World</a>";
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From: Your Name <me#mydomain.com>' . "\r\n";
$headers .= 'Bcc: {$to}' . "\r\n";
if(mail($to, $subject, $message, $headers)){
echo 'ok';
}
but see what is happend
every user see the full list of the users
alt text http://img694.imageshack.us/img694/1289/21811933.gif
Your call to mail is passing the $to as the to parameter meaning those emails will be be in the to header try passing an empty string instead. You are passing the info into the bcc header so the email should still get to them that way.
That is because you have put all the users in the "to" line. You are also passing them into the "bcc" line too so just doing this may help you but as far as I know you need at least one address in the to line (although this may not be the case). It'll look pretty strange for each person doing it that way though.
The best way to avoid these issues would be to send the email multiple times, once to each user. To modify your code example to do this, I'd do something like the following:
$toAddresses = array("mial#live.com", "mail#yahoo.com");
$subject = "Mini-mass Emailer";
$message = "<a href='#'>Hello World</a>";
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From: Your Name <me#mydomain.com>' . "\r\n";
foreach ($toAddresses as $to) {
if(mail($to, $subject, $message, $headers)){
echo "OK - sent message to {$to}";
}
}
The easiest way is to take this Mail-Class of phpguru.org:
http://www.phpguru.org/static/htmlMimeMail5
There you can specify with setBcc() the addresses which should be "blind", it's pretty easy and works well. I use this class in every project.
Best Regards.

php email() with image

Using outlook I can send emails with images inserted into message body(not as attachment). How can i do that using mail() function from PHP?
I would recommend Swift Mailer:
http://swiftmailer.org/docs/embedding-files
From the documentation (Example #4 Sending HTML email):
Note the $message variables contents, and the value of the $headers variable.
$to = "john#doe.com";
$subject = "HTML Email";
$message = "Hello <img src='http://mysite.com/world.jpg' />";
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From: HTML Emailer <auto#example.com>' . "\r\n";
mail($to, $subject, $message, $headers);
If the emails are in html/mime format you could do it as html...
I have used HTML Mime Email extensively, and it is very straightforward:
http://www.phpclasses.org/browse/package/32.html
$mail = new htmlMimeMail();
$mailhtml = $mail->getFile('./emailheader.html');
$mailimglogo = $mail->getFile('./images/email-logo-1.jpg');
$mail->addHTMLImage($mailimglogo, 'email-logo-1.jpg', 'image/jpeg');
$mailhtml .= '<tr><td class="mailheader" colspan="2" align="center">';
$mailhtml .= '<img src="email-logo-1.jpg"></td></tr>';
...
$mailhtml .= $mail->getFile('./emailfooter.html');
$mail->setHtml($mailhtml);
$mail->setFrom('Dana Brainerd <dana#danabrainerdphotography.com>');
$mail->setCc('adam#adamcasey.net');
$mail->setBcc('webmaster#danabrainerdphotography.com');
$mail->setSubject("Dana Brainerd Photography Order Number {$roworder['order_number']}");
$mailresult = $mail->send(array($roworder['customer_email']));
If you don't want to host the images someplace and would them to to be included inline, you'll need to do is encode them, insert the encoded text and reference them by ID. PHPmailer handles this pretty nicely (see Inline Attachments):
http://phpmailer.worxware.com/index.php?pg=tutorial#3
Otherwise, you can just reference them by their web address as described in the other posts.

Categories