Jump to content

PHP MAIL with HTML message problem


PHP_TRY_HARD

Recommended Posts

I am having lots and lots of trouble!

When I send the PHP mail everything works fine except the Text comes through with all the html tags.  How can I have it email trough as a nice html email message. I need to pimp my email with a bold font and logo etc.  Below is the code.  Please help!! :'(

 

//Start Message

$message = "

 

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />

  <title>You Have Contacted *********</title>

</head>

<body>

 

 

<p><font face=arial size=2><strong>Dear $Name</strong>,</p>

 

<font face=arial size=2><p>Thank you for contacting *******, we appreciate your comments.  An ******** representative will be in contact with you within the next 2 business days.<br><br>The safe collection of your information and your privacy are important to us, as such please visit <a href=http://www.**********.com.au/>www.**********.com.au</a> to view our privacy policy  in full.<br><br>

Your Enquiry number is:  <strong>#$enq_num </strong><br>Date Submitted $dt <br> Please Keep this number for future reference.<br>

</p>

<font face=arial size=2><p>Please check your details below for accuracy.</p></font>

 

<font face=arial size=2><strong>Name:</strong> $Name <br></font>

 

<font face=arial size=2><strong>Email Address:</strong> $email<br></font>

 

<font face=arial size=2><strong>Address:</strong> $Address<br></font>

 

<font face=arial size=2><strong>Postcode:</strong> $postcode<br></font>

 

<font face=arial size=2><strong>User Comments:</strong><br>$User_Comments<br><br><br></font>

<hr noshade color=gray><br>

<font face=arial size=2>   

If you wish to change these details, please email us at<br>

<a href=mailto:info@*******.net.au>info@******.net.au</a> with your request.<br><br>

   

Please print this information and store it for future reference.<br><br>

 

Yours Sincerely,

 

<p><strong>***** ********<br>

** - Marketing and Public Relations</strong><br>**********<br><font color=gray>

Phone: (00) 0000 0000 <font color=red>|</font> Fax:(00) 0000 00000<br>0111 ***************** Road **********</p><br></font>

<img src=http://www.zzzzzzzzzzzzz.com.au/date.png><br><br><img src=http://www.xxxxxxxxxxx.com.au/logozzzzz/logo.png><br>

</body>

</html>

";

 

 

//End Message

 

$to = "info@********.net.au";

 

// Always set content-type when sending HTML email

$headers = 'MIME-Version: 1.0' . "\r\n";

$headers .= "From: $yoursite <$youremail>\r\n";

$headers .= "Reply-To: info@*********.net.au\r\n";

$headers .= "Return-Path: info@*********.net.au\r\n";

$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

 

 

 

//Mail the thing

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

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

    mysql_close();

    header( "Location: $thankyouurl" );

exit();

 

/*if(mail($to, $subject, $message, $headers)){

    echo "success=true";

}else{

    echo "success=false";

}  */

 

?>

Link to comment
Share on other sites

Thank you for the super quick reply, should I put the tags on their own line?  Is this correct?

 

Thank you once again.

 

//Start Message

$message = "<html>"

$message = "<head>"

$message = "<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />"

$message = "</head>"

$message = "<body>"

Link to comment
Share on other sites

Thanks,

 

When I used the multipul "$message" it would't even send the mail.  I cut the cod down to the bare mininmum and it still wouldn't send.  Any Ideas?

 

//Start Message

$message = "<html>"

$message = "<body>"

$message = "<p><font face=arial size=2><strong>Dear $Name</strong>,</p>"

$message = "</body>"

$message = "</html>";

 

Link to comment
Share on other sites

This is wrong.

<?php
$message = "<html>"
$message = "<body>"
$message = "<p><font face=arial size=2><strong>Dear $Name</strong>,</p>"
$message = "</body>"
$message = "</html>";
?>

Do like this.

<?php
$message = '<html>';
$message =.'<body>';
$message =.'<p><font face="arial" size="2"><strong>Dear '.$Name.'</strong>,</p>';
$message =.'</body>';
$message =.'</html>';
?>

The =. appends to the string, where as = replaces the string. Also using all of those double qoutes is putting php in eval mode, which takes longer to process.

Make sure you don't do this:

<?php
$message = "<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />"
?>

You effectively stoped writing to your string when you used double qoutes before you were done. You have to open and close with the same type of qoutes, preferably single qoutes unless you have a variable. Like so:

<?php
$message = '<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />;
//if you have a variable do it like this
$message = '<meta http-equiv="Content-Type" content="'.$content.'; charset=iso-8859-1" />';
?>

Finaly Do not forget your semi colons (  ;  )

Link to comment
Share on other sites

Thank you both for your help once again,

 

I have tried to emulate your code but it still isn't working.

is this legal?

 

$message .= ''.$email.'';

 

Here is the code that I tried to fix.

 

//Start Message
$message = '<html>';
$message .= '<body>';
$message .= '<p><font face="arial" size="2"><strong>Dear '.$Name.'</strong>,</p>';
$message .= '<font face="arial" size="2">';
$message .= '<p>';
$message .= 'Thank you for contacting Client, we appreciate your comments.';
$message .= ' An Client representative will be in contact with you within the next 2 business days.';
$message .= '<br>';
$message .= '<br>';
$message .= 'The safe collection of your information and your privacy are important to us, as such please visit'; 
$message .= '<a href="http://www.client.com.au/">www.client.com.au</a> to view our privacy policy  in full.';
$message .= '<br>';
$message .= '<br>';
$message .= 'Your Enquiry number is:';
$message .=  '<strong>';
$message .= ''.$enq_num.''; 
$message .= '</strong>';
$message .= '<br>';
$message .= 'Date Submitted';
$message .= ''.$dt.'';
$message .= '<br>'; 
$message .= 'Please Keep this number for future reference.';
$message .= '<br>';
$message .= '</p>';
$message .= '<font face="arial" size="2"><p>Please check your details below for accuracy.</p></font>';

$message .= '<font face="arial" size="2"><strong>Name:</strong>';
$message .= ''.$Name.''; 
$message .= '<br>';
$message .= '</font>';

$message .= '<font face="arial" size="2"><strong>Email Address:</strong>';
$message .= ''.$email.'';
$message .= '<br>';
$message .= '</font>';

$message .= '<font face="arial" size="2"><strong>Address:</strong>';
$message .= ''.$Address.'';
$message .= '<br>';
$message .= '</font>';

$message .= '<font face="arial" size="2"><strong>Postcode:</strong>';
$message .= ''.$postcode.'';
$message .= '<br>';
$message .= '</font>';

$message .= '<font face="arial" size="2"><strong>User Comments:</strong>';
$message .= '<br>';
$message .= ''.$User_Comments.'';
$message .= '<br>';
$message .= '<br>';
$message .= '</font>';
$message .= '<hr noshade color="gray">';
$message .= '<br>';
$message .= '<font face="arial" size="2"> If you wish to change these details, please email us at';
$message .= '<br>' 
$message .= '<a href="mailto:info@client.net.au">info@client.net.au</a> with your request.';
$message .= '<br>';
$message .= '<br>';
$message .= 'Please print this information and store it for future reference.';
$message .= '<br>';
$message .= '<br>';

$message .= 'Yours Sincerely,';
$message .= '<p>';
$message .= '<strong>client';
$message .= '<br>';
$message .= 'GM - clinet</strong>';
$message .= '<br>';
$message .= 'client';
$message .= '<br>';
$message .= '<font color="gray">';
$message .= 'Phone: (07) 3113 5888'; 
$message .= '<font color="red">|</font> Fax:(07) 0000 8888';
$message .= '<br>';
$message .= 'address, address</p>';
$message .= '<br>';
$message .= '</font>';
$message .= '<br>';
$message .= '<img src="http://www.client.com.au/client/logo.png">';
$message .= '<br>';
$message .= '</body>';
$message .= '</html>';



//End Message

Link to comment
Share on other sites

this is a simple html email i use and it work for me and it works great and really simple

 

here is the mail form

mail.php

<form method="post" action="mail2.php">
<table width=100% height=100% cellspacing=0 cellpadding=5 bgcolor=lightblue valign=top><td>
To: <input type=text name=to size=30 style="border-width:0px;">
<br>
From: <input type=text name=from style="border-width:0px;">
<br>
Subject: <input type=text name=sub size=60 style="border-width:0px;"><br>
<br>
Messege:<br>
<textarea name=mess cols=100 rows=20 style="border-width:0px;"></textarea><br>
<br>
<input type=submit value=send>
</td></tr></table></form>

 

and her is the php code to send it

 

$to=$_POST['to'];
$mess=$_POST['mess'];
$my_subject=$_POST['sub']; 
$messege="$_POST['mess']"; 



$my_headers ="MIME-Version: 1.0\r\n"; 
$my_headers.="Content-type: text/html; charset=iso-8859-1\r\n"; 
$my_headers.="From: $_POST[from]"; 


$mail=mail("$to", "$my_subject", "$messege", "$my_headers"); 
     if(!$mail){
print "<center>Sorry Your E-mail Was Not Sent</center>";
}else{
print "<center>Your E-mail Was Sent Successfuly</center>";
} 

hopefully this helps and if it doesnt i am sorry

Link to comment
Share on other sites

>:( Its working now because I forgot a ";" but now it is showing up in my email with all the tags.

a snippets is below

 

html><body><p><font face="arial" size="2"><strong>Dear TESTER</strong>,</p><font face="arial" size="2"><p>Thank you for contacting client, we appreciate your comments. An client representative will be in contact with you within the

 

Any one know what Im doing wrong? 

Link to comment
Share on other sites

No I was making sure that you were not using it, because it would convert all of the Tags and that would be bad. However you should use htmlspecialchars() on the user input before you insert it into your message to prevent a malicious user from injecting links and other tags. But I did notice that your snippet said:

html><body><p>

Make sure that it actually starts like: <html><body> and ends with </body></html> . Other than that every thing looks like it should be working.

Link to comment
Share on other sites

for a start i have never had to put all the head and html tags in to get it to display as an html page when sending emails from a php script. and secondly i have never had to put message at the beginning of every line concatenated or not.

 

here is a script i am using at the minute which retrieves all the email address from a mysql db and sends out an email to each address when my web page is updated.

 

$message .= '<body leftmargin="0" topmargin="0">
<table width="700" border="0" align="center" cellpadding="0" cellspacing="5">
   <tr>
	<td>The ' . $table . ' section of the <a href="piponline.info"> PiPonline website</a> has been updated.  Please follow the link to see the new content.
</tr>
 </table>
 </body>';


$query = "SELECT * FROM emails WHERE page = '$table'";
$result = mysql_query($query) or die(mysql_error());

while($row = mysql_fetch_assoc($result)) {

$contactemail = $row['email'];




// Contacts
//$replyName = $merc_replyId;
//$replyEmail = $merc_replyAddress;

$replyName = "xxxxxx";
$replyEmail = "xxxxxxxx";

$contactname = "";


// Subject
$subject = "Website Update";

// Headers
$headers = "MIME-Version: 1.0" . PHP_EOL;
$headers .= "Content-type: text/html; charset=iso-8859-1" . PHP_EOL;
$headers .= "From: ".$replyName." <".$replyEmail.">" . PHP_EOL;
$headers .= "BCC: ".$contactname." <".$contactemail.">\r\n" . PHP_EOL;


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

 

where i have this

$message .= '

in my script is the start of the email body you can put anything after this in html format and it will display as you like. to end the message body just simply end the last body tag a show in the script above and all should work fine

 

anywhere you would like to have php variables showing up on the page you must format them as follows

 

' . $table . '

for them to show up correctly.

 

also remember as it is an email if you want to have any images in there being pulled from your server they will need the absolute file reference for it show correctly so it would look some thing like

 

http://www.yoursite.com/images/image_you_want.gif

 

i know the html section of this is not very big but if you would like me to post one which has all the bells an whistles just ask and i will.

Link to comment
Share on other sites

The most common reason for this, I believe, is bad headers.

shortj75 nailed it already, that post contains everything you need to send HTML email.

(however, this: $to=$_POST['to']; is a wide open door to become a spam relay, please don't do that on the internet)

check your headers at the client by viewing the raw source of the email.

Link to comment
Share on other sites

  • 2 weeks later...
(however, this: $to=$_POST['to']; is a wide open door to become a spam relay, please don't do that on the internet)

 

Do you have any links to alternatives to avoid the spam potential? I knew this could be a problem and I have finished my app locally and want to seal this part up before it's live. Any suggestions or links to?

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.