我正在尝试使用PHPMailer类发送包含多个附件的电子邮件。多个文件在目录中成功上传,但它只在Email中发送一个文件。以及如何使用bootstrap格式化我的电子邮件正文?您的建议将受到高度赞赏。我正在使用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');
// }
}
答案 0 :(得分:1)
我将类创建移到了文件文件处理循环之前,并将附件代码移到了循环中
<?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'];
$mail = new PHPMailer; //moved here
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);
$mail->addAttachment($name); //attache here
}
} else {
$name = '';
}
//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;
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');
// }
}