phpmailer failed to send e-mail on Alibaba cloud server, Chinese code is garbled

My environment is: apache+mysql+php

First, check whether socket and openssl extensions are enabled

 

 

If not, find the php.ini file and open it. Open this extension

Remove the ';' before the extension=php_openssl.dll

php_sockets.dll 
php_openssl.dll

 

 

Make sure you have these two sentences in the code that you send the message:

$mail->SMTPSecure = 'ssl'; //Set up and use ssl Encryption login authentication
$mail->Port = 465;         //Set up ssl Connect smtp Server's remote server port number 465

 

Add access to port 465 to the firewall

I have been testing normally locally before, but failed to send emails after I changed to Alibaba cloud server

Because alicloud does not have access to port 465 by default

The server opens the 465 security group. The latest alicloud ECS can be configured directly in the security policy
Alibaba cloud management background > ECS > network and Security > Security Group > self configuration

 

 

Restart php and server after completion

The message has been successfully received.

 

To add a small problem, I use the Chinese pop-up of alert to display the garbled code. The local test is Ok

This is the original code:

exit("<script charset='UTF-8'>alert(\"Message successfully!\");location.href='".$_SERVER["HTTP_REFERER"]."'</script>");

And then it's changed to the following sentence, and it's no longer jumbled

echo '<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"><title>Tips</title><script language=javascript>alert("Message successfully!");location.href="'.$_SERVER["HTTP_REFERER"].'"</script></head></html>';

 

Add the situation that the title or content of the email sent by phpmailer or the prompt message are in Chinese disorder

There are many online tutorials, which are not suitable for me

I found out the code by myself and pasted it. You can try it, OK, no, you can find another solution

        //Send message to email
        //Reference to phpmailer core file
        require_once("PHPMailer/PHPMailer.php");
        require_once("PHPMailer/SMTP.php");
        session_start();
        $mail = new PHPMailer\PHPMailer\PHPMailer();

        $mail->isSMTP();// Use SMTP service
        $mail->CharSet = "utf8";// The encoding format is utf8,If the code is not set, the Chinese code will be garbled
        $mail->Host = "smtp.xx.com";// Sender SMTP server address
        $mail->SMTPAuth = true;// Use authentication or not
        $mail->Username = "cyy@qq.com";// Outgoing server mailbox
        $mail->Password = "xxxxxxxx";//Client authorization password,Not the login password of the mailbox!
        $mail->SMTPSecure = "ssl";// Use ssl Protocol mode
        $mail->Port = 465;//  qq Port 465 or 587
        $mail->setFrom("cyy@qq.com", "cyy Hair");// Set sender information, such as sender in message format description
        $mail->addAddress("cyy2@qq.com", "cyy2 Collected");// Set recipient information, such as recipients in the message format description
        $mail->addReplyTo("cyy@qq.com", "cyy Hair"); // Set the replier information, which refers to the email address to which the replying email will be sent if the recipient wants to reply after receiving the email     
        $mail->Subject = "Mail title";// Mail title

        $code=rand(100000,999999);//Verification Code
        $_SESSION["code"] = $code;
        return $code."----".session("code");
        $mail->Body = "The message content is <b>Your verification code is:$code</b>,If I do not need to pay attention to the operation!";
        $mail->Body = "Mail text";
        $mail->AltBody = "This is the plain text Pure text"; // This is the body content displayed in plain text mode. If it is not supported Html Way, it will use this, basically useless
       
        if (!$mail->send()) { // Send mail
            echo "Message could not be sent.";
            echo "Mailer Error: " . $mail->ErrorInfo;// Output error message
        }
        echo '<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"><title>Tips</title><script language=javascript>alert("Message successfully!");location.href="'.$_SERVER["HTTP_REFERER"].'"</script></head></html>';

Tags: PHP SSL Javascript Apache

Posted on Tue, 10 Mar 2020 23:20:22 -0700 by UKlee