PHPMailer - simple question, i think.

Discussion in 'PHP' started by Bmathers, May 9, 2010.

  1. #1
    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.
     
    Bmathers, May 9, 2010 IP
  2. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #2
    danx10, May 9, 2010 IP
  3. Bmathers

    Bmathers Peon

    Messages:
    11
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    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:
     
    Bmathers, May 9, 2010 IP
  4. Bmathers

    Bmathers Peon

    Messages:
    11
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
          <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):
     
    Bmathers, May 9, 2010 IP
  5. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #5
    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:
     
    danx10, May 9, 2010 IP
  6. Bmathers

    Bmathers Peon

    Messages:
    11
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    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.
     
    Bmathers, May 9, 2010 IP
  7. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #7
    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.
     
    danx10, May 9, 2010 IP
  8. Bmathers

    Bmathers Peon

    Messages:
    11
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    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
     
    Bmathers, May 9, 2010 IP
  9. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #9
    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:
     
    danx10, May 9, 2010 IP
  10. Bmathers

    Bmathers Peon

    Messages:
    11
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #10
    I now get please enter phone
    Please enter citty errors
     
    Bmathers, May 9, 2010 IP
  11. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #11
    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.
     
    danx10, May 9, 2010 IP
  12. Bmathers

    Bmathers Peon

    Messages:
    11
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #12
    i know i dont know php but im not that stupid, ;) they are filled in!

    How strange!
     
    Bmathers, May 9, 2010 IP
  13. Bmathers

    Bmathers Peon

    Messages:
    11
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #13
    Tried again and still getting the same message. Try it at m9media.co.uk/contacts.htm
     
    Bmathers, May 9, 2010 IP
  14. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #14
    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:
     
    danx10, May 9, 2010 IP
  15. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #15
    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?
     
    danx10, May 9, 2010 IP
  16. Bmathers

    Bmathers Peon

    Messages:
    11
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #16
    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
     
    Bmathers, May 9, 2010 IP
  17. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #17
    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)
     
    danx10, May 9, 2010 IP
  18. Bmathers

    Bmathers Peon

    Messages:
    11
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #18
    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.
     
    Bmathers, May 9, 2010 IP
  19. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #19
    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 )
     
    danx10, May 9, 2010 IP
  20. Bmathers

    Bmathers Peon

    Messages:
    11
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #20
    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
     
    Bmathers, May 9, 2010 IP