使用PHPMailer(SMTP)发送邮件

时间:2017-11-11 15:53:52

标签: php email smtp gmail phpmailer

我正在尝试使用PHPMailer(SMTP)发送邮件。如果我从localhost发送,我会收到错误警告:

2017-11-11 15:14:39服务器 - >客户端:220 2.0.0准备启动TLS 2017-11-11 15:14:40连接失败错误#2:stream_socket_enable_crypto():SSL操作失败,代码为1. OpenSSL错误消息:错误:14090086:SSL例程:ssl3_get_server_certificate:证书验证失败[C:\ xampp \ htdocs \ NEU \ phpmailer \ vendor \ phpmailer \ phpmailer \ src \ SMTP.php第404行] SMTP错误:无法连接到SMTP主机。 2017-11-11 15:14:40客户 - >服务器:退出

我已经阅读过如下更改ssl选项:

$mail->SMTPOptions = array(
                        'ssl' => array(
                            'verify_peer' => false,
                            'verify_peer_name' => false,
                            'allow_self_signed' => true
                        )
                    );

这对我有用,但我想应该有另一个选项来解决问题,因为这并没有在实际的服务器上修复它。 在服务器上,浏览器只是保持加载一段时间,直到我收到504错误甚至消息:

无法加载资源:net :: ERR_INCOMPLETE_CHUNKED_ENCODING

现在我真的不知道如何解决这个问题,因为我对此非常陌生。 我也读过有关ssl认证的一些问题,但没有得出任何结论......

我的要求代码:

<?php

    // Import PHPMailer classes into the global namespace
    use PHPMailer\PHPMailer\PHPMailer;

    //Load composer's autoloader
    require 'phpmailer/vendor/autoload.php';

    //Empfänger E-Mail
    $recipient = 'recipient@web.de';

    //Formularfelder
    $vname = trim($_POST['vname']);
    $name = trim($_POST['name']);
    $email = trim($_POST['email']);
    $message = trim($_POST['message']);

    if($name != null && $email != null && $message != null) {
        if (!filter_var($email, FILTER_VALIDATE_EMAIL) === false) {

                    $mail = new PHPMailer(true);                                    // Passing `true` enables exceptions

                    //Server settings
                    $mail->isSMTP();                                                // Set mailer to use SMTP
                    $mail->SMTPDebug = 2;
                    $mail->Host = 'smtp.gmail.com';                         // Specify main and backup SMTP servers
                    $mail->SMTPAuth = true;                                     // Enable SMTP authentication
                    $mail->Username = 'username@gmail.com';     // SMTP username
                    $mail->Password = 'passowrd';                       // SMTP password
                    $mail->SMTPSecure = 'tls';                                  // Enable TLS encryption
                    $mail->Port = 587;                                      // TCP port to connect to

                    /*$mail->SMTPOptions = array(
                        'ssl' => array(
                            'verify_peer' => false,
                            'verify_peer_name' => false,
                            'allow_self_signed' => true
                        )
                    );  */

                    //Recipients
                    $mail->setFrom("$recipient", '**');
                    $mail->addAddress("$recipient", '**');     // Add a recipient 
                    $mail->addReplyTo($email, $name);

                    $body = "<p><strong>Name: </strong>$vname $name <br> <strong>Nachricht: </strong>$message</p>";

                    //Content
                    $mail->isHTML(true);                                     // Set email format to HTML
                    $mail->Subject = 'Kontaktformular';
                    $mail->Body    = $body;
                    $mail->AltBody = strip_tags($body);

                    if(!$mail->send()){
                        $signal = 'bad';
                        $msg = 'Mailer Error: ' . $mail->ErrorInfo;
                    } else {
                        $signal = 'ok';
                        $msg = 'Nachricht versendet, vielen Dank!';
                    }

        } else {
            $signal = 'bad';
            $msg = 'Ungültige E-Mail-Adresse, bitte versuchen Sie es erneut.';
        }
    } else {
        $signal = 'bad';
        $msg = 'Bitte füllen Sie alle erforderlichen Felder aus.';
    }

    $data = array(
        'signal' => $signal,
        'msg' =>  $msg
    );

    echo json_encode($data);

?>

我希望你可以帮助我,如果你需要更多的信息,只需要他们。对不起,我的英语不是最好的。 最好的问候

0 个答案:

没有答案