Send File Attachment from Form Using phpMailer and PHP(使用 phpMailer 和 PHP 从表单发送文件附件)
问题描述
我在 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 从表单发送文件附件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:使用 phpMailer 和 PHP 从表单发送文件附件
基础教程推荐
- 在 PHP 中强制下载文件 - 在 Joomla 框架内 2022-01-01
- 超薄框架REST服务两次获得输出 2022-01-01
- XAMPP 服务器不加载 CSS 文件 2022-01-01
- 通过 PHP SoapClient 请求发送原始 XML 2021-01-01
- 如何在 PHP 中的请求之间持久化对象 2022-01-01
- 在多维数组中查找最大值 2021-01-01
- WooCommerce 中选定产品类别的自定义产品价格后缀 2021-01-01
- 在 Woocommerce 中根据运输方式和付款方式添加费用 2021-01-01
- Libpuzzle 索引数百万张图片? 2022-01-01
- mysqli_insert_id 是否有可能在高流量应用程序中返回 2021-01-01