PDA

View Full Version : How to format an email sent using PHP


tux
09-24-2009, 01:35 PM
Hello All,

I am using this code to send an email using PHP but how do you format what is sent in the email so it looks good when it is received. Here is the code I am using to send the email from a form filled in by the user.

<?php
$submitted = $_POST['submitted'];
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$email = $_POST['email'];
$telephone = $_POST['telephone'];
$mobile = $_POST['mobile'];
$question = $_POST['question'];
///////////////////////////////////////////////
if($submitted && $firstname && $lastname && $email && $question){

$subject = 'Some Subject Here';
$emailadd = 'myemail@myemail.co.uk';
$sender = $_POST['email'];
$url = '../contact.php?thankyou=1';
$req = '0';

$text = "Results submitted from the Contact Form:\n\n";
$space = ' ';
$line = '
';
foreach ($_POST as $key => $value)
{
if ($req == '1')
{
if ($value == '')
{echo "$key is empty, please use your back button and complete the form. Thank you.";die;}
}
$j = strlen($key);
if ($j >= 20)
{echo "Name of form element $key cannot be longer than 20 characters";die;}
$j = 20 - $j;
for ($i = 1; $i <= $j; $i++)
{$space .= ' ';}
$value = str_replace('\n', "$line", $value);
$conc = "{$key}:$space{$value}$line";
$text .= $conc;
$space = '';
}
mail($emailadd, $subject, $text, 'From: '.$sender.'');
echo '<META HTTP-EQUIV=Refresh CONTENT="0; URL='.$url.'">';

}elseif($submitted){
header("location:contact.php?error=1");
exit;
}


?>



This is how the email looks at present when it is received:

Results submitted from the Contact Form:

submitted: 1
firstname: Some First Name
lastname: Some Last Name
email: users_email@users_email.co.uk
telephone:
mobile:
question: Testing the email
x: 24
y: 7


Not sure where the x & y, 24 & 7 come from.

How can the user info be formatted so it lines up?

Thanks everyone.

Paul

edbr
09-25-2009, 12:49 AM
the values you got may have come from the form cant see why you got them
try this, replace $j = strlen($key);
if ($j >= 20)
{echo "Name of form element $key cannot be longer than 20 characters";die;}
$j = 20 - $j;
for ($i = 1; $i <= $j; $i++)
{$space .= ' ';}
$value = str_replace('\n', "$line", $value);
$conc = "{$key}:$space{$value}$line";
$text .= $conc;
$space = '';
}
with
$text = "
First name $firstname \n
Last name $lastname \n
e mail address $email \n
telephone $telephone \n
mobile $mobile \n
test question $question \n

";

tux
09-25-2009, 09:02 AM
Thanks for your input Edbr,

I have done some research on this and have found out how to send HTML email using PHP. This code also sends plain text in case the user has HTML email turned off.

Here is the code.......

<?php
///////////////////////////////////////////////declare your variables here from the form on the previous page
$submitted = $_POST['submitted'];
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$email = $_POST['email'];
$telephone = $_POST['telephone'];
$mobile = $_POST['mobile'];
$question = $_POST['question'];
///////////////////////////////////////////////ensure required fields are filled in
if($submitted && $firstname && $lastname && $email && $question){
///////////////////////////////////////////////
$to = 'youremail@here';//define the receiver of the email
$subject = 'required subject here';//define the subject of the email
///////////////////////////////////////////////

//create a boundary string. It must be unique
//so we use the MD5 algorithm to generate a random hash
$random_hash = md5(date('r', time()));
//define the headers we want passed. Note that they are separated with \r\n
$headers = "From: yourfrom@emaliaddresshere\r\nReply-To: yourreplyto@emaliaddresshere";//put the 'From' & 'Reply To' email address here
//add boundary string and mime type specification
$headers .= "\r\nContent-Type: multipart/alternative; boundary=\"PHP-alt-".$random_hash."\"";
//define the body of the message.
ob_start(); //Turn on output buffering
?>

--PHP-alt-<?php echo $random_hash; ?>
Content-Type: text/plain; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit

<!--Place your code here for plain text email-->

First Name: <?php echo $firstname; ?>\n
Last Name: <?php echo $lastname; ?>\n
Email Address: <?php echo $email; ?>\n
Telephone: <?php echo $telephone; ?>\n
Mobile: <?php echo $mobile; ?>\n\n
Question: <?php echo $question; ?>

<!--------------------------------------->

--PHP-alt-<?php echo $random_hash; ?>
Content-Type: text/html; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit

<!--Place your code here for HTML email-->

<img src="path to an image here">
<p align="left" >First Name: <b><?php echo $firstname; ?></b><br>
Last Name: <b><?php echo $lastname; ?></b><br>
Email Address: <b><?php echo $email; ?></b><br>
Telephone: <b><?php echo $telephone; ?></b><br>
Mobile: <b><?php echo $mobile; ?></b></p>
<p>Question: <b><?php echo $question; ?></b></p>

<!--------------------------------------->

--PHP-alt-<?php echo $random_hash; ?>--
<?php
//copy current buffer contents into $message variable and delete current output buffer
$message = ob_get_clean();
//send the email
$mail_sent = @mail( $to, $subject, $message, $headers );
//if the message is sent successfully print "Mail sent". Otherwise print "Mail failed"
echo $mail_sent ? "Mail sent" : "Mail failed";

}elseif($submitted){//if required fields not filled in redirect to error page
header("location:pathtoredirectedpagehere.php");
exit;
}


?>

You can add any HTML mark up to display the email how you like. I have put in some simple stuff as an example.

I hope this helps someone, any questions post and I'll try to explain it.

Regards, Paul