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' 堆栈跟踪:#0 {main} 在 C:\xampp\htdocs\ 中抛出第 10 行的 loginsystem\phpmailer.php
解决方案
你误解了use
陈述的目的。它们不是路径;他们将命名空间的类名导入您当前的命名空间。将代码更改为此,如示例所示:
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
推荐阅读
- javafx - Javafx:如何使用按钮和文本字段在标签中打印数组字符串?
- javascript - How i get events in electron webcontents?
- python - 从's'中的'data'变量中获取什么数据?
- java - 为库存 nifi 处理器创建自定义 DistributedMapCacheClient 控制器服务
- java - Java:从使用对象获取数组创建 CSV
- apache-spark - Databricks 内存的最大容量是多少?
- android - FlatList 当许多屏幕在他们之前的路线上时缓慢并开始闪烁。?
- tensorflow - 如何解决有关 Tensorflow 和 cuda 兼容性的问题?
- javascript - 匹配除我的模式以外的所有内容(RegEx)
- python - ValueError:将文件上传到 s3 存储桶时,文件名必须是字符串