Jump to content


Photo

Trying to integrate PHPmailer into working contact form

mail form

  • Please log in to reply
4 replies to this topic

#1 onjegolders

onjegolders

    Hero Member

  • Members
  • PipPipPipPipPip
  • 806 posts
  • 212

  • LocationMidlands, UK

Posted 22 April 2012 - 06:41 AM

Hi guys, sorry to be bugging you, was just wondering if anyone with more PHP experience may be able to see a line or two in my code which is stopping the form from working.

It's essentially a working contact form that Ryan helped me with to which I'm trying to integrate PHPMailer as I was having problems receiving mail natively.

Here is my code:


<?php

include("./header.inc");


$success_message = "<h3 class='success'>Thank you, your message has been sent.</h3>";
$success = false; // we assume it and set to true if mail sent
$error = false;

// set and sanitize our form field values

$form = array(
  'name' => $input->post->name,
  'email' => $input->post->email,
  'phone' => $input->post->phone,
  'subject' => $input->post->subject,
  'level' => $input->post->level,
  'hours' => $input->post->hours,
  'message' => $input->post->message
);

$required_fields = array(
  'name' => $input->post->name,
  'email' => $input->post->email,
  'subject' => $input->post->subject,
  'level' => $input->post->level,
);


// check if the form was submitted
if($input->post->submit) {

  // determine if any fields were ommitted or didn't validate
  foreach($required_fields as $key => $value) {
	if( trim($value) == '' ) {
	  $error_message = "<h3 class='error'>Please check that you have completed all the required fields.</h3>";
	  $error = true;
	}
  }
  // if no errors, email the form results
  if(!$error) {
	$to_name = "To name";
	$to = "recipient@email.com";
	$subject = "Contact Form Request";
	$from_name = "From Name";
	$from = "sender@email.com";
	foreach($form as $key => $value) $message .= "$key: $value\n";

	require_once("./scripts/PHPMailer/class.phpmailer.php");
	require_once("./scripts/PHPMailer/class.smtp.php");

	$mail = new PHPMailer();

	  $mail->IsSMTP(); // telling the class to use SMTP
	  $mail->SMTPAuth   = true;					 // enable SMTP authentication
	  $mail->SMTPSecure = "ssl";					// sets the prefix to the servier
	  $mail->Host	   = "smtp.gmail.com";		 // sets GMAIL as the SMTP server
	  $mail->Port	   = 465;					  // set the SMTP port for the GMAIL server
	  $mail->Username   = "username";	// GMAIL username
	  $mail->Password   = "password";   // GMAIL password

	  $mail->FromName = $from_name;
	  $mail->From = $from;
	  $mail->AddAddress($to, $to_name);
	  $mail->Subject = $subject;
	  $mail->Body = $message;

	  $mail->Send();

	if ($mail->Send()) {
	  $success = true;
	}
  }
}
?>



<?php if(!$success) { ?>

  <?php

  if($error) {
	echo $error_message; // or pull from a PW field
  }
  ?>

  <p id="contact_header">If you'd like to get in touch regarding tuition, please use the form below.</p>

  <div id="contact_aside">
	<h6>Or you can find me, here: </h6>
	<ul>
	 <li>Skype: <?php echo $page->skype_name; ?></li>
	 <li><a href="<?php echo $page->fb_url; ?>" target="_blank">Facebook</a></li>
	 <?php if ($page->twitter_url) {
		  echo "<li><a href='$page->twitter_url' target='_blank'>Twitter</a></li>";
	 } ?>
	</ul>
  </div><!-- /#contact_aside -->

  <div class="clear"></div><!-- /.clear -->

  <form action="./" method="post" id="contact_form">

	<fieldset class="first">
	 <h6>About yourself: </h6>
	
	 <label for="name">Your name: (required) </label>
	 <input type="text" name="name" autofocus>
	
	 <label for="email">Your email: (required) </label>
	 <input type="email" name="email">
	
	 <label for="phone">Your phone number: </label>
	 <input type="phone" name="phone">
	</fieldset>
	<fieldset>
	 <h6 class="mt60">Your tuition: </h6>
		  
			<label for="subject">Subject: </label>
			<select name="subject" id="subject" />
			<?php
			foreach ($page->subject_options as $subject) {
			 echo "<option>$subject->subject_option</option>";
			}
		   ?>
			</select>
		  
			<label for="level">Level: </label>
			<select name="level" id="level" />
			<?php
			foreach ($page->level_options as $level) {
			 echo "<option>$level->level_option</option>";
			}
		   ?>
			</select>
			
			<label for="hours">Hours per week: </label>
			<input type="number" min="1" max="40" value="2" name="hours">
	</fieldset>
	<div class="clear"></div><!-- /.clear -->
	
	 <fieldset class="full">
		  <label for="message">Additional information: </label>
		  <textarea name="message" cols="30" rows="10"></textarea>
		  <input type="submit" name="submit" value="submit" id="submit">
	 </fieldset>
  </form>




<div class="clear"></div><!-- /.clear -->


<?php }  else {

  echo $success_message; ?>
  
  <p>Please click <a href="<?php echo $config->urls->root; ?>">here</a> if you would like to return to the homepage.</p>

  <?php } ?>


<?php include("./footer.inc"); ?>

At present when the form is submitted, the page seems to think about something for a while then the page gets reloaded exactly the same as before.

Thanks in advance if you have any ideas.

#2 ryan

ryan

    Hero Member

  • Administrators
  • 5,780 posts
  • 3125

  • LocationAtlanta, GA

Posted 23 April 2012 - 11:52 AM

I don't have experience with PHPmailer, but would guess that the $mail->Send() is failing, since $success never gets set to true. I think the best bet here is to figure out how to retrieve the error message from PHPmailer, as I think that would likely answer the question. Also want to mention that when you finish this up, be sure to run all the fields in your $form and $required_fields through a sanitizer. Particularly any that can appear in email headers like Subject or From.

#3 onjegolders

onjegolders

    Hero Member

  • Members
  • PipPipPipPipPip
  • 806 posts
  • 212

  • LocationMidlands, UK

Posted 23 April 2012 - 01:01 PM

Thanks, I think when I get bogged down in things like this, I may start again and go through step by step, better to do it that way and you always end up wiser at the end!

#4 ryan

ryan

    Hero Member

  • Administrators
  • 5,780 posts
  • 3125

  • LocationAtlanta, GA

Posted 24 April 2012 - 09:18 AM

I have a feeling that your code was okay, and it was just PHPMailer that was failing... perhaps because one of the options needs to be changed, like SSL or password or something like that. That's why I think getting a look at whatever error message it is producing would probably answer it.

#5 Martijn Geerts

Martijn Geerts

    Sr. Member

  • Members
  • PipPipPipPip
  • 373 posts
  • 168

Posted 17 July 2012 - 12:47 PM

Are you sure it's port 465 and not 587 ?





Also tagged with one or more of these keywords: mail, form

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users