Jump to content

[SOLVED] Sending HTML mail from PHP


Andy17

Recommended Posts

Hey guys,

 

I have a small problem with my e-mail script that sends an HTML mail. The sent text is the content a user enters into a text field. So, when the user presses Enter in the text field to make spaces, I would like these spaces to be shown in the mail. I have tried using nl2br() but without success. I have the following code (I obviously have more, but the code below is the relevant part):

 

<?php

$mail = mysql_real_escape_string(trim(strip_tags(htmlspecialchars($_POST['receivermail'], ENT_QUOTES))));
$subject = mysql_real_escape_string(trim(strip_tags(htmlspecialchars($_POST['subject'], ENT_QUOTES))));
$message = mysql_real_escape_string(trim(strip_tags(htmlspecialchars($_POST['mailtext'], ENT_QUOTES))));

$headers  = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
$headers .= "from: no-reply@dizzit.net";

mail($mail, $subject, $message, $headers);

?>

 

If I type in "a [space] b [space] c", it will look like this in the mail: a\r\nb\r\nc

 

I guess the problem is in the headers? I'm just not sure what to change there since I'm new to that part. So basically I just want a line break to write <br />.

 

Thank you.

Link to comment
Share on other sites

nl2br should do it

<?php

$mail = htmlspecialchars($_POST['receivermail'], ENT_QUOTES);
$mail = mysql_real_escape_string($mail);
$mail = trim($mail);
$mail = strip_tags($mail);

$subject = htmlspecialchars($_POST['subject'], ENT_QUOTES);
$subject = mysql_real_escape_string($subject);
$subject = trim($subject);
$subject = strip_tags($subject);

$message = htmlspecialchars($_POST['mailtext'], ENT_QUOTES);
$message = mysql_real_escape_string($message);
$message = trim($message);
$message = strip_tags($message);
$message = nl2br($message);

$headers  = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
$headers .= "From: Dizzit.net<no-reply@dizzit.net>";

mail($mail, $subject, $message, $headers);

?>

You have to do it after you use htmlentities or it gets converted to a safe form

 

Link to comment
Share on other sites

Hi Blade280891 and thank you for your reply. Unfortunately, I already tried this before posting here (sorry for not mentioning how I used nl2br()) and it does not work. Each space, which should be "<br />" displays as "\r\n" in the sent e-mail.

Link to comment
Share on other sites

I'm not entirely sure what you mean because I do not want to show the e-mail on the website when a user presses the send button. I would just like it to send it. Anyways, here is the code:

 

<?php

if (isset($_POST['submitbutton']))

{

$mail = htmlspecialchars($_POST['receivermail'], ENT_QUOTES);
$mail = mysql_real_escape_string($mail);
$mail = trim($mail);
$mail = strip_tags($mail);

$subject = htmlspecialchars($_POST['subject'], ENT_QUOTES);
$subject = mysql_real_escape_string($subject);
$subject = trim($subject);
$subject = strip_tags($subject);

$message = htmlspecialchars($_POST['mailtext'], ENT_QUOTES);
$message = mysql_real_escape_string($message);
$message = trim($message);
$message = strip_tags($message);
$message = nl2br($message);

$headers  = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
$headers .= "From: dizzit.net<no-reply@dizzit.net>";

mail($mail, $subject, $message, $headers);

}

?>

<form name="sendmail" method="post">

Reciever's e-mail address: <input type="text" name="receivermail" id="receivermail" value="example@domain.com" onfocus="if (this.value == 'example@domain.com') {this.value = '';}" onblur="if (this.value == '') {this.value = 'example@domain.com';}" />

Subject: <input type="text" name="subject" id="subject" value="Please write a descriptive subject" onfocus="if (this.value == 'Please write a descriptive subject') {this.value = '';}" onblur="if (this.value == '') {this.value = 'Please write a descriptive subject';}" />

Message: <textarea name="mailtext" id="mailtext" style="width: 400px; height: 200px;" onfocus="if (this.value == 'Write your message here...') {this.value = '';}" onblur="if (this.value == '') {this.value = 'Write your message here...';}" />Write your message here...</textarea>

<input type="submit" name="submitbutton" value="Send" />

</form>

Link to comment
Share on other sites

Do what i said.

To view the source code right click on the page then view source.

 

Oh, sorry, I just misunderstood you. I echo'd it and it looked like this:

 

this\r\n\r\nis\r\n\r\na\r\n\r\ntest\r\n\r\nfor\r\n\r\nBlade280891

 

Where it should have looked like this:

 

this<br /><br />is<br /><br />a<br /><br />test<br /><br />for<br /><br />Blade280891

 

So, for some reason, "\r\n" is printed instead of "<br />". I noticed the \r\n in the header and was wondering if I would have to change something there.

Link to comment
Share on other sites

Ok, one last test echo the $message out after each change

e.g.

$message = htmlspecialchars($_POST['mailtext'], ENT_QUOTES);
echo "Stage 1: ".$message;//do this after every change.
$message = mysql_real_escape_string($message);
$message = trim($message);
$message = strip_tags($message);
$message = nl2br($message);

Link to comment
Share on other sites

Ok it appears that the $message = mysql_real_escape_string($message); line caused the problem (for some unknown reason), so I moved this line to after the mail is sent. Now the line breaks work, but the following code still does escapes:

 

<?php

echo $_POST['mailtext']; // Stage 1
$message = htmlspecialchars($_POST['mailtext'], ENT_QUOTES);
// Stage 2
$message = trim($message);
// Stage 3
$message = strip_tags($message);
// Stage 4
$message = nl2br($message);
// Stage 5

?>

 

This means that it puts a \ in front of every ', even though I don't do mysql_real_escape_string() anywhere in my code. This already happens at stage 1 and I tried to remove the ENT_QUOTES but nothing changed.

 

This is very strange.

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.