首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >PHPMailer连接问题

PHPMailer连接问题
EN

Stack Overflow用户
提问于 2022-07-05 11:54:28
回答 1查看 55关注 0票数 0

我想用php(使用windows操作系统)发送电子邮件,.However我一直遇到连接问题,附件文件不是found.Are,有什么解决方案吗?

代码语言:javascript
复制
//-------------sending mail----------------------------

//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\SMTP;

use PHPMailer\PHPMailer\Exception;

require 'PHPMailer-master/src/Exception.php';

require 'PHPMailer-master/src/PHPMailer.php';

require 'PHPMailer-master/src/SMTP.php';

//Create an instance; passing true enables exceptions

$mail = new PHPMailer(true);

try {
    //Server settings

    $mail->isSMTP();                                            //Send using SMTP
    $mail->Host       = 'smtp.gmail.com';                     //Set the SMTP server to send through
    $mail->SMTPAuth   = true;                                   //Enable SMTP authentication
    $mail->Username   = 'username@gmail.com';                     //SMTP username
    $mail->Password   = 'password';                               //SMTP password
    $mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;         //Enable implicit TLS encryption
    $mail->Port       = 587;                                    //TCP port to connect to; use 587 if you have set `SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS`

    //Recipients
    $mail->setFrom('hotelpage@gmail.com', 'Hotels Page');
    $mail->addAddress('MohanDepesh@gmail.com', 'MohanDepesh');     //Add a recipient
    $mail->addReplyTo('no-reply@gmail.com', 'no-reply');
    $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 = 'Here is the subject';
    $mail->Body    = 'This is the HTML message body <b>in bold!</b>';
    $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. Mailer Error: {$mail->ErrorInfo}";

}

EN

回答 1

Stack Overflow用户

发布于 2022-07-05 12:23:19

请核对你收到错误的以下几点-

1.检查你是否使用正确的邮件和密码。2.由于“允许不太安全的应用程序”,settings.Try可能会启动此设置进行测试。如果您仍然面临问题,请附上准确的错误。

编辑:由于允许不太安全的应用程序现在是禁用的,您可以使用appPassword。

票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72868999

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档