PHPMailer Problem

+1 Jacob Beck · March 5, 2015

$errors = '';

$user_name = $_POST['user_name']; 
$user_email = $_POST['user_email'];
$user_reason = $_POST['User_reason'];
$user_message = $_POST['User_message'];

$message = "You have received a new message. ".
" Here are the details:\n Name: $user_name \n Email: $user_email \n Reason: $user_reason \n Message: \n $user_message"; 

if(empty($user_name) || empty($user_email) || empty($user_reason) || empty($user_message))
$errors .= "\n Error: all fields are required";

require_once 'PHPMailer/PHPMailerAutoload.php';

$email_form = new PHPMailer;

$email_form->SMTPAuth = true;
$email_form->SMTPDebug = 0;

$email_form->Host = '';
$email_form->Username = '*****************************';
$email_form->Password = '*****************************';
$email_form->SMTPSecure = 'ssl';
$email_form->Port = 465;

$email_form->From = $user_email;
$email_form->FromName = $user_name;
$email_form->addReplyTo($user_email, $user_name);


$email_form->Subject = 'Contact form submission: $user_name';
$email_form->Body = $message;
$email_form->AltBody = $message;

$email_form->WordWrap = 50;


if (!$email_form->send())
echo "Message could not be sent. <p>";
echo "Mailer Error: " . $email_form->ErrorInfo;
header('Location: name of location');


<!DOCTYPE html> 
<title>Contact form handler</title>

<!-- This page is displayed only if there is some error -->
echo nl2br($errors);


It does not give me my $email_form->ErrorInfo, it just redirects me my to file.php

Post a Reply


Oldest  Newest  Rating
0 Mohamad Mirohamadi · March 5, 2015
you a duplicate $email_form->send();

check this:
0 Mohamad Mirohamadi · March 5, 2015
I just checked your code now ,  and I realized you didn't add any address to send your mail to.
add the following line to your code:
$email_form->addAddress('', 'John Doe');
0 Jacob Beck · March 5, 2015
OOOO dang you are completely right. so the if (!$email_form->send()) statement  sends it.
0 Jacob Beck · March 5, 2015
Yea neither of those fixed it for me. Is this PHPMailer only good to send emails to yourself and not from a form.
0 Mohamad Mirohamadi · March 6, 2015
SMTP needs accurate times, and the PHP time zone MUST be set:

Also try :
$email_form->SMTPSecure = 'tls';

replace this lines:
$email_form->From = $user_email;
$email_form->FromName = $user_name;
$email_form->addReplyTo($user_email, $user_name);


$mail->setFrom($user_email, $user_name);
$mail->addReplyTo($user_email, $user_name);
$mail->addAddress('', 'John Doe');

This statement will trying to send mail and return false if it failed


Sorry I don't have experience in PHPMailer, just trying to help.
0 Jacob Beck · March 7, 2015
Its cool, I don't either. I did how ever find out what the problem was. there was 2. The first 1 was since i was using PHP5.5 I needed to include class.phpmailer.php. The require_once 'PHPMailer/PHPMailerAutoload.php' only works on older versions(below PHP5.3). The second 1 was I had to reset my password on my gmail account. The first time I used it, gmail blocked it. changed password and all good. 
  • 1



Server-side, HTML embedded scripting language used to create dynamic Web pages.