-1

I've been using phpmailer for a contact form recently, but when I try to send the contact form to the email it results into an error. Any help will be appreciated! Keep in mind that this is using xampp localhost. Also my contact.php redirects to another page which ma My error is

2016-02-21 00:02:19 Invalid address: (addAnAddress Reply-To): 11 Notice: Undefined property: PHPMailer::$send in C:\xampp\htdocs\Guild Website\contact.php on line 51

Contact.php script is also -

<?php
ini_set('display_errors', 1); ini_set('display_startup_errors', 1); error_reporting(E_ALL);
session_start();

require_once 'libs/phpmailer/PHPMailerAutoload.php';

$errors = [];

if(isset($_POST['name'], $_POST['email'], $_POST['ign'], $_POST['gamepref'], $_POST['cps'], $_POST['anythingelse'])) {

$fields = [
    'name' => $_POST['name'],
    'e-mail' => $_POST['email'],
    'IGN' => $_POST['ign'],
    'game preference' => $_POST['gamepref'],
    'CPS' => $_POST['cps'],
    'anything else' => $_POST['anythingelse'],
];

foreach($fields as $field => $data) {
    if(empty($data)) {
        $errors[] = "The $field field is required."; 
    }
}

if(empty($errors)) {

    $mail = new PHPMailer;

    $mail->isSMTP();
    $mail->SMTPAuth = true;
    $mail->SMTPDebug = 2;

    $mail->Host = 'smtp.gmail.com';
    $mail->Username = 'somemail@gmail.com';
    $mail->Password = 'pass';
    $mail->SMTPSecure = 'ssl';
    $mail->Port = 465;

    $mail->isHTML();

    $mail->Subject = '$fields[\'name\'] is wanting to apply.';
    $mail->Body = '<p>Name: $fields[\'name\']</p><p>E-Mail: $fields[\'e-mail\']</p><p>IGN: $fields[\'IGN\']</p><p>Game Preference: $fields[\'game preference\']</p><p>CPS: $fields[\'CPS\']</p><p>Message: $fields[\'anything else\']</p>';

    $mail->FromName = 'Contact';

    $mail->AddReplyTo($fields['e-mail'], $fields['name']);

    $mail->AddAddress('somemail@gmail.com');

    if($mail->send) {
        header('Location: thanks.php');
        die();
    } else {
        $errors[] = 'Sorry, could not send email. Try again later.';
    }
}

} else {
$errors[] = "Something went wrong! (Mabye it is because you didn't put anything!)";
}
$_SESSION['errors'] = $errors;
$_SESSION['fields'] = $fields;
  • What is the error you are receiving? – IeuanG Feb 20 '16 at 23:22
  • How do I check? I tried using $mai;->SMTPDebug = 2; but I have no idea how to view the message. – krcoolgamingFTW Feb 20 '16 at 23:44
  • Put this at the top of your file: ini_set('display_errors', 1); ini_set('display_startup_errors', 1); error_reporting(E_ALL); – IeuanG Feb 20 '16 at 23:47
  • Ok, and do you have any errors in your $SESSION['errors'] variable? – IeuanG Feb 21 '16 at 00:00
  • Thanks for the help so far! I actually just cancelled the redirection to the page error.php, and came with the error of: 2016-02-21 00:02:19 Invalid address: (addAnAddress Reply-To): 11 Notice: Undefined property: PHPMailer::$send in C:\xampp\htdocs\Guild Website\contact.php on line 51 – krcoolgamingFTW Feb 21 '16 at 00:04
  • Shouldn't $mail->send be $mail->Send()? – IeuanG Feb 21 '16 at 00:11
  • Just tried that, but I return with the error: 2016-02-21 00:19:02 Invalid address: (addAnAddress Reply-To): 11 2016-02-21 00:19:06 SMTP ERROR: Failed to connect to server: (0) 2016-02-21 00:19:06 SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting by the ways, thank you so much for the help! I'm glad to see that this community is great. – krcoolgamingFTW Feb 21 '16 at 00:20
  • Looking at similar questions, try the solution to [this](http://stackoverflow.com/questions/10396264/phpmailer-reply-using-only-reply-to-address) – IeuanG Feb 21 '16 at 00:25
  • That doesn't seem to help. I put debugging set to 4 and am starting to get the error of - 2016-02-21 00:31:02 Connection: opening to ssl://smtp.gmail.com:465, timeout=300, options=array ( ) 2016-02-21 00:31:02 SMTP ERROR: Failed to connect to server: (0) 2016-02-21 00:31:02 SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting – krcoolgamingFTW Feb 21 '16 at 00:32
  • My last shot at it, try port 25 – IeuanG Feb 21 '16 at 00:38
  • That didn't work, but thank's for the help! Have a good night/day. – krcoolgamingFTW Feb 21 '16 at 00:43
  • `send` is a function, not a variable, and you're getting an invalid address error because you're giving it an invalid address. Don't redirect while debugging, it just gets in the way. – Synchro Feb 22 '16 at 10:31

1 Answers1

0

Just found out a solution! First I switched over to tls, then I added this - $mail->SMTPOptions = array( 'ssl' => array( 'verify_peer' => false, 'verify_peer_name' => false, 'allow_self_signed' => true ) and finally allowed less secure apps for gmail.