PHPMailer / PHPMailer

The classic email sending library for PHP
GNU Lesser General Public License v2.1
20.93k stars 9.71k forks source link

Website registration verification email is rejected by gmail #2843

Open kootrya opened 1 year ago

kootrya commented 1 year ago

The website built on the Linux system uses phpmailer to use gmail as the registration verification code to send the email, but it will still be rejected by other gmail mailboxes. Checking the log shows that the email is sent on behalf (SPF and DKIM are not passed), and the gmail error is 550 7.5. 26, but I have already done the deployment of gmail according to the tutorial, how to deal with it. thanks~

==============error log============ host gmail-smtp-in.l.google.com[142.250.138.26] said: 550-5.7.26 This message does not pass authentication checks (SPF and DKIM both 550-5.7.26 do not pass). SPF check for [aspades.xyz] does not pass with ip: 550-5.7.26 [75.127.7.127].To best protect our users from spam, the message has 550-5.7.26 been blocked. Please visit 550-5.7.26 https://support.google.com/mail/answer/81126#authentication for more 550 5.7.26 information. 23-20020aca1117000000b00354eb46fddfsi4821773oir.110 - gsmtp (in reply to end of DATA command)

Synchro commented 1 year ago

Without seeing your code I can't tell what you're doing wrong.

kootrya commented 1 year ago
function send_mail($mailto, $subject = '', $body = '') {
    if ($GLOBALS['_CFG']['mail_service'] && file_exists(ROOT_PATH . '/include/mail.class.php')) {
        include_once (ROOT_PATH . 'include/mail.class.php');    
        include_once (ROOT_PATH . 'include/smtp.class.php');            
        $mail = new PHPMailer();
        $mail->IsSMTP();
        $mail->IsHTML(true);
        $mail->SMTPAuth = true;
        $mail->SMTPSecure = 'ssl';
        $mail->CharSet = 'utf-8';
        $mail->Encoding = 'base64';
        $mail->Host = $GLOBALS['_CFG']['mail_host'];     //SMTP server address  
        $mail->Port = $GLOBALS['_CFG']['mail_port'];    //SMTP server port   
        $mail->Username = $GLOBALS['_CFG']['mail_username']; //authentication username
        $mail->Password = $GLOBALS['_CFG']['mail_password'];   //authentication password
        $mail->AddReplyTo($GLOBALS['_CFG']['mail_username'],$GLOBALS['_CFG']['site_name']);//Reply address
        $result = array('result'=>false);
        if(filter_var($mailto, FILTER_VALIDATE_EMAIL)){
            $mail->Subject = $subject;//Mail title  
            $mail->Body = $body;
            $mail->SetFrom($GLOBALS['_CFG']['mail_username'],$GLOBALS['_CFG']['site_name']);//Set sender
            $mail->AddAddress($mailto);//Add recipient  
            $result = $mail->Send();
            $mail->ClearAddresses();
        }
        if($result['result']) {
            return true;
        }else{
            return $result;
        }
    } else {
        $subject = "=?UTF-8?B?".base64_encode($subject)."?=";          // Solve the problem of garbled characters in the email subject, UTF8 encoding format
        $header  = "From: ".$GLOBALS['_CFG']['site_name']." <".$GLOBALS['_CFG']['email'].">\n";
        $header .= "Return-Path: <".$GLOBALS['_CFG']['email'].">\n";   // Prevent being treated as spam
        $header .= "MIME-Version: 1.0\n";
        $header .= "Content-type: text/html; charset=utf-8\n";         // The email content is utf-8 encoded
        $header .= "Content-Transfer-Encoding: 8bit\r\n";              // Pay attention to the end of the header, only this is followed by \r
        ini_set('sendmail_from', $GLOBALS['_CFG']['email']);           // Solve a bug in mail
        $body = wordwrap($body, 70);                                   // Up to 70 characters per line, this is the limit of the mail method
        if (mail($mailto, $subject, $body, $header))
            return ture;
    }
}
kootrya commented 1 year ago

smtp.gmail.com ssl 465

Gmail secondary verification, SPF, DKIM, etc. have passed the Google MX tool test, and currently try to modify /etc/hosts and /etc/hostname

Synchro commented 1 year ago

You're doing some very odd things. I don't know why you are renaming PHPMailer's files; that just creates a maintenance hassle for you. You're using include_once, which is a bit pointless – it will fail later if it can't find the classes, so better to fail at the point when the problem occurs. A better idea would be to use composer anyway.

You don't need to add a reply-to if it's the same as your from address - PHPMailer will ignore it if you do that anyway, so you can delete the call to addReplyTo.

You're doing extra validation on $mailto, when PHPMailer already does that in addAddress, so you can check its return value. You're not using PHPMailer's DKIM, so PHPMailer can have nothing to do with failing DKIM signatures.

If you want to send email from a gmail address, you must send through google's servers. You say you're doing that, however, I see that you're using conventional password auth, which gmail no longer allows. Can you post debug output with $mail->SMTPDebug = 3 set?

kootrya commented 1 year ago

ok, i'll take care of it, thanks~

kirilinko commented 1 year ago

It could be that you lack some permissions in the mailbox. Did you use the key that Gmail generates for the account access?