I am using phpmailer for a simple contact form, however i need to output more than one item in the body. How can i do this, ive tried lots of different ways but cant get the required output. <?php $name = trim($_POST['name']); $email = $_POST['email']; $phone = $_POST['phone']; $city = $_POST['city']; $comments = $_POST['comments']; $site_owners_email = 'bmavz9@hotmail.com'; // Replace this with your own email address $site_owners_name = 'Planet Red Events'; // replace with your name if (strlen($name) < 2) { $error['name'] = "Please enter your name"; } if (!preg_match('/^[a-z0-9&\'\.\-_\+]+@[a-z0-9\-]+\.([a-z0-9\-]+\.)*+[a-z]{2}/is', $email)) { $error['email'] = "Please enter a valid email address"; } if (strlen($comments) < 3) { $error['comments'] = "Please leave a comment."; } if (!$error) { require_once('phpMailer/class.phpmailer.php'); $mail = new PHPMailer(); $mail->From = $email; $mail->FromName = $name; $mail->Subject = "Website Contact Form"; $mail->AddAddress($site_owners_email, $site_owners_name); $mail->Body = $comments; $mail->Send(); echo "<li class='success'> Congratulations, " . $name . ". We've received your email. We'll be in touch as soon as we possibly can! </li>"; } # end if no error else { $response = (isset($error['name'])) ? "<li>" . $error['name'] . "</li> \n" : null; $response .= (isset($error['email'])) ? "<li>" . $error['email'] . "</li> \n" : null; $response .= (isset($error['comments'])) ? "<li>" . $error['comments'] . "</li>" : null; echo $response; } # end if there was an error sending ?> PHP: I want to add Phone and City to $mail->Body = $comments; but i dont know how to. Any help would be appreciated.
Look at: http://php.net/manual/en/language.operators.string.php and use it on: $mail->Body = $comments; PHP:
Thanks for your reply, that works now. However i cant get the City and phone to output. Can you see a reason why? Below is the html form and the php code ?php $name = trim($_POST['name']); $email = trim($_POST['email']); $phone = trim($_POST['phone']); $city = trim($_POST['city']); $comments = trim($_POST['comments']); $site_owners_email = 'bmathers@m9media.co.uk'; // Replace this with your own email address $site_owners_name = 'Planet Red Events'; // replace with your name if (strlen($name) < 2) { $error['name'] = "Please enter your name"; } if (!preg_match('/^[a-z0-9&\'\.\-_\+]+@[a-z0-9\-]+\.([a-z0-9\-]+\.)*+[a-z]{2}/is', $email)) { $error['email'] = "Please enter a valid email address"; } if (strlen($phone)) { $error['phone'] = "Please enter your phone"; } if (strlen($city)) { $error['city'] = "Please enter your city"; } if (strlen($comments) < 3) { $error['comments'] = "Please leave a comment."; } if (!$error) { require_once('phpMailer/class.phpmailer.php'); $mail = new PHPMailer(); $mail->From = $email; $mail->FromName = $name; $mail->Subject = "Website Contact Form"; $mail->AddAddress($site_owners_email, $site_owners_name); $mail->Body = $comments; $mail->Body .= $city; $mail->Body .= $name; $mail->Body .= $email; $mail->Body .= $phone; $mail->Send(); echo "<li class='success'> Congratulations, " . $name . ". We've received your email. We'll be in touch as soon as we possibly can! </li>"; } # end if no error else { $response = (isset($error['name'])) ? "<li>" . $error['name'] . "</li> \n" : null; $response .= (isset($error['email'])) ? "<li>" . $error['email'] . "</li> \n" : null; $response .= (isset($error['phone'])) ? "<li>" . $error['phone'] . "</li> \n" : null; $response .= (isset($error['city'])) ? "<li>" . $error['city'] . "</li> \n" : null; $response .= (isset($error['comments'])) ? "<li>" . $error['comments'] . "</li>" : null; echo $response; } # end if there was an error sending ?> PHP:
<form method="post" action="sendEmail.php"> <div id="container"> <div id="main"> <p><small>Name:</small> <input type="text" name="name" id="name" /></p> <p><small>Telephone:</small> <input type="text" name="phone" id="phone" /></p> <p><small>Email Address:</small> <input type="text" name="email" id="email" /></p> <p><small>City:</small> <input type="text" name="city" id="city" /></p> <p><small>Questions/Comments:</small> <textarea name="comments" id="comments" rows="8" cols="12"></textarea></p> <p><input type="submit" name="submit" id="submit" value="Email Us!" /></p> <ul id="response" /> </div> Code (markup):
In sendEmail.php add this right at the top - the next line after the opening tag (<?php) (to prevent spam/abuse): function prevent_spam($input){ $bad = array('to:', 'cc:', 'bcc:', 'content_type:', 'mime-version:', 'multipart-mixed:', 'content-transfer-encoding:'); return str_ireplace($bad, '', strip_tags($input)); } $_POST = array_map('prevent_spam', $_POST); PHP: And... Add "\r\n" (which is a new line), before each variable to help distinguish the output... $mail->Body .= "\r\n". $phone; PHP:
Thanks for that, taht helps. I still cant get the city and the phone to output. Same html code but i have used your suggestions so my php now looks like this <?php function prevent_spam($input){ $bad = array('to:', 'cc:', 'bcc:', 'content_type:', 'mime-version:', 'multipart-mixed:', 'content-transfer-encoding:'); return str_ireplace($bad, '', strip_tags($input)); } $_POST = array_map('prevent_spam', $_POST); $name = trim($_POST['name']); $email = trim($_POST['email']); $phone = trim($_POST['phone']); $city = trim($_POST['city']); $comments = trim($_POST['comments']); $site_owners_email = 'bmathers@m9media.co.uk'; // Replace this with your own email address $site_owners_name = 'Planet Red Events'; // replace with your name if (strlen($name) < 2) { $error['name'] = "Please enter your name"; } if (!preg_match('/^[a-z0-9&\'\.\-_\+]+@[a-z0-9\-]+\.([a-z0-9\-]+\.)*+[a-z]{2}/is', $email)) { $error['email'] = "Please enter a valid email address"; } if (strlen($phone)) { $error['phone'] = "Please enter your phone"; } if (strlen($city)) { $error['city'] = "Please enter your city"; } if (strlen($comments) < 3) { $error['comments'] = "Please leave a comment."; } if (!$error) { require_once('phpMailer/class.phpmailer.php'); $mail = new PHPMailer(); $mail->From = $email; $mail->FromName = $name; $mail->Subject = "Website Contact Form"; $mail->AddAddress($site_owners_email, $site_owners_name); $mail->Body = $comments; $mail->Body .= "\r\n".$city; $mail->Body .= "\r\n".$name; $mail->Body .= "\r\n".$email; $mail->Body .= "\r\n".$phone; $mail->Send(); echo "<li class='success'> Congratulations, " . $name . ". We've received your email. We'll be in touch as soon as we possibly can! </li>"; } # end if no error else { $response = (isset($error['name'])) ? "<li>" . $error['name'] . "</li> \n" : null; $response .= (isset($error['email'])) ? "<li>" . $error['email'] . "</li> \n" : null; $response .= (isset($error['phone'])) ? "<li>" . $error['phone'] . "</li> \n" : null; $response .= (isset($error['city'])) ? "<li>" . $error['city'] . "</li> \n" : null; $response .= (isset($error['comments'])) ? "<li>" . $error['comments'] . "</li>" : null; echo $response; } # end if there was an error sending ?> PHP: I dont understand why the phone and city are not being outputted in the email taht is being sent. the name, email and comments are working fine.
Add to the top of sendEmail.php: error_reporting(E_ALL); PHP: and add at the bottom: //output the $_POST array to see whats getting submitted... print_r($_POST); PHP: Then reply if any errors occur along with a copy of the output array.
Notice: Undefined index: phone in /customers/m9media.co.uk/m9media.co.uk/httpd.www/sendEmail.php on line 12 Notice: Undefined index: city in /customers/m9media.co.uk/m9media.co.uk/httpd.www/sendEmail.php on line 13 Notice: Undefined variable: error in /customers/m9media.co.uk/m9media.co.uk/httpd.www/sendEmail.php on line 41 Deprecated: Function split() is deprecated in /customers/m9media.co.uk/m9media.co.uk/httpd.www/phpMailer/class.phpmailer.php on line 472
Update sendEmail.php with: <?php function prevent_spam($input) { $bad = array('to:', 'cc:', 'bcc:', 'content_type:', 'mime-version:', 'multipart-mixed:', 'content-transfer-encoding:'); return str_ireplace($bad, '', strip_tags($input)); } $_POST = array_map('prevent_spam', $_POST); $_POST = array_map('trim', $_POST); $name = $_POST['name']; $email = $_POST['email']; $phone = $_POST['phone']; $city = $_POST['city']; $comments = $_POST['comments']; // Replace this with your own email address $site_owners_email = 'bmathers@m9media.co.uk'; // replace with your name $site_owners_name = 'Planet Red Events'; $error = NULL; if (strlen($name) < 2) { $error['name'] = "Please enter your name"; } if (!preg_match('/^[a-z0-9&\'\.\-_\+]+@[a-z0-9\-]+\.([a-z0-9\-]+\.)*+[a-z]{2}/is', $email)) { $error['email'] = "Please enter a valid email address"; } if (strlen($phone) < 2) { $error['phone'] = "Please enter your phone"; } if (strlen($city) < 2) { $error['city'] = "Please enter your city"; } if (strlen($comments) < 3) { $error['comments'] = "Please leave a comment."; } if (is_null($error)) { require_once('phpMailer/class.phpmailer.php'); $mail = new PHPMailer(); $mail->From = $email; $mail->FromName = $name; $mail->Subject = "Website Contact Form"; $mail->AddAddress($site_owners_email, $site_owners_name); $mail->Body = $comments; $mail->Body .= "\r\n" . $city; $mail->Body .= "\r\n" . $name; $mail->Body .= "\r\n" . $email; $mail->Body .= "\r\n" . $phone; $mail->Send(); echo "<li class='success'> Congratulations, " . $name . ". We've received your email. We'll be in touch as soon as we possibly can! </li>"; } // end if no error else { $response = (isset($error['name'])) ? "<li>" . $error['name'] . "</li> \n" : null; $response .= (isset($error['email'])) ? "<li>" . $error['email'] . "</li> \n" : null; $response .= (isset($error['phone'])) ? "<li>" . $error['phone'] . "</li> \n" : null; $response .= (isset($error['city'])) ? "<li>" . $error['city'] . "</li> \n" : null; $response .= (isset($error['comments'])) ? "<li>" . $error['comments'] . "</li>" : null; echo $response; } // end if there was an error sending ?> PHP:
Thats because your not filling the form fields in for telephone and city. I've just tested the code i've just submitted and it works fine aslong as all the fields are filled in.
Your not stupid and I wern't calling you stupid. I've just tested the code on localhost refer to the following: <form method="post"> <p><small>Name:</small> <input type="text" name="name" id="name" /></p> <p><small>Telephone:</small> <input type="text" name="phone" id="phone" /></p> <p><small>Email Address:</small> <input type="text" name="email" id="email" /></p> <p><small>City:</small> <input type="text" name="city" id="city" /></p> <p><small>Questions/Comments:</small> <textarea name="comments" id="comments" rows="8" cols="12"></textarea></p> <p><input type="submit" name="submit" id="submit" value="Email Us!" /></p> </form> <?php function prevent_spam($input) { $bad = array('to:', 'cc:', 'bcc:', 'content_type:', 'mime-version:', 'multipart-mixed:', 'content-transfer-encoding:'); return str_ireplace($bad, '', strip_tags($input)); } if (isset($_POST['submit'])){ $_POST = array_map('prevent_spam', $_POST); $name = trim($_POST['name']); $email = trim($_POST['email']); $phone = trim($_POST['phone']); $city = trim($_POST['city']); $comments = trim($_POST['comments']); // Replace this with your own email address $site_owners_email = 'bmathers@m9media.co.uk'; // replace with your name $site_owners_name = 'Planet Red Events'; $error = NULL; if (strlen($name) < 2) { $error['name'] = "Please enter your name"; } if (!preg_match('/^[a-z0-9&\'\.\-_\+]+@[a-z0-9\-]+\.([a-z0-9\-]+\.)*+[a-z]{2}/is', $email)) { $error['email'] = "Please enter a valid email address"; } if (strlen($phone) < 2) { $error['phone'] = "Please enter your phone"; } if (strlen($city) < 2) { $error['city'] = "Please enter your city"; } if (strlen($comments) < 3) { $error['comments'] = "Please leave a comment."; } if (is_null($error)) { require_once('phpMailer/class.phpmailer.php'); $mail = new PHPMailer(); $mail->From = $email; $mail->FromName = $name; $mail->Subject = "Website Contact Form"; $mail->AddAddress($site_owners_email, $site_owners_name); $mail->Body = $comments; $mail->Body .= "\r\n" . $city; $mail->Body .= "\r\n" . $name; $mail->Body .= "\r\n" . $email; $mail->Body .= "\r\n" . $phone; $mail->Send(); echo "<li class='success'> Congratulations, " . $name . ". We've received your email. We'll be in touch as soon as we possibly can! </li>"; } // end if no error else { $response = (isset($error['name'])) ? "<li>" . $error['name'] . "</li> \n" : null; $response .= (isset($error['email'])) ? "<li>" . $error['email'] . "</li> \n" : null; $response .= (isset($error['phone'])) ? "<li>" . $error['phone'] . "</li> \n" : null; $response .= (isset($error['city'])) ? "<li>" . $error['city'] . "</li> \n" : null; $response .= (isset($error['comments'])) ? "<li>" . $error['comments'] . "</li>" : null; echo $response; } // end if there was an error sending } ?> PHP:
Your not stupid and I wern't calling you stupid. I've just tested the code on localhost refer to the following: <form method="post"> <p><small>Name:</small> <input type="text" name="name" id="name" /></p> <p><small>Telephone:</small> <input type="text" name="phone" id="phone" /></p> <p><small>Email Address:</small> <input type="text" name="email" id="email" /></p> <p><small>City:</small> <input type="text" name="city" id="city" /></p> <p><small>Questions/Comments:</small> <textarea name="comments" id="comments" rows="8" cols="12"></textarea></p> <p><input type="submit" name="submit" id="submit" value="Email Us!" /></p> </form> <?php function prevent_spam($input) { $bad = array('to:', 'cc:', 'bcc:', 'content_type:', 'mime-version:', 'multipart-mixed:', 'content-transfer-encoding:'); return str_ireplace($bad, '', strip_tags($input)); } if (isset($_POST['submit'])){ $_POST = array_map('prevent_spam', $_POST); $name = trim($_POST['name']); $email = trim($_POST['email']); $phone = trim($_POST['phone']); $city = trim($_POST['city']); $comments = trim($_POST['comments']); // Replace this with your own email address $site_owners_email = 'bmathers@m9media.co.uk'; // replace with your name $site_owners_name = 'Planet Red Events'; $error = NULL; if (strlen($name) < 2) { $error['name'] = "Please enter your name"; } if (!preg_match('/^[a-z0-9&\'\.\-_\+]+@[a-z0-9\-]+\.([a-z0-9\-]+\.)*+[a-z]{2}/is', $email)) { $error['email'] = "Please enter a valid email address"; } if (strlen($phone) < 2) { $error['phone'] = "Please enter your phone"; } if (strlen($city) < 2) { $error['city'] = "Please enter your city"; } if (strlen($comments) < 3) { $error['comments'] = "Please leave a comment."; } if (is_null($error)) { require_once('phpMailer/class.phpmailer.php'); $mail = new PHPMailer(); $mail->From = $email; $mail->FromName = $name; $mail->Subject = "Website Contact Form"; $mail->AddAddress($site_owners_email, $site_owners_name); $mail->Body = $comments; $mail->Body .= "\r\n" . $city; $mail->Body .= "\r\n" . $name; $mail->Body .= "\r\n" . $email; $mail->Body .= "\r\n" . $phone; $mail->Send(); echo "<li class='success'> Congratulations, " . $name . ". We've received your email. We'll be in touch as soon as we possibly can! </li>"; } // end if no error else { $response = (isset($error['name'])) ? "<li>" . $error['name'] . "</li> \n" : null; $response .= (isset($error['email'])) ? "<li>" . $error['email'] . "</li> \n" : null; $response .= (isset($error['phone'])) ? "<li>" . $error['phone'] . "</li> \n" : null; $response .= (isset($error['city'])) ? "<li>" . $error['city'] . "</li> \n" : null; $response .= (isset($error['comments'])) ? "<li>" . $error['comments'] . "</li>" : null; echo $response; } // end if there was an error sending } ?> PHP: Anyone else can verify that it works?
Ive just copied your php code from above and now it doesnt even send an email, did you change something. Ive looked through and cant see any changes
The code works perfectly fine on my end must be something else beside the code then... Ensure you have the latest PHPMailer installed. (http://phpmailer.worxware.com/index.php?pg=phpmailer)
Do you know where i can get the latest version from, although i dont think thats it as it has/does work but just misses fields out. Is the code above the exact code your using? Thanks for all your help by the way.
The code above is the exact code im using. Heres a direct download link to the latest phpmailer which works on php 5 & 6 ( http://sourceforge.net/project/showfiles.php?group_id=26031&package_id=252700 )
Dan, thanks for your help. Ive tried creating a new html page and it works with just the html, it must be something else that stopping it. But thanks for your help