PDA

View Full Version : Is it possible with PHP mail scripts



Sammie
July 22nd, 2008, 20:32
Hello,

Okay well i have the following php mail script for my form, now i have a message section, what i would like to know is it possible to have the line breaks appear in the mail sent to me, as in if someone said this it would come out the same in the email..



Hey

Can you please test my site for me!

Thanks
Brian

So my script i am using is the following.


<?php
$to['email'] = 'support@sammieservices.net'; # Set to the email address you wish to recieve the mail
$to['name'] = 'Sammie Services - Support'; # Set to the name that is going to recieve the mail
$thankyoupage = 'source/thankyou_email.php'; # Set to the name of a page a client will be shown after submitting the form

$self = basename($_SERVER['PHP_SELF']);

if (isset($_POST['submit'])) {
$errors = array();
if (empty($_POST['name']))
$errors[] = 'Please provide your full name';
if (!eregi("^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$", $_POST['mail']))
$errors[] = 'Please provide a valid email address';
if (empty($_POST['subject']))
$errors[] = 'Please provide a subject';
if (!isset($_POST['msg']{10}))
$errors[] = 'Please provide a message';
if (empty($errors)) {
$headers = 'MIME-Version: 1.0' . "\r\n" .
"Content-type: text/html; charset=iso-8859-1\r\n" .
"From: " . $_POST['name'] . " <" . $_POST['mail'] . ">\r\n" .
'X-Mailer: PHP/' . phpversion();
$body = $to['name'] . ',<br /><br />' . $_POST['name'] . ' has sent you this message:<br /><br />' . $_POST['msg'];
mail($to['name'] . ' <' . $to['email'] . '>',$_POST['subject'],$body,$headers);
include('source/thankyou_contact.php'); # Include a page thanking the client for emailing
} else {
$message = '<p style="color:#e22929">';
foreach ($errors as $error) {
$message .= $error.'<br />';
}
$message .= '<br /></p>';
unset($_POST['submit']); # Unset $_POST['submit'] so the script will display the form again
}
}
if (!isset($_POST['submit']))


{ ?>
<?php if (isset($message)) echo $message;?>

<form action="<?php echo $self; ?>" method="post">
<p><input type='text' class='fieldbox1' name='name' value='Your Name' /></p>
<p><input type='text' class='fieldbox1' name='mail' value='Contact E-Mail Address' /></p>
<p><input type='text' class='fieldbox1' name='subject' value='Subject Of Your Message' /></p>
<p><textarea class='textbox1' rows='0' cols='0' name='msg'>Enter Your Message Here...</textarea></p>
<div class='contact_extra'>
MSN: msn@sammieservices.com<br />
E-Mail: support@sammieservices.com
</div>
<p><input class='submit_contact' type='submit' value='' name='submit' /></p>

</form>

<?php } ?>

Thanks
Sam

Tree
July 22nd, 2008, 23:08
Change $body's declaration to


$body = $to['name'] . ',<br /><br />' . $_POST['name'] . ' has sent you this message:<br /><br />' . str_replace("\n","<br>",$_POST['msg']);

Sammie
July 23rd, 2008, 01:51
Awesome, that works, thanks. :beer:

themoose
July 23rd, 2008, 11:56
Although Tree's would work great, it'd be quicker and probably more efficient (not all new lines are \n), to use nl2br() ($body = $to['name'] . ',<br /><br />' . $_POST['name'] . ' has sent you this message:<br /><br />' . str_replace("\n","<br>",$_POST['msg']); )


$body = $to['name'] . ',<br /><br />' . $_POST['name'] . ' has sent you this message:<br /><br />' . nl2br($_POST['msg']);