简体   繁体   English

使用php从表单发送带有文件附件的电子邮件

[英]Sending email with file attachment from form using php

I have a job application form that needs to allow a user to send a copy of their CV along with some other basic details. 我有一份工作申请表,需要允许用户发送其简历以及其他一些基本详细信息。 I don't need the file to be saved on the server, just emailed as an attachment. 我不需要将文件保存在服务器上,只需将电子邮件作为附件发送即可。

I've followed a few different tutorials and guides, and have come up with the following function: 我遵循了一些不同的教程和指南,并提出了以下功能:

function mail_file ($email,$from,$subject,$body,$file){
$boundary = md5(rand());
$headers = array(
    "MIME-Version: 1.0",
    "From:{$from}",
    "Content-Type: Multipart/mixed; boundary = {$boundary}"
);

$message = array(
    "--{$boundary}",
    "Content-Type: text/plain: charset = utf-8",
    "Content-Transfer-Encoding: 7bit",
    "",
    chunk_split($body),
    "--{$boundary}",
    "Content-Type: {$file['type']}: name={$file['name']}",
    "Content-Disposition: attachment; filename= {$file['name']}",
    "Content-Transfer-Encoding: base64",
    "",
    chunk_split(base64_encode(file_get_contents($file['path']))),
    "--{$boundary}--"
);

return mail ($email, $subject, implode("\r\n",$message),implode("\r\n",$headers));

} }

It sends an email - but it's blank, and has 2 attachments (noname.txt and noname). 它发送一封电子邮件-但为空白,并带有2个附件(noname.txt和noname)。

Would someone be able to take a look at the above, and point me in the right direction? 有人可以看一下上面的内容,并指出正确的方向吗?

As per Marc's advice, using PHPMailer is a lot simpler! 根据Marc的建议,使用PHPMailer更简单! I suggest anyone trying to send attachments use phpmailer. 我建议任何尝试发送附件的人都使用phpmailer。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM