【发布时间】:2018-10-27 00:43:36
【问题描述】:
我正在尝试使用 PHPMailer 类发送带有多个附件的电子邮件。多个文件在目录中成功上传,但它只在电子邮件中发送一个文件。以及如何使用引导程序格式化我的电子邮件正文?您的建议将不胜感激。我正在使用 PHPMailer 6.0.5
这是我的 PHP 代码:
<?php
$msg = '';
use PHPMailer\PHPMailer\PHPMailer;
include_once 'PHPMailer/PHPMailer.php';
include_once 'PHPMailer/Exception.php';
// include_once 'PHPMailer/SMTP.php';
if (isset($_POST['submit'])) {
$inputZip = $_POST['inputZip'];
$selectService = $_POST['selectService'];
$lawnMovingService = $_POST['lawnMovingService'];
$leafRemovalService = $_POST['leafRemovalService'];
$snowPlowingService = $_POST['snowPlowingService'];
$handymanService = $_POST['handymanService'];
$inputName = $_POST['inputName'];
$inputEmail = $_POST['inputEmail'];
$inputPhone = $_POST['inputPhone'];
$inputMessage = $_POST['inputMessage'];
if (isset($_FILES['images']['name']) && $_FILES['images']['name'] != '') {
$destination = "attachment/";
foreach ($_FILES["images"]["tmp_name"] as $key => $value) {
$tmp_name = $_FILES["images"]["tmp_name"][$key];
$name = $destination . basename($_FILES["images"]["name"][$key]);
move_uploaded_file($tmp_name, $name);
}
} else {
$name = '';
}
$mail = new PHPMailer;
//For SMTP
//$mail->Host = "smtp.gmail.com";
//$mail->isSMTP(); // This line may cause problem
//$mail->SMTPAuth = true;
//$mail->Username = "example@gmail.com";
//$mail->Password = "examplePassword";
//$mail->SMTPSecure = "ssl"; //OR TLS
//$mail->Port = 465; //TLS : 587
$mail->addAddress('milan.uptech@gmail.com');
$mail->setFrom($inputEmail);
$mail->Subject = 'Service Booking from Website';
$mail->isHTML(true);
$mail->Body = $inputMessage;
$mail->addAttachment($name);
if ($mail->send()) {
header('Location: index.php');
} else {
header('Location: contact.php');
}
// if(sendemail('milan.uptech@gmail.com', $email, $name, $body, $file)) {
// $msg = 'Email Sent!';
// sendemail($inputEmail, 'milan.uptech@gmail.com', $inputName, 'We have received your email');
// }
}
【问题讨论】:
-
您需要为每个要附加的文件调用 addAttachment(),所以在循环中
-
@smith 如何循环 addAttachment() 你有什么好的例子吗?
-
我马上补充答案,
-
基本上,只需将您的 foreach 循环向下移动到您正在执行的位置
addAttachment并在其中移动addAttachment调用。 -
@smith 我希望我可以将您的答案标记为完美解决方案。 ;)
标签: php email phpmailer email-attachments