php - 在电子邮件正文 phpmailer 中显示附件的文件名
问题描述
我在我的网站上有一个使用 phpmailer 的表单,并且只是在其中添加了附件功能并且效果很好,我收到了电子邮件中的表单和附件,但是有没有办法在电子邮件正文内容中显示附件的文件名,下面是我当前的代码
<?php
// PHPMailer classes into the global namespace
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
// Base files
require 'PHPMailer/src/Exception.php';
require 'PHPMailer/src/PHPMailer.php';
require 'PHPMailer/src/SMTP.php';
$msg = '';
if (array_key_exists('userfile', $_FILES)) {
// create object of PHPMailer class with boolean parameter which sets/unsets exception.
$mail = new PHPMailer(true);
try {
//$mail->isSMTP(); // using SMTP protocol
$mail->Host = 'emailhost'; // SMTP host
$mail->SMTPAuth = true; // enable smtp authentication
$mail->Username = 'emailaddress'; // sender host
$mail->Password = 'emailpassword'; // sender host password
$mail->SMTPSecure = 'ssl'; // for encrypted connection
$mail->Port = 587; // port for SMTP
$mail->setFrom('emailaddress', "Name"); // sender's email and name
$mail->addAddress('emailaddress', "Name"); // receiver's email and name
$attachmentNames = [];
for ($ct = 0, $ctMax = count($_FILES['userfile']['tmp_name']); $ct < $ctMax; $ct++) {
$uploadfile = tempnam(sys_get_temp_dir(), hash('sha256', $_FILES['userfile']['name'][$ct]));
$filename = $_FILES['userfile']['name'][$ct];
$FileType = pathinfo($filename, PATHINFO_EXTENSION);
// Allow certain file formats
$allowTypes = array('jpg', 'png', 'jpeg');
if(in_array($FileType, $allowTypes)){
if (move_uploaded_file($_FILES['userfile']['tmp_name'][$ct], $uploadfile)) {
if ($mail->addAttachment($uploadfile, $filename)) {
$attachmentNames[] = $_FILES['userfile']['name'][$ct];
} else {
$msg .= 'Failed to attach file ' . $_FILES['userfile']['name'][$ct];
}
} else {
$msg .= 'Failed to move file to ' . $uploadfile;
}
} else {
$msg .= 'Sorry only jpg, png and jpeg files are allowed to upload';
}
}
$mail->Subject = 'New Repair Booking';
$mail->Body = "A new repair has been booked in. The repair information is below" . "\r\n\r\n" . "Repair Date: " . $_POST["date"] . "\r\n" . "Device: " . $_POST["selectdevice"] . "\r\n" . "Brand: " . $_POST["selectbrand"] . "\r\n" . "Name: " . $_POST["name"] . "\r\n" . "Email: " . $_POST["email"] . "\r\n" . "Phone Number: " . $_POST["phone"] . "\r\n" . "Repair Description/Issue: " . $_POST["repairdescription"] . "\r\n\r\n" . "Attached Filename(s): " . implode(', ', $attachmentNames);
}
};
?>
解决方案
在解析附件时构建一个文件名数组,然后implode
将其附加到Body
:
$attachmentNames = [];
//...
for ($ct = 0, $ctMax = count($_FILES['userfile']['tmp_name']); $ct < $ctMax; $ct++) {
$uploadfile = tempnam(sys_get_temp_dir(), hash('sha256', $_FILES['userfile']['name'][$ct]));
$filename = $_FILES['userfile']['name'][$ct];
if (move_uploaded_file($_FILES['userfile']['tmp_name'][$ct], $uploadfile)) {
if ($mail->addAttachment($uploadfile, $filename)) {
$attachmentNames[] = $_FILES['userfile']['name'][$ct];
} else {
$msg .= 'Failed to attach file ' . $_FILES['userfile']['name'][$ct];
}
} else {
$msg .= 'Failed to move file to ' . $uploadfile;
}
}
//...
if (count($attachmentNames) > 0) {
$mail->Body .= "\n\nAttachments: " . implode(', ', $attachmentNames);
}
请注意,您的文件上传处理不安全,因为您没有使用is_uploaded_file
或验证它们move_uploaded_file
。请参阅文档和PHPMailer 文件上传示例,了解如何正确执行此操作。您还应该检查 的返回值,addAttachment()
因为它也有可能失败。
推荐阅读
- powershell - 带有 content.replace 的嵌套 foreach() 循环未按预期设置内容(或添加内容)
- css - 在全屏模式下使用 Google 地图看不到警报对话框
- r - 在R中迭代数据框
- sql - 何时在子查询中使用 IN OR AND?(SQL oracle 数据库 hr 架构)
- javascript - CKEditor 4.6 有时会在不进行配置的情况下剥离属性
- java - 如何使用 Groovy 在 Outlook 中创建“免费”条目?
- node.js - 如何显示完整的属性路径 Joi 验证
- laravel - 通过修剪函数将输入名称变量转换为在数据库中搜索
- elasticsearch - 在logstash中分离键值对
- javascript - 如何在 switch 语句中传递按钮 ID 以执行该按钮应该执行的任何功能?