PHPMailer not sending e-mail through Gmail, returns blank page

635 Views Asked by At

I am trying to set up a contact form at the bottom of my page. I am using PHPMailer and trying to receive emails at my gmail account. Every time I submit the form, my url changes to url/email.php and I get a blank page. What am I doing wrong? Can anyone see the glaring error that I am obviously missing?

HTML:

<div class="container-fluid">
        <form action="email.php" id="contact-me" method="post" name="contact-me">
          <div class="row-fluid">
            <div class="form-group">
              <div class="col-md-3 col-md-offset-3">
                <input class="input" name="name" id="name" placeholder="Name" type="text">
              </div>
              <div class="col-md-3">
                <input class="input" id="email" name="email"
                placeholder="Email" type="text">
              </div>
            </div>
          </div>
            <div class="form-group">
                <textarea class="input input-textarea" id="message" name="message" placeholder="Type your message here" rows=
                "5"></textarea>
            </div>
            <input name="send" class="send" type="submit" value="Get In Touch">
        </form>
  </div>
</section>

PHP:

<?php
if (isset($_POST['submit']))
{
require('PHPMailer-master/PHPMailerAutoload.php');
require_once('PHPMailer-master/class.smtp.php');
include_once('PHPMailer-master/class.phpmail.php');

$name = strip_tags($_Post['name']);
$email = strip_tags($_POST['email']);
$msg = strip_tags($_POST['message']);
$subject = "Message from Portfolio";

$mail = new PHPMailer();
$mail->isSMTP();
$mail->CharSet = 'UTF-8';
$mail->Host = 'smtp.gmail.com';
$mail->SMTPAuth = true;
$mail->SMTPSecure = 'tls';                            
$mail->Port = 587;     
$mail->Username = '[email protected]';       
$mail->Password = '***********';

$mail->From = $email;
$mail->FromName = $name;

$mail->AddAddress("me.com", "Katia Sittmann"); 
$mail->AddReplyTo($email, $name);
$mail->isHTML(true); 

$mail->WordWrap = 50;
$mail->Subject =$subject;
$mail->Body = $msg;
$mail->AltBody ="No message entered";

if(!$mail->send()) {
  echo 'Message could not be sent.';
  echo 'Mailer Error: ' . $mail->ErrorInfo;
  exit;
}else{
   header("Location: thank-you.html");
}
}
?>
1

There are 1 best solutions below

1
On BEST ANSWER

You've got some very confused code here. You should start out with an up-to-date example, not an old one, and make sure you're using the latest PHPMailer (at least 5.2.10).

Don't do this:

require('PHPMailer-master/PHPMailerAutoload.php');
require_once('PHPMailer-master/class.smtp.php');
include_once('PHPMailer-master/class.phpmail.php');

All you need is this, which will auto-load the other classes if and when they are needed:

require 'PHPMailer-master/PHPMailerAutoload.php';

This won't work:

$name = strip_tags($_Post['name']);

PHP variables are case-sensitive, so do this:

$name = strip_tags($_POST['name']);

This will cause problems:

$mail->From = $email;

When you do this, you're forging the From address, and it will get rejected by SPF checks. Put your own address in here, and let the reply-to carry the submitter's address, as you're already doing.

Submit may not be included in the submission if the form is not submitted via that control, e.g. if it's submitted by hitting return in a text input. Check for a field that you know will always be in a submission:

if (array_key_exists('email', $_POST)) {...

Adding a try/catch will not achieve anything - PHPMailer does not throw exceptions unless you ask it to, and you are not.

You are applying strip_tags on the body, but then calling $mail->isHTML(true); - do you want HTML or not?

All that said, none of those things will cause a blank page. It's fairly likely you are running into gmail auth problems which are popular lately, and to see that you need to enable debug output:

$mail->SMTPDebug = 2;

and then read the troubleshooting guide on what you see.