代碼在沒有附件的情況下作業正常。并且附件代碼在另一個頁面上作業正常有人可以幫助解決這個問題嗎?<?php
require('phpmailer/class.phpmailer.php');
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->SMTPDebug = 0;
$mail->SMTPAuth = TRUE;
$mail->SMTPSecure = "ssl";
$mail->Port = 465;
$mail->Username = "[email protected]";
$mail->Password = "Guildsconnect";
$mail->Host = "webs10rdns1.websouls.net";
$mail->Mailer = "smtp";
$mail->SetFrom($contact_email, $contact_name);
$mail->AddReplyTo($contact_email, $contact_name);
$mail->AddAddress("[email protected]");
$mail->Subject = $sub1;
$mail->WordWrap = 80;
$mail->MsgHTML($emailbodyis);
foreach ($_FILES["attachment"]["name"] as $k => $v) {
$mail->AddAttachment( $_FILES["attachment"]["tmp_name"][$k], $_FILES["attachment"]["name"][$k] );
}
$mail->IsHTML(true);
if(!$mail->Send()) {
$_SESSION["error"] = "Problem in Sending Mail.";
} else {
$_SESSION["success"] = "Mail Sent Successfully.";
}
?>
uj5u.com熱心網友回復:
首先,您使用的是非常舊且不受支持的 PHPMailer 版本,因此請升級. 看起來您的代碼基于一個非常古老的示例。
根據 PHP 檔案,您沒有安全地處理檔案上傳,也沒有在嘗試使用之前驗證上傳的檔案。
PHPMailer 提供了一個示例,說明如何正確處理、上傳和附加它。關鍵部分用于在使用move_uploaded_file()之前驗證上傳,而不是信任提供的檔案名。解釋這個例子:
//Extract an extension from the provided filename
$ext = PHPMailer::mb_pathinfo($_FILES['userfile']['name'], PATHINFO_EXTENSION);
//Define a safe location to move the uploaded file to, preserving the extension
$uploadfile = tempnam(sys_get_temp_dir(), hash('sha256', $_FILES['attachment']['name'])) . '.' . $ext;
if (move_uploaded_file($_FILES['attachment']['tmp_name'], $uploadfile)) {
//Now create a message
$mail = new PHPMailer();
...
//Attach the uploaded file
if (!$mail->addAttachment($uploadfile, 'My uploaded file')) {
$msg .= 'Failed to attach file ' . $_FILES['attachment']['name'];
}
if (!$mail->send()) {
$msg .= 'Mailer Error: ' . $mail->ErrorInfo;
} else {
$msg .= 'Message sent!';
}
} else {
$msg .= 'Failed to move file to ' . $uploadfile;
}
你不需要設定Mailer自己;已經為您完成了isSMTP()。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/428123.html
下一篇:電子郵件表格布局-重疊文本
