Jump to content

Recommended Posts

Hi All,

 

Can anyone suggest where I can start looking to on how to create a HTL Rich E-Mail that is generated from a PHP form?

 

I have done various searchs on Google and I haven't been able to find anything that seems to work.

 

Basically, I want the form infor to be psoted to the MySQL database, but at the same time to send a HTML Rich E-mail to the used who completed the form.

 

I have the normal e-mail and writing to the database working, I just want to spice it up now...

 

Any help will be appreciated.

Link to comment
https://forums.phpfreaks.com/topic/270077-html-rich-e-mail-from-php-form/
Share on other sites

$regPassword = sha1($regPassword);
$id = uniqid();

$register = insert("members", "name, email, username, password, user_level, id, ban", "'$regName', '$email', '$regUsername', '$regPassword', 1, '$id', 0");

if($register){

$newsTitle = "New member registered!";

$cont = $regUsername." has just joined Fusion Forums!<br>";
$cont.= "Check out his/her profile:<br><br>";
$cont.= "View Profile";

$newsCont = $cont;

$newMem = insert("news", "news_title, news_content, username", "'$newsTitle', '$newsCont', '$regUsername'");

if($newMem){ 
$to = $email;
$subject = "Fusion Forums - Account Confirmation";
$message = "Hello! You have recently registered to Fusion Forum's.<br><br>";
$message.= "This is a confirmation email, below you will find your account details along with a Unique ID.<br><br>";
$message.= "In order to activate your account you must first enter the ID into the text field that follows the link at the end of this email. Your details are as follows:<br><br>";
$message.= "<table>";
$message.= "<tr>";
$message.= "<td><strong>Name:</strong></td>";
$message.= "<td></td>";
$message.= "<td>".$regName."</td>";
$message.= "<tr>";
$message.= "<tr>";
$message.= "<td><strong>Email:</strong></td>";
$message.= "<td></td>";
$message.= "<td>".$email."</td>";
$message.= "<tr>";
$message.= "<tr>";
$message.= "<td><strong>Username:</strong></td>";
$message.= "<td></td>";
$message.= "<td>".$regUsername."</td>";
$message.= "<tr>";
$message.= "<tr>";
$message.= "<td><strong>Unique ID:</strong></td>";
$message.= "<td></td>";
$message.= "<td>".$id."</td>";
$message.= "<tr>";
$message.= "</table><br><br>";
$message.= "Please follow this link in order to activate your account (opens in your default browser):<br>";
$message.= "<a href='http://www.janedealsart.co.uk/activate.php?id=".$id."'>Activate Account</a>";
$from = "noreply@janedealsart.co.uk";
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers.= "From: ".$from;
mail($to, $subject, $message, $headers);
$done = "You have successfully registered to Fusion Fourm's.<br>";
$done.= "We have sent you an email with a confirmation code on it,";
$done.= " when you go to confirm your account you will need this code in order to be able to access the forum's.";

$msg2 .= $done; 

}else{
$msg3 .= "Sorry, we could not register your account details, if this persists contact the webmaster. ";
}
}else{
$msg3 .= "Sorry, we could not register your account details, if this persists contact the webmaster. ";
}

 

That should work, it writes the users details to the database, enters some content into a news feed, then sends the user who registered an email with their details.

Ok, thats how I had it, but I just realsied that I was being a bit of an idiot and I tried adding the image to the subject tag. And of coarse this won't display an image.

 

So basic HTML/PHP coding from here on in. Thanks guys, I should be able to finifh this now. :happy-04:

You are effectivly sending an html page as the message body, so you can bould it as you would any other html page. You shouldn't need much css for an email so I'd suggest sending the css as pard of the head rather than linking to it, but you can stick it in the head whichever way you like :

$message = <<<HTML_MESSAGE
<html>
<head>
<title></title>
<!-- link to a css file -->
<link rel="stylesheet" type="text/css" href="http://your_server.com/css/email_styles/filename.css" />
<!-- add a block of css to style the mail -->
<style type="text/css">
.all_your_css{
goes:in here;
}
</style>
</head>
<body>
<div class="all_your_css">Your Eail Content Goes Here</div>
</body>
</html>
HTML_MESSAGE;

My image isn't attaching :-\

 

$message.= "<body background='http://vmcedctr01/img/mail_bg_main.jpg' width='100%' height='100%'/><font face='Arial' size='2' color='black'>";
$message.= "Hi $_POST[owner]<br><br>";
$message.= "Thank you for placing an online order.
Here is a summary of your order.
Please keep this email as a reference if you wish to track your order.<br><br>
Your order details are as follows:<br><br>";
$message.= "<table border=1 width='40%'>";

 

That's just the top part of the code.

Here is the code. Keep in mind that:

  1. A big part of this forms the actual body of the message.
     
  2. The server won't resolve as it is an internal server that I am testing on first.
     
  3. Some of the message body has been removed due to confidentiaity.
     
  4. Line 238 is the clsoing php tag -> ?> and will now be line 95 with the confidentiality part of the message body removed.

<?php
$con = mysql_connect("localhost","username","password");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}

if
(
$_POST['owner'] == "Owner Name"
)
{
$to = "Owner@departmentname.co.za";
$from = 'csops@departmentname.co.za';
$subject = "New Request - Reference Number $_POST[reference]_$_POST[request_type]";
$message.= "
<html>
<body background='http://internalservername/img/mail_bg_main.jpg' alt='name/description of image' width='100%' height='100%'/><font face='Arial' size='2' color='black'>
<table border='0' width='85%' background='http://internalservername/img/mail_bg.jpg' align='center'><font face='Arial' size='2' color='black'>
<td>
<table border='0' width='43%' align='center'>
<td>
<font face='Arial' size='2' color='black'>Hi $Name<br><br>
Thank you for placing an online order with departmentname.
Here is a summary of your order.
Please keep this email as a reference if you wish to track your order.<br><br>
Your order details are as follows:<br><br>
<table border='' width='40%'><font face='Arial' size='2' color='black'>
<tr>
<td><strong><font face='Arial' size='2' color='black'>Order Number:</strong></td>
<td></td>
<td><font face='Arial' size='2' color='black'>.$orderNumber.</td>
</tr>
<tr>
<td><strong><font face='Arial' size='2' color='black'>Order Date:</strong></td>
<td></td>
<td><font face='Arial' size='2' color='black'>.$orderDate.</td>
</tr>
</table><br><br>



<table border='' width='100%'><font face='Arial' size='2' color='black'>

<tr>
<td width='25%'><font face='Arial' size='2' color='black'><strong>Deal Description</strong></td>
<td width='25%'><font face='Arial' size='2' color='black'><strong>Quantity</strong></td>
<td width='25%'><font face='Arial' size='2' color='black'><strong>Price</strong></td>
<td width='25%' colspan='2'><font face='Arial' size='2' color='black'><strong>Amount</strong></td>
</tr>
<tr>
<td width='25%'><font face='Arial' size='2' color='black'>$dealDescription</td>
<td width='25%'><font face='Arial' size='2' color='black'>$quantity</td>
<td width='25%'><font face='Arial' size='2' color='black'>$price</td>
<td width='12.5%'><font face='Arial' size='2' color='black'>Total</td>
<td width='12.5%'><font face='Arial' size='2' color='black'>$total</td>
</tr>



<table border='0' width='100%'><font face='Arial' size='2' color='black'>

<tr>
<td><font face='Arial' size='2' color='black'><strong>Returns Policy:</strong> You can cancel this order within two weeks after we have received it.
You can return new, unopened items from a cancelled order within 2 weeks after they have been delivered to you.
Items should be returned in their original packaging.<br>
<br>Should you have any queries, do get in touch with our customer services team on: 0741 000 5555 or send us an email shoponline@departmentname.co.za<br>
<br>Your new phone will be dispatched once all your details are confirmed.<br>
<br>Look forward to welcoming you to the family!<br>
<br>Best wishes,<br>
<font color='CC0000'>
departmentname South Africa</td>
</tr>
</table><br><br>
</td>
</table>
</td>
</table>
</html>";


mail($to, $subject, $message, $from);
?>

Edited by SalientAnimal

well that's unfortunate. Basicly your error is coming from the fact that something before line xxx is not terminated properly, either a missing semi-colon or closing curly brace is the common cause of this. but if you are striping out lines (which I fully appreciate you're need to do), then that's going to make finding it a little difficult. Here is a suggestion that I think should help. when you are assigning multiple string lines to a variable then use a heredoc string block

i.e.

$message .= <<<MESSAGE_PT2
<html>
<body background='http://internalservername/img/mail_bg_main.jpg' alt='name/description of image' width='100%' height='100%'/><font face='Arial' size='2' color='black'>
<table border='0' width='85%' background='http://internalservername/img/mail_bg.jpg' align='center'><font face='Arial' size='2' color='black'>
<td>
<table border='0' width='43%' align='center'>
<td>
<font face='Arial' size='2' color='black'>Hi $Name<br><br>
Thank you for placing an online order with departmentname.
Here is a summary of your order.
Please keep this email as a reference if you wish to track your order.<br><br>
Your order details are as follows:<br><br>
<table border='' width='40%'><font face='Arial' size='2' color='black'>
<tr>
<td><strong><font face='Arial' size='2' color='black'>Order Number:</strong></td>
<td></td>
<td><font face='Arial' size='2' color='black'>.$orderNumber.</td>
</tr>
<tr>
<td><strong><font face='Arial' size='2' color='black'>Order Date:</strong></td>
<td></td>
<td><font face='Arial' size='2' color='black'>.$orderDate.</td>
</tr>
</table><br><br>



<table border='' width='100%'><font face='Arial' size='2' color='black'>

<tr>
<td width='25%'><font face='Arial' size='2' color='black'><strong>Deal Description</strong></td>
<td width='25%'><font face='Arial' size='2' color='black'><strong>Quantity</strong></td>
<td width='25%'><font face='Arial' size='2' color='black'><strong>Price</strong></td>
<td width='25%' colspan='2'><font face='Arial' size='2' color='black'><strong>Amount</strong></td>
</tr>
<tr>
<td width='25%'><font face='Arial' size='2' color='black'>$dealDescription</td>
<td width='25%'><font face='Arial' size='2' color='black'>$quantity</td>
<td width='25%'><font face='Arial' size='2' color='black'>$price</td>
<td width='12.5%'><font face='Arial' size='2' color='black'>Total</td>
<td width='12.5%'><font face='Arial' size='2' color='black'>$total</td>
</tr>



<table border='0' width='100%'><font face='Arial' size='2' color='black'>

<tr>
<td><font face='Arial' size='2' color='black'><strong>Returns Policy:</strong> You can cancel this order within two weeks after we have received it.
You can return new, unopened items from a cancelled order within 2 weeks after they have been delivered to you.
Items should be returned in their original packaging.<br>
<br>Should you have any queries, do get in touch with our customer services team on: 0741 000 5555 or send us an email shoponline@departmentname.co.za<br>
<br>Your new phone will be dispatched once all your details are confirmed.<br>
<br>Look forward to welcoming you to the family!<br>
<br>Best wishes,<br>
<font color='CC0000'>
departmentname South Africa</td>
</tr>
</table><br><br>
</td>
</table>
</td>
</table>
</html>
MESSAGE_PT2;

 

use this for all large multi-line string blocks and life should get much easier. Just a note, when closing the heredoc you can not have any leading white space at the start of the line, it must be the first thing on that line.

Ok cool, I now have the picture attaching etc. but, sadly only the first picture seems to attach.

 

The second one doesn't.

Also outlook is disabling the pictures from automatically displaying... Is there a way to force the pictures to display?

not from the senders side. It would require the security settings on the mail client to be tweeked, setting the senders domain to trusted in the junk filter could be enough, but you may also need to change other options as well. it's a common factor, which is why most people have a "having trouble viewing this message? click here" bit in the mail somewhere that links to a web page version.

 

what's the difference between the image files? are they in the same folder on the server? are the file extensions the correct case (href is case sensitive remember)

The images are both in the same location on the server. I have tried various images from this folder. the only difference is that the one image is a full background image for the entire page and the other image is an image embeded in a table.

 

Here are the codes for the images:

 

<body background='http://servername/img/mail_bg_main.jpg' alt='Background Image' width='100%' height='100%'><font face='Arial' size='2' color='white'>

<table border='1' background='http://servername/img/mailbg.jpg' alt='Message Background Image' width='85%' align='center'><font face='Arial' size='2' color='white'>

 

The image in the body tag works.

The image in the table tag doesn't work.

Edited by SalientAnimal

you could try

<table border='1' style='background-image:url("http://servername/img/mail_bg.jpg");width:85%;font-family:Arial;font-size:small;color:white;'>

I've never tried to apply a background image to a table before, but I have had tables be strangly fussy about how they take style info.

You better check the url I used, it's got an underscore in it, as that's what was in your original post, but I see in your last one there it's not got one...

Edited by Muddy_Funster
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.