我準備爲網站創建一個表單頁面,用戶需要填寫很多字段並將其發送到指定的電子郵件。通過PHPMailer發送帶有附件的電子郵件
到目前爲止,我已經創建了一個虛擬的PHP電子郵件頁面,使用Google的SMTP獲取您的消息,1個附件和收件人電子郵件地址。
這裏是我的uploadtest.html代碼:
<body>
<h1>Test Upload</h1>
<form action="email.php" method="get">
Message: <input type="text" name="message">
Email: <input type="text" name="email"><br>
Attach File: <input type="file" name="file" id="file">
<input type="submit">
</form>
</body>
uploadtest.html是用戶會看到什麼
下面是email.php代碼:
<?php
require("class.phpmailer.php");
$mail = new PHPMailer();
$recipiant = $_GET["email"];
$message = $_GET["message"];
$mail->IsSMTP(); // telling the class to use SMTP
$mail->SMTPAuth = true; // SMTP authentication
$mail->Host = "smtp.gmail.com"; // SMTP server
$mail->Port = 465; // SMTP Port
$mail->SMTPSecure = 'ssl';
$mail->Username = "[email protected]"; // SMTP account username
$mail->Password = "xxxxxxxx"; // SMTP account password
$mail->AddAttachment($_FILES['tmp_name']); //****HERE'S MY MAIN PROBLEM!!!
$mail->SetFrom('[email protected]', 'CiniCraft.com'); // FROM
$mail->AddReplyTo('[email protected]', 'Dom'); // Reply TO
$mail->AddAddress($recipiant, 'Dominik Andrzejczuk'); // recipient email
$mail->Subject = "First SMTP Message"; // email subject
$mail->Body = $message;
if(!$mail->Send()) {
echo 'Message was not sent.';
echo 'Mailer error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent.';
}
?>
所以,從我可以在這裏告訴,PHPMailer的AddAttachment()方法將需要附加的文件DIRECTORY的URL作爲參數。這是我的主要問題。
變量的名稱是什麼名稱,以獲得我上傳的文件(dir/upload.jpg)的位置,因此我可以將它用作AddAttachment()方法中的參數?
會發生什麼,當你這樣做:的var_dump($ _ FILES [ '不對tmp_name'])的權利之前,你的附件代碼?你能發佈那個輸出嗎? – Maximus2012
也可能需要從代碼中刪除您的個人信息。 – Maximus2012
'$ MAIL-> AddAttachment($ _ FILES [ 'tmp_name的值']);'應'$ MAIL-> AddAttachment($ _ FILES [ '文件'] [ 'tmp_name的值']);' –