3 回答
TA贡献1801条经验 获得超8个赞
我会重写它,变成这样:
// Now create a message
require 'vendor/autoload.php';
//$mail->AllowEmpty = true;
$mail = new PHPMailer;
$mail->setFrom('info@*****.com', 'first last');
$mail->addAddress('info@*****.com');
$mail->Subject = 'PHPMailer file sender';
$mail->Body = "Contact form submission:\n" . "Name: $sender_name\n";
if (array_key_exists('userfile', $_FILES) && $_FILES["userfile"]["error"] == UPLOAD_ERR_OK) {
// First handle the upload
// Don't trust provided filename - same goes for MIME types
// See http://php.net/manual/en/features.file-upload.php#114004 for more thorough upload validation
$uploadfile = tempnam(sys_get_temp_dir(), hash('sha256', $_FILES['userfile']['name']));
if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
// Upload handled successfully
// Attach the uploaded file
//$mail->addAttachment($uploadfile, 'My uploaded file');
$name = $_FILES['userfile']['name'];
$ext = end((explode(".", $name)));
$mail->addAttachment($uploadfile, 'You Have Attachment.'.$ext);
} else {
$msg .= 'Failed to move file to ' . $uploadfile;
}
}
if (!$mail->send()) {
$msg .= "Mailer Error: " . $mail->ErrorInfo;
} else {
$msg .= "Message sent!";
}
这样它将发送电子邮件,如果有文件附件,它将被包含在内。
TA贡献2039条经验 获得超7个赞
它以这种方式工作,现在它正在发送带/不带附件的电子邮件,我还将发件人姓名和电子邮件等许多内容更改为变量和正文内容。现在我没有收到任何 PHP 错误,并且表单按照我的意愿完美运行,我希望这会对某人有所帮助,谢谢大家。
<?php
use PHPMailer\PHPMailer\PHPMailer;
$msg = '';
if (array_key_exists('userfile', $_FILES)) {
require 'vendor/autoload.php';
// Create a message
$mail = new PHPMailer;
$mail->SetFrom($_POST['email'], $_POST['sender_name']); //altered
$mail->addAddress('info@***.com');
$mail->Subject = 'Service Request';
$mail->Body = "Sender: " .$_POST['sender_name'] ."\n" ."Phone: " .$_POST['phone']; //altered
//Attach multiple files one by one
for ($ct = 0; $ct < count($_FILES['userfile']['tmp_name']); $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)) {
$mail->addAttachment($uploadfile, $filename);
}
}
if (!$mail->send()) {
$msg .= "Mailer Error: " . $mail->ErrorInfo;
} else {
$msg .= "Message sent!";
}
}
?>
TA贡献2003条经验 获得超2个赞
完成实际发送的整个脚本取决于是否有文件是或否的事实。这是由脚本顶部的 if 语句处理的。我建议在始终发送电子邮件的地方进行编辑,并且只有在有文件是或否时才会添加文件。但由于我在移动设备上,我不知道编辑建议是否通过。
- 3 回答
- 0 关注
- 197 浏览
添加回答
举报