首页 > 解决方案 > 在电子邮件正文 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);
}
};
?>

标签: phpemailphpmaileremail-attachments

解决方案


在解析附件时构建一个文件名数组,然后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()因为它也有可能失败。


推荐阅读