如何设置phpmailer以使用它发送文件

时间:2018-10-21 02:39:19

标签: php phpmailer

我正在尝试学习如何使用phpmailer,以便可以将附件文件发送给用户并尝试观看此视频:https://www.youtube.com/watch?v=fcLXsxpk2dc

这是我的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\src\PHPMailer;
use phpmailer\PHPMailer\src\Exception;

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

$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 = 'smtp.live.com';  // Specify main and backup SMTP servers
    $mail->SMTPAuth = true;                               // Enable SMTP authentication
    $mail->Username = 'piano0011@hotmail.com';                 // SMTP username
    $mail->Password = 'Grandpiano888';                           // SMTP password
    $mail->SMTPSecure = 'tls';                            // Enable TLS encryption, `ssl` also accepted
    $mail->Port = 25;                                    // TCP port to connect to

    //Recipients
    $mail->setFrom('piano0011@hotmail.com', 'PianoCourse101');
    $mail->addAddress('joe@example.net', 'Joe User');     // Add a recipient
    $mail->addAddress('ellen@example.com');               // Name is optional
    $mail->addReplyTo('info@example.com', 'Information');
    $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;
}

它说:

  

致命错误:未捕获错误:在C:\ xampp \ htdocs \ loginsystem \ phpmailer.php:10中找不到类'phpmailer \ PHPMailer \ src \ PHPMailer':10堆栈跟踪:#0 {main}抛出在C:\ xampp中第10行的\ htdocs \ loginsystem \ phpmailer.php

enter image description here

1 个答案:

答案 0 :(得分:0)

您误解了use语句的目的。他们不是一条路。他们将命名空间的类名导入到您当前的命名空间中。如示例所示,将您的代码更改为此:

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;