簡體   English   中英

使用php從表單發送帶有文件附件的電子郵件

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

我有一份工作申請表,需要允許用戶發送其簡歷以及其他一些基本詳細信息。 我不需要將文件保存在服務器上,只需將電子郵件作為附件發送即可。

我遵循了一些不同的教程和指南,並提出了以下功能:

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));

}

它發送一封電子郵件-但為空白,並帶有2個附件(noname.txt和noname)。

有人可以看一下上面的內容,並指出正確的方向嗎?

根據Marc的建議,使用PHPMailer更簡單! 我建議任何嘗試發送附件的人都使用phpmailer。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM