PHPMailer错误:无法发送消息.Mailer错误:地址无效:(addAnAddress to):您的电子邮件

时间:2018-06-01 15:23:02

标签: php phpmailer

大家好我已经写得正确,但是如果使用xampp或在网站上传不起作用,那么我已经激活gmail上的允许发布应用程序,我不知道解决方案,请帮助我,我需要这个有一个客户联系表格给我发送邮件数据

<?php
    use PHPMailer\PHPMailer\PHPMailer;
    use PHPMailer\PHPMailer\Exception;

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

    $mail = new PHPMailer(true);                              // Passing `true` enables exceptions
    try {
        if(isset($_POST['send'])){
            $name = $_POST['name'];
            $email = $_POST['email'];
            $subject = $_POST['subject'];
            $message = $_POST['message'];
            //Server settings
            //$mail->SMTPDebug = 2;                                 // Enable verbose debug output
            $mail->isSMTP();                                      // Set mailer to use SMTP
            $mail->Host = 'smtp.gmail.com';  // Specify main and backup SMTP servers
            $mail->SMTPAuth = true;                               // Enable SMTP authentication
            $mail->Username = 'i write my email but not work';                 // SMTP username
            $mail->Password = "i write my password but not work";                           // SMTP password
            $mail->SMTPSecure = 'tls';                            // Enable TLS encryption, `ssl` also accepted
            $mail->Port = 587;                                    // TCP port to connect to

            //Recipients
            $mail->setFrom($email, $name);
            $mail->addAddress('Your Email', 'Your Name');     // Add a recipient


            //Content
            $mail->isHTML(true);                                  // Set email format to HTML
            $mail->Subject = $subject;
            $mail->Body    = $message . "<br> Sent from: Form";

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

    <form action="" method="post">
        <label>Name:</label><br><input type="text" name="name" placeholder="Your Name" required autocomplete="off" autofocus><br><br>
        <label>Email:</label><br><input type="email" name="email" placeholder="Your Email" required autocomplete="off"><br><br>
        <label>Subject:</label><br><input type="text" name="subject" placeholder="Your Subject" required autocomplete="off"><br><br>
        <label>Message:</label><br><textarea name="message" placeholder="Your Message" required></textarea><br><br>
        <input type="submit" name="send">

    </form>

错误

2018-06-01 15:43:12 SERVER -> CLIENT: 220 smtp.gmail.com ESMTP b15-v6sm39782048wri.14 - gsmtp
2018-06-01 15:43:12 CLIENT -> SERVER: EHLO 127.0.0.1
2018-06-01 15:43:12 SERVER -> CLIENT: 250-smtp.gmail.com at your service, [93.46.246.225]250-SIZE 35882577250-8BITMIME250-STARTTLS250-ENHANCEDSTATUSCODES250-PIPELINING250 SMTPUTF8
2018-06-01 15:43:12 CLIENT -> SERVER: STARTTLS
2018-06-01 15:43:12 SERVER -> CLIENT: 220 2.0.0 Ready to start TLS
SMTP Error: Could not connect to SMTP host.
2018-06-01 15:43:13 CLIENT -> SERVER: QUIT
2018-06-01 15:43:13 SERVER -> CLIENT: 
2018-06-01 15:43:13 SMTP ERROR: QUIT command failed: 
SMTP Error: Could not connect to SMTP host.
Message could not be sent.Mailer Error: SMTP Error: Could not connect to SMTP host.

2 个答案:

答案 0 :(得分:1)

您需要更改此行以获得有效的电子邮件地址:

$mail->addAddress('Your Email', 'Your Name');

'您的电子邮件'应替换为'user@example.com'或任何其他电子邮件。

答案 1 :(得分:0)

您可以查看此视频,它将解决您的问题https://www.youtube.com/watch?v=FwqeOONRDvw&t=140s

只需要编辑php.ini 和sendmail.ini