duandai6373
2015-12-23 03:51
浏览 69
已采纳

如何使用Gmail SMTP服务器正确设置PHPMailer?

I have a form that basically has 3 radio buttons that will let you pick a staff member you want to get in contact with. The form requires a name, email and message. I cannot find where it tells me what the error is. I have debugging on but do not know where the error is coming from.

My goal is to have it so that the person they select will be emailed and when the email is sent it will be redirected to a page that has the correct staff members info on it. However for the life of me I cannot get this to work. I believe everything works correct but when I inserted the code that I found on SO to connect to the SMTP servers and email the contact form input my formProcess.php breaks.

EDIT: With the help of some of you here I have found the solution to 2 of the errors I am getting. However now that I have fixed these errors I am getting a different error. I am now receiving this:

2015-12-23 04:42:59 SMTP ERROR: Failed to connect to server: Cannot allocate memory (12) 2015-12-23 04:42:59 SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting 2015-12-23 04:42:59 SMTP ERROR: Failed to connect to server: Cannot allocate memory (12) 2015-12-23 04:42:59 SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting

Warning: Cannot modify header information - headers already sent by (output started at /www/contact/phpmailer/class.smtp.php:234) in /www/contact/formProcess.php on line 80

EDIT 2: I have uploaded it to my hosted server. It has solved the error mentioned above. I now have an issue with the password failing, even though I have signed in using the password I am using in the code. Instead of copy/pasting the error message you can see it live on my site for yourself.

EDIT 3: I just noticed that there is an email from Gmail that says "Someone tried to sign in to your Google Account from an app that doesn't meet standard security standards." This can't be a coincidence can it? Is this why I cannot connect? and if so what can I do to meet security standards?

EDIT 4: I have now got everything working fine except for 2 things. 1, for some reason it is sending the emails twice. I am not sure why but I feel like I can figure it out. The real issue I am having now is that I now want to include Googles reCAPTCHA to my form as well. Everything works fine until I added this bit of code that I thought would verify if the reCAPTCHA was successful and if it wasn't just add an error to my code, but after I entered the code below to do that my code breaks.

$curl = curl_init(); 

curl_setopt_array($curl, [
    CURLOPT_RETURNTRANSFER => 1,
    CURLOPT_URL => 'https://www.google.com/recaptcha/api/siteverify',
    CURLOPT_POST => 1,
    CURLOPT_POSTFIELDS => [
       'secret'=>'Well it says secret for a reason!',
       'response'=> $_POST['g-recaptcha-response']
    ]
]);

$response = json_decode(curl_exec($curl));

if (!$response->success){
    $errors[] = 'There was a problem with reCAPTCHA, please try again.';
};

And bellow is all code that processes the form.

<?php

session_start();
ini_set('display_errors', 1); error_reporting(E_ALL);

require_once 'PHPMailerAutoload.php';

$errors = [];
$toWho ='';

if(isset($_POST['name'], $_POST['email'], $_POST['message'],     $_POST['who'])){

$fields = [
  'name' => $_POST['name'],  
  'email' => $_POST['email'],  
  'message' => $_POST['message'],
  'who' => $_POST['who']
];

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

    if ($fields['who'] ==  "staff1") {
    $toWho = 'staff1@domain.com';
} else if ($fields['who'] == "staff2")  {
    $toWho = 'staff2@domain.com';
} else {
    $toWho = 'staff3@domain.com';
}






$curl = curl_init(); 

curl_setopt_array($curl, [
    CURLOPT_RETURNTRANSFER => 1,
    CURLOPT_URL => 'https://www.google.com/recaptcha/api/siteverify',
    CURLOPT_POST => 1,
    CURLOPT_POSTFIELDS => [
       'secret'=>'Well it says secret for a reason!',
       'response'=> $_POST['g-recaptcha-response']
    ]
]);

$response = json_decode(curl_exec($curl));

if (!$response->success){
    $errors[] = 'There was a problem with reCAPTCHA, please try again.';
};









if(empty($errors)) {

    $mail = new PHPMailer;



    $mail->isSMTP();
    $mail->SMTPAuth = true;
    $mail->smtpSecure = 'tls';
    $mail->Port = 587;

//        $mail->SMTPDebug = 3;

    $mail->Host = 'mailen3.cloudsector.net';

    $mail->From = "No-reply@domain.com";
    $mail->Username = 'No-Reply@domain.com';
    $mail->Password = 'PAsswoRD';

    $mail->SetFrom("No-reply@domain.com", "No Reply" );
    $mail->AddReplyTo($fields['email'], $fields['name']);
    $mail->AddAddress($toWho, $fields['who']);

    $mail->Subject    = $fields['name'] . ' wants to talk!';
    $mail->Body       = 'From: ' .$fields['name']. ' (' .$fields['email']. ') ' .$fields['message']. ;




    $mail->send();


    if($mail->send()) {
        header('Location: ../../' .$fields['who']. 'thanks.php');
        die();
    }else {
        $errors[] = 'Sorry! Something went wrong and your message could not be sent. Please try again ';
    }


}


} else {
     $errors[] = 'Something went wrong.';
 }

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

 header('Location: index.php');
 ?>
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dongshou2024 2015-12-23 04:02
    已采纳

    Try displaying errors by adding the following code at the top of your script:

    ini_set('display_errors', 1);
    ini_set('display_startup_errors', 1);
    error_reporting(E_ALL);
    

    If you're getting a server error 500, Try commenting out small blocks of code until the script works. That way you can identify the problem.

    已采纳该答案
    打赏 评论

相关推荐 更多相似问题