[英]Attachment not sent with Email
我正在使用PHP開發一個小型的電子郵件應用程序,要使用PHPMailer發送電子郵件,所有功能都可以通過一個“附件”分開工作。 附件只是不與發送的電子郵件一起處理。 我嘗試了多種方法來修復它,但是我現在相信的那個方法離工作秩序並不遙遠。
我將發布處理附件過程的一小部分代碼。
HTML代碼:
<form action="php/smtp_saved.php" method="post">
<input type="file" name="attach" id="attach" />
</form>
PHP代碼:
if ($_POST['action'] == 'Send') {
if (preg_match('<,>', $email['recipient'])) {
$address = explode(',', $email['recipient']);
if (sizeof($address) < 19) {
foreach ($address as $recipient) {
$save = new saveSaved();
$save->save($_SESSION['user'], $email['recipient'], gmdate('Y-m-d H:i:s', strtotime ('+1 hour')), $_SESSION['delete'],$email['HTML']);
require_once('../PHPMailer/PHPMailerAutoload.php');
require_once('../PHPMailer/class.smtp.php');
$mail = new PHPMailer;
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp.gmail.com'; // Specify main and backup server
$mail->Port = '465';
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = $_SESSION['user']; // SMTP username
$mail->Password = $_SESSION['pass']; // SMTP password
$mail->SMTPSecure = 'ssl'; // Enable encryption, 'ssl' also accepted
$mail->From = $_SESSION['user'];
$mail->FromName = 'OneTwoTrade';
//$mail->addAddress($email['recipient']); // Add a recipient
//$mail->addAddress('ellen@example.com'); // Name is optional
$mail->addAddress(trim($recipient));
if (preg_match('<,>', $email['cc'])) {
$cc = explode(',', $email['cc']);
if (sizeof($cc) < 9) {
foreach ($cc as $carbonC) {
$mail->addCC($carbonC);
}
} else {
exit ('Max 10 Recipients Per Email');
}
} else {
$mail->addCC($email['cc']);
}
//$mail->addBCC('bcc@example.com');
$mail->WordWrap = 50; // Set word wrap to 50 characters
if(is_uploaded_file($_FILES['attach']['tmp_name'])) {
$file = $_FILES['attach']['name'];
$mail->AddAttachment($file);
}
/*
if(isset($_FILES['uploaded_file']) && $_FILES['uploaded_file']['error'] == UPLOAD_ERR_OK){
$mail->addAttachment($_FILES['uploaded_file']['tmp_name'],
$_FILES['uploaded_file']['name']);
}
*/
// Add attachments
//$mail->addAttachment('/tmp/image.jpg', 'new.jpg'); // Optional name
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = $email['subject'];
$mail->Body = $email['HTML'];
//$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
//$mail->SMTPDebug =1;
if (!$mail->send()) {
header("Location: ../saved_emails.php?error2");
//echo 'Message could not be sent.';
//echo 'Mailer Error: ' . $mail->ErrorInfo;
exit;
}
}
}
}
有人可以幫助我發現錯誤或直接獲得體面的信息來源嗎?
通過表單附加文件時,將enctype="multipart/form-data"
屬性添加到form
標簽。 我認為是因為這個問題。
<form action="php/smtp_saved.php" method="post" enctype="multipart/form-data">
<input type="file" name="attach" id="attach" />
</form>
稍作搜索后,我發現了這一點: AddAttachment()
函數中同時使用了附件的temp_name
和name
鍵:
if(isset($_FILES['attach']) && $_FILES['attach']['error'] == UPLOAD_ERR_OK) {
$mail->AddAttachment($_FILES['attach']['tmp_name'], $_FILES['attach']['name']);
}
$file = $_FILES['attach']['name'];
應該是$file = $_FILES['attach']['tmp_name'];
。 實際的文件數據在$_FILES['attach']['tmp_name']
。 $_FILES['attach']['name']
只是客戶端上載文件的名稱。
好的伙計們想感謝所有試圖幫助我並將其推向解決方案的人,我設法解決了這個問題,這是我必須要做的...
if(is_uploaded_file($_FILES['attach']['tmp_name'])) {
$file = $_FILES['attach']['tmp_name'];
$name = $_FILES['attach']['name'];
$mail->AddAttachment($file, $name);
}
:)))))))))))))))))))))))))))))))))))))))))
就像超級按鈕一樣工作,只需檢查PHPMailer文檔和AddAttachment方法需要兩個參數,
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.