PHPMailer Problem

+1 Jacob Beck · March 5, 2015
<?php

if(isset($_POST['submit']))
{
$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->isSMTP();
$email_form->SMTPAuth = true;
$email_form->SMTPDebug = 0;

$email_form->Host = 'smtp.gmail.com';
$email_form->Username = '*****************************@gmail.com';
$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->isHTML(true);

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

$email_form->WordWrap = 50;

$email_form->send();

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

?>

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

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


</body>
</html>





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

Post a Reply

Replies

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

check this:
https://support.google.com/mail/answer/78775?hl=en
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('whoto@example.com', '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:
date_default_timezone_set('Asia/Tehran');


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);


with:

$mail->setFrom($user_email, $user_name);
$mail->addReplyTo($user_email, $user_name);
$mail->addAddress('whoto@example.com', 'John Doe');


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

$email_form->send()


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

PHP

107,295 followers
About

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

Links
Moderators