繁体   English   中英

使用PHPMailer在PHP中使用SMTP发送电子邮件

[英]Send email using SMTP in PHP with PHPMailer

我尝试了这段代码:

<?php

// Import PHPMailer classes into the global namespace
// These must be at the top of your script, not inside a function
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

//Load composer's autoloader
require '/home/username/public_html/vendor/autoload.php';



/**
     * @param type $array
     * @param type $int
     * @return type
     */
    function send_mail_customer($nickname, $email, $total_value)
    {
        $to      = $email;
        $subject = 'DISTRIBUIÇÃO DE LUCRO: ' . $nickname ;

        $from = "myname@mydomain.com";
        $name = $nickname;

        $message = "<html><body>";
        $message .= "<H1> DISTRIBUIÇÃO MENSAL DE LUCRO </H1>";
        $message .= "<strong>Total ganho:</strong> " .$total_value. " euros";
        $message .= "</body></html>";


        $mail = new PHPMailer(true);                              // Passing `true` enables exceptions
        try {
            //Server settings
            $mail->SMTPDebug = 2;                                 // Enable verbose debug output
            $mail->isSMTP();                                      // Set mailer to use SMTP
            $mail->Host = 'mail.mydomain.com';                   // Specify main and backup SMTP servers
            $mail->SMTPAuth = true;                               // Enable SMTP authentication
            $mail->Username = 'Myname@mydomain.com';          // SMTP username
            $mail->Password = 'Mypassword';                       // SMTP password
            $mail->SMTPSecure = 'ssl';                            // Enable TLS encryption, `ssl` also accepted
            $mail->Port = 465;                                    // TCP port to connect to 587

            //Recipients
            $mail->setFrom($from, 'MyDomain');
            $mail->addAddress($to, $nickname);     // Add a recipient
            //$mail->addAddress('contact@example.com');               // Name is optional
            $mail->addReplyTo($from, 'MyDomain');
            //$mail->addCC('cc@example.com');
            //$mail->addBCC('bcc@example.com');

            //Attachments
            //$mail->addAttachment('/var/tmp/file.tar.gz');         // Add attachments
            //$mail->addAttachment('/tmp/image.jpg', 'new.jpg');    // Optional name

            //Content
            $mail->isHTML(true);                                  // Set email format to HTML
            $mail->Subject = $subject;
            $mail->Body    = $message;
            //$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';

            $mail->send();
            echo 'Message has been sent';
        } catch (Exception $e) {
            echo 'Message could not be sent.';
            echo 'Mailer Error: ' . $mail->ErrorInfo;
        }

    }

    $x="NunoSousa";
    $email="myemail@gmail.com";
    $total_value = 15;
    send_mail_customer($x, $email, $total_value);

?>

我收到错误:

2018-09-26 08:06:21 SMTP错误:无法连接到服务器:(0)SMTP connect()失败。 https://github.com/PHPMailer/PHPMailer/wiki/故障排除消息无法发送。Mailer错误:SMTP connect()失败。 https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting

我阅读了Wiki,但看不到出了什么问题。 我还尝试使用端口587进行TLS加密,并且问题仍然相同。

更改

 $mail->SMTPSecure = 'ssl';  

 $mail->SMTPSecure = 'tls'; 
 $mail->Host = 'smtp.gmail.com'; 
 $mail->Port = 587; 

也许您的房东是错的。 在我的情况下,我的主机名为smtp.domain.com 另外我在端口587使用了tls ,您可以使用正确的主机再次尝试连接。

在你的情况下

 $mail->Host = 'smtp.mydomain.com';
 $mail->SMTPSecure = 'tls';
 $mail->Port = 587;

您的邮件主机,设置为服务器的IP地址:

$mail->SMTPDebug = 0;  // Enable verbose debug output, default is 2
$mail->isSMTP();   // Set mailer to use SMTP
$mail->Host = ''; 
$mail->SMTPAuth = true;  // Enable SMTP authentication
$mail->Username = '';    // SMTP username
$mail->Password = '';    // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also 
                          //accepted
$mail->Port = 587; 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM