Attach File Through PHP Mail

独自空忆成欢 提交于 2019-12-02 16:04:40

问题


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?


回答1:


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

PHP EMAIL WITH ATTACHMENT

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.

PHPMailer



来源:https://stackoverflow.com/questions/11961969/attach-file-through-php-mail

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!