1

I'm a game developer so I do not have any PHP/network/back end web development knowledge. I've been stuck with this problem all week with no luck online. Before PHPMailer, I tried using the mail() function in PHP but I was getting confused with all the .ini files. I found PHPMailer on this site in one of the answers and I have gotten much further. I followed this tutorial https://www.youtube.com/watch?v=FtWD_ZH9lnE&authuser=0 exactly, but the email will not send and debug is not printing out anything. My code always returns false and displays my "can't send email" message.

<?php session_start();

require_once 'PHPMailer/PHPMailerAutoLoad.php';
require_once 'PHPMailer/class.phpmailer.php';

$errors=[];



if(isset($_POST['name'], $_POST['twitterHandle'], $_POST['email'], $_POST['description'])) {

    $fields = [

        'name' => $_POST['name'],
        'twitterHandle' => $_POST['twitterHandle'],
        'email' => $_POST['email'],
        'description' => $_POST['description']

        ];

    foreach($fields as $field => $data){

        if(empty($data)){

            $errors[] = 'The ' . $field . ' field is required';

        }

    }

    //mail connection if all fields are ! empty

    if(empty($errors)){

        $mail = new PHPMailer;

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

        $mail->SMTPDebug = 1;

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

        $mail->isHTML;

        $mail->Subject = 'subject';
        $mail->Body = 'From: ' . $fields['name'] . ' (' . $fields['email'] . ')<p>' . $fields['description'] . '</p>';

        $mail->FromName = 'Contact';

        $mail->addReplyTo($fields['email'], $fields['name']);

        $mail->addAddress('@gmail.com', 'name');

        //send email
        if($mail->send()){

            header('Location: index.php');
            die();

        }else{

        //cannot send
            $errors[] = "Sorry, but I could not process your submission. Please try again.";

        }



    }

} else{

$errors[] = 'Something went wrong';

}

$_SESSION['errors'] = $errors;
$_SESSION['fields'] = $fields;

header('Location: index.php');


?>

I have tried SSL on port 465 and 25 and also TLS on 587. I forwarded 465 and 25 on my network but 587 wont open. IMAP is also enabled on my gmail.

ouflak
  • 2,458
  • 10
  • 44
  • 49
  • 3
    don't spit out a fixed/USELESS error message. phpmailer can TELL you what went wrong. `$errors[] = $mail->ErrorInfo;` – Marc B Aug 04 '15 at 18:59

1 Answers1

0

1). Keep using tls.

2). Most probably you've enabled 2Factor Authentication, you need to go to your gmail settings dashboard and allow your app to authenticate with google. I too had the same problem, which only got resolved when I fixed this setting on google.

Also, have a look at this answer :- send email using Gmail SMTP server through PHP Mailer

Community
  • 1
  • 1
Akshay
  • 2,244
  • 3
  • 15
  • 34
  • Honesty I feel like an idiot right now. I thought I disabled two step verification a few days ago but I might not have applied my changes. I went back and made sure they were applied and it worked. Thank you. – michaelHodge47 Aug 04 '15 at 21:23