問題描述
我在 example.com/contact-us.php
上有一個表單,看起來像這樣(簡化):
I have a form on example.com/contact-us.php
that looks like this (simplified):
<form method="post" action="process.php" enctype="multipart/form-data">
<input type="file" name="uploaded_file" id="uploaded_file" />
<input type="hidden" name="MAX_FILE_SIZE" value="10000000" />
</form>
在我的 process.php
文件中,我有以下代碼利用 PHPMailer()
發送電子郵件:
In my process.php
file, I have the following code utilizing PHPMailer()
to send an email:
require("phpmailer.php");
$mail = new PHPMailer();
$mail->From = me@example.com;
$mail->FromName = My name;
$mail->AddAddress(me@example.com,"John Doe");
$mail->WordWrap = 50;
$mail->IsHTML(true);
$mail->Subject = "Contact Form Submitted";
$mail->Body = "This is the body of the message.";
電子郵件正確發送正文,但沒有uploaded_file
的附件.
The email sends the body correctly, but without the Attachment of uploaded_file
.
我的問題
我需要將表單中的文件 uploaded_file
附加到電子郵件中并發送.在 process.php
腳本通過電子郵件發送文件后,我不關心保存文件.
I need the file uploaded_file
from the form to be attached to the email, and sent. I do NOT care about saving the file after the process.php
script sends it in an email.
我知道我需要在某處(我假設在 Body
行下)添加 AddAttachment();
以發送附件.但是...
I understand that I need to add AddAttachment();
somewhere (I'm assuming under the Body
line) for the attachment to be sent. But...
- 我在
process.php
文件的頂部放了什么來拉入文件uploaded_file
?喜歡使用$_FILES['uploaded_file']
從 contact-us.php 頁面中提取文件嗎? AddAttachment();
的內容是什么來附加文件并與電子郵件一起發送?此代碼需要放在何處?
- What do I put at the top of the
process.php
file to pull in the fileuploaded_file
? Like something using$_FILES['uploaded_file']
to pull in the file from the contact-us.php page? - What goes inside of
AddAttachment();
for the file to be attached and sent along with the email and where does this code need to go?
請幫忙并提供代碼!謝謝!
Please help and provide code!Thanks!
推薦答案
嘗試:
if (isset($_FILES['uploaded_file']) &&
$_FILES['uploaded_file']['error'] == UPLOAD_ERR_OK) {
$mail->AddAttachment($_FILES['uploaded_file']['tmp_name'],
$_FILES['uploaded_file']['name']);
}
也可以在此處找到基本示例.
Basic example can also be found here.
AddAttachment
的函數定義是:
public function AddAttachment($path,
$name = '',
$encoding = 'base64',
$type = 'application/octet-stream')
這篇關于使用 phpMailer 和 PHP 從表單發送文件附件的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!