通过PHP邮件附加文件 [英] Attach File Through PHP Mail
问题描述
我尝试使用PHP邮件获取自定义联系表单,让用户附上一张照片,然后发送给PHP邮件代码中列出的收件人。
< input type =fileid =filename =file>
表单代码如下;
< form action =register-mail.phpmethod =POSTenctype =multipart / form-data>
< input type =fileid =filename =file>
< input type =submitvalue =提交>
< / form>
PHP邮件代码如下;
<?php $ file = $ _FILES ['file'];
$ formcontent =电子邮件文字内容;
$ recipient =fake@email.com;
$ subject =这是一张照片;
$ mailheader ='发件人:基本注册< fake@email.com>'。 \r\\\
;
mail($ recipient,$ subject,$ formcontent,$ mailheader)或者die(Error!);
die();
?>
我似乎无法将它附加到正在发送的电子邮件中。我做错了什么?
这不是附件的工作原理。对附件使用 mail()
比这稍微复杂一些。你必须告诉 mail()
哪个部分应该处理文件附件,以及哪个部分负责通过设置 MIME边界 。换句话说,代码应该分为两部分:
- 处理邮件正文的部分
- 处理文件上传的部分
详细教程在这里
但是,我建议您使用称为PHPMailer的非常方便的工具来完成相同的任务。它简化了流程,并让班级处理所有的工作。
I am trying to get a custom contact form using PHP mail to have a user attach a photo, that then gets sent to the recipient outlined in the PHP mail code
<input type="file" id="file" name="file">
The form code is as follows;
<form action="register-mail.php" method="POST" enctype="multipart/form-data">
<input type="file" id="file" name="file">
<input type="submit" value="Submit">
</form>
The PHP mail code is as follows;
<?php $file = $_FILES['file'];
$formcontent="Email Text Content";
$recipient = "fake@email.com";
$subject = "Here is a Photo";
$mailheader = 'From: Basic Sign-up <fake@email.com>' . "\r\n";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
die();
?>
I can't seem to get it to attach the file to the email being sent. What am I doing wrong?
That is not how attachment works. Using the mail()
for attachments is a little more complex than that. You got to tell mail()
which part should handle the file attachment and which part is responsible to display the email body by setting up a MIME Boundary. In other words, the code should be divided into 2 parts:
- A section to handle the message being sent in body
- A section to handle file uploading
A detailed tutorial is here
However, I would suggest you to use a very handy tool called PHPMailer to do the same task. It simplifies the process and lets the class handle all the legwork.
这篇关于通过PHP邮件附加文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!