用PHP发送邮件中的多个文件附件

Himanshu Upadhyay

我下面有用于邮件附件的代码,但是它不起作用,不知道为什么会发生。

function mail_attachment($filename, $path, $mailto, $from_mail, $subject, $message){    
    $uid = md5(uniqid(time()));
    $mime_boundary = "==Multipart_Boundary_x{$uid}x"; 

    $header = "From: <".$from_mail.">\r\n";
    $header .= "MIME-Version: 1.0\r\n";
    $header .= "Content-Type: multipart/mixed; boundary=\"".$mime_boundary."\"\r\n\r\n";
    $header .= "This is a multi-part message in MIME format.\r\n";
    $header .= "--".$mime_boundary."\r\n";
    $header .= "Content-type:text/html; charset=iso-8859-1\r\n";
    $header .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
    $header .= nl2br($message)."\r\n\r\n";
    $header .= "--".$mime_boundary."\r\n";

    foreach($filename as $k=>$v){

        $file = $path.$v;
        $file_size = filesize($file);
        $handle = fopen($file, "r");
        $content = fread($handle, $file_size);
        fclose($handle);
        $content = chunk_split(base64_encode($content));

        $header .= "Content-Type: application/octet-stream; name=\"".$v."\"\r\n"; // use different content types here
        $header .= "Content-Transfer-Encoding: base64\r\n";
        $header .= "Content-Disposition: attachment; filename=\"".$v."\"\r\n\r\n";
        $header .= $content."\r\n\r\n";
        $header .= "--".$mime_boundary."--"."\r\n";
    } 

    if (mail($mailto, $subject, "", $header)) {
        //echo "mail send ... OK"; // or use booleans here
        return true;
    } else {
        //echo "mail send ... ERROR!";
        return false;
    }
}
$path='upload/';
$send_email = mail_attachment($files, $path, $to, "roxmorphy26@gmail", $subject, $message);
//mail($to,$subject,$message,$headers);
if($send_email){ echo 'done';} else {echo 'not';}

但它会给出类似错误的警告-警告:mail():在Additional_header中发现了多个或格式错误的换行符

请帮忙。

帕特·帕特尔
<?php
define("LIBR", "\n"); // use a "\r\n" if you have problems
define("PRIORITY", 3); // 3 = normal, 2 = high, 4 = low
define("TRANS_ENC", "7bit");
define("ENCODING", "iso-8859-1");


class attach_mailer {

    var $from_name;
    var $from_mail;
    var $mail_to;
    var $mail_cc;
    var $mail_bcc;
    var $webmaster_email = "[email protected]";

    var $mail_headers;
    var $mail_subject;
    var $text_body = "";
    var $html_body = "";

    var $valid_mail_adresses; // boolean is true if all mail(to) adresses are valid

    var $uid; // the unique value for the mail boundry
    var $alternative_uid; // the unique value for the mail boundry
    var $related_uid; // the unique value for the mail boundry

    var $html_images = array();
    var $att_files = array();

    var $msg = array();

    // functions inside this constructor
    // - validation of e-mail adresses
    // - setting mail variables
    // - setting boolean $valid_mail_adresses
    function attach_mailer($name = "", $from, $to, $cc = "", $bcc = "", $subject = "") {
        $this->valid_mail_adresses = true;
        if (!$this->check_mail_address($to)) {
            $this->msg[] = "Error, the \"mailto\" address is empty or not valid.";
            $this->valid_mail_adresses = false;
        } 
        if (!$this->check_mail_address($from)) {
            $this->msg[] = "Error, the \"from\" address is empty or not valid.";
            $this->valid_mail_adresses = false;
        } 
        if ($cc != "") {
            if (!$this->check_mail_address($cc)) {
                $this->msg[] = "Error, the \"Cc\" address is not valid.";
                $this->valid_mail_adresses = false;
            } 
        }
        if ($bcc != "") {
            if (!$this->check_mail_address($bcc)) {
                $this->msg[] = "Error, the \"Bcc\" address is not valid.";
                $this->valid_mail_adresses = false;
            } 
        }
        if ($this->valid_mail_adresses) {
            $this->from_name = $this->strip_line_breaks($name);
            $this->from_mail = $this->strip_line_breaks($from);
            $this->mail_to = $this->strip_line_breaks($to);
            $this->mail_cc = $this->strip_line_breaks($cc);
            $this->mail_bcc = $this->strip_line_breaks($bcc);
            $this->mail_subject = $this->strip_line_breaks($subject);
        } else {
            return;
        }       
    }
    function get_msg_str() {
        $messages = "";
        foreach($this->msg as $val) {
            $messages .= $val."<br />\n";
        }
        return $messages;           
    }
    // use this to prent formmail spamming
    function strip_line_breaks($val) {
        $val = preg_replace("/([\r\n])/", "", $val);
        return $val;
    }
    function check_mail_address($mail_address) {
        $pattern = "/^[\w-]+(\.[\w-]+)*@([0-9a-z][0-9a-z-]*[0-9a-z]\.)+([a-z]{2,4})$/i";
        if (preg_match($pattern, $mail_address)) {
            if (function_exists("checkdnsrr")) {
                $parts = explode("@", $mail_address);
                if (checkdnsrr($parts[1], "MX")){
                    return true;
                } else {
                    return false;
                }
            } else {
                // on windows hosts is only a limited e-mail address validation possible
                return true;
            }
        } else {
            return false;
        }
    }
    function get_file_data($filepath) {
        if (file_exists($filepath)) {
            if (!$str = file_get_contents($filepath)) {
                $this->msg[] = "Error while opening attachment \"".basename($filepath)."\"";
            } else {
                return $str;
            }
        } else {
            $this->msg[] = "Error, the file \"".basename($filepath)."\" does not exist.";
            return;
        }
    }
    // use for $dispo "attachment" or "inline" (f.e. example images inside a html mail
    function add_attach_file($file, $encoding = "base64", $dispo = "attachment", $type = "application/octet-stream") {
        $file_str = $this->get_file_data($file);
        if ($file_str == "") {
            return;
        } else {
            if ($encoding == "base64") $file_str = base64_encode($file_str);
            $this->att_files[] = array(
                "data"=>chunk_split($file_str),
                "name"=>basename($file), 
                "cont_type"=>$type, 
                "trans_enc"=>$encoding,
                "disposition"=>$dispo);

        }
    }

    function add_html_image($img_name) {
        $file_str = $this->get_file_data($img_name);
        $img_dim = getimagesize($img_name);
        if ($file_str == "") {
            return;
        } else {
            $this->html_images[] = array(
                "data"=>chunk_split(base64_encode($file_str)),
                "name"=>basename($img_name), 
                "cont_type"=>$img_dim['mime'],
                "cid"=>md5(uniqid(time()))."@".$_SERVER['SERVER_NAME']);
        }
    }

    function create_stand_headers() {
        if ($this->from_name != "") {
            $headers = "From: ".$this->from_name." <".$this->from_mail.">".LIBR;
            $headers .= "Reply-To: ".$this->from_name." <".$this->from_mail.">".LIBR;
        } else {
            $headers = "From: ".$this->from_mail.LIBR;
            $headers .= "Reply-To: ".$this->from_mail.LIBR;
        }
        if ($this->mail_cc != "") $headers .= "Cc: ".$this->mail_cc.LIBR;
        if ($this->mail_bcc != "") $headers .= "Bcc: ".$this->mail_bcc.LIBR;
        $headers .= sprintf("Message-ID: <%s@%s>%s", md5(uniqid(time())), $_SERVER['SERVER_NAME'], LIBR);
        $headers .= "X-Priority: ".PRIORITY.LIBR;
        $headers .= "X-Mailer: Attachment Mailer [version 1.2]".LIBR;
        $headers .= "MIME-Version: 1.0".LIBR;
        return $headers;
    }

    function create_html_image($img_array) {
        $img = "Content-Type: ".$img_array['cont_type'].";".LIBR.chr(9)." name=\"".$img_array['name']."\"".LIBR;
        $img .= "Content-Transfer-Encoding: base64".LIBR;
        $img .= "Content-ID: <image".$img_array['cid'].">".LIBR;
        $img .= "Content-Disposition: inline;".LIBR.chr(9)." filename=\"".$img_array['name']."\"".LIBR.LIBR;
        $img .= $img_array['data'];
        return $img;        
    }

    function create_attachment($data_array) {
        $att = "Content-Type: ".$data_array['cont_type'].";".LIBR.chr(9)." name=\"".$data_array['name']."\"".LIBR;
        $att .= "Content-Transfer-Encoding: ".$data_array['trans_enc'].LIBR;
        $att .= "Content-Disposition: ".$data_array['disposition'].";".LIBR.chr(9)." filename=\"".$data_array['name']."\"".LIBR.LIBR;
        $att .= $data_array['data'];
        return $att;        
    }

    function create_html_body() {
        $html = "Content-Type: text/html; charset=".ENCODING.LIBR;
        $html .= "Content-Transfer-Encoding: ".TRANS_ENC.LIBR.LIBR;
        foreach ($this->html_images as $img) {
            $this->html_body = str_replace($img['name'], "cid:image".$img['cid'], $this->html_body);
        }
        $html .= $this->html_body;
        return $html.LIBR.LIBR;
    }

    function build_message() {
        $this->headers = $this->create_stand_headers();
        $msg = "";
        $is_html = ($this->html_body != "") ? true : false;
        $is_attachment = (count($this->att_files) > 0) ? true : false;
        $is_images = (count($this->html_images) > 0) ? true : false;
        if ($is_attachment) {
            $this->uid = md5(uniqid(time()));
            $this->headers .= "Content-Type: multipart/mixed;".LIBR.chr(9)." boundary=\"".$this->uid."\"".LIBR.LIBR;
            $this->headers .= "This is a multi-part message in MIME format.".LIBR;
            if (!$is_html) {
                $msg .= "--".$this->uid.LIBR;
            } else {
                $this->headers .= "--".$this->uid.LIBR;
            }
        } 
        if ($is_html) {
            $this->alternative_uid = md5(uniqid(time()));
            $this->headers .= "Content-Type: multipart/alternative;".LIBR.chr(9)." boundary=\"".$this->alternative_uid."\"".LIBR.LIBR;
            if (!$is_attachment) {
                $this->headers .= "This is a multi-part message in MIME format.".LIBR;
            }
            $msg .= LIBR."--".$this->alternative_uid.LIBR;
        }
        $body_head = "Content-Type: text/plain; charset=".ENCODING."; format=flowed".LIBR;
        $body_head .= "Content-Transfer-Encoding: ".TRANS_ENC.LIBR.LIBR;
        if (!$is_attachment && !$is_html) {
            $this->headers .= $body_head;
        } else {
            $msg .= $body_head;
        }
        $msg .= trim($this->text_body).LIBR.LIBR;
        if ($is_html) {
            $msg .= "--".$this->alternative_uid.LIBR;
            if ($is_images) {
                $this->related_uid = md5(uniqid(time()));
                $msg .= "Content-Type: multipart/related;".LIBR.chr(9)." boundary=\"".$this->related_uid."\"".LIBR.LIBR.LIBR;
                $msg .= "--".$this->related_uid.LIBR;
                $msg .= $this->create_html_body();
                foreach ($this->html_images as $img) {
                    $msg .= "--".$this->related_uid.LIBR;
                    $msg .= $this->create_html_image($img);
                }
                $msg .= LIBR."--".$this->related_uid."--";
            } else {
                $msg .= $this->create_html_body();
            }
            $msg .= LIBR.LIBR."--".$this->alternative_uid."--".LIBR.LIBR;
        }
        if ($is_attachment) {
            foreach ($this->att_files as $att) {
                $msg .= "--".$this->uid.LIBR;
                $msg .= $this->create_attachment($att);
            }
            $msg .= "--".$this->uid."--";
        }
        return $msg;        
    }

    function process_mail() {
        if (!$this->valid_mail_adresses) return;
        if (mail($this->mail_to, $this->mail_subject, $this->build_message(), $this->headers, "-f".$this->webmaster_email)) {
            $this->msg[] = "Your mail is succesfully submitted.";
            return true;
        } else {
            $this->msg[] = "Error while sending you mail.";
            return false;
        }
    }
}

$test = new attach_mailer($name = "Olaf", $from = "[email protected]", $to = "[email protected]", $cc = "", $bcc = "", $subject = "Test text email with attachments");
$test->text_body = "...Some body text\n\n the admin";
$test->add_attach_file("uploads/admin_doc.docx");
//$test->add_attach_file("ip2nation.zip"); 
$test->process_mail();
echo $test->get_msg_str();

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

如何在php中的单个邮件中发送多个附件

来自分类Dev

附加多个文件时,Unix邮件中的附件丢失

来自分类Dev

尝试通过邮件发送多个文本附件

来自分类Dev

使用Python中的Mailgun发送多个文件附件

来自分类Dev

PHPMailer发送邮件,但不附带文件附件

来自分类Dev

PHPMailer发送邮件,但不附带文件附件

来自分类Dev

在电子邮件附件中仅获取一个文件,而不是多个附件

来自分类Dev

使用 PHP 在邮件中发送附件

来自分类Dev

发送多个附件 PHP imap 只发送 1 个附件

来自分类Dev

如何使用PHP生成多个PDF并通过电子邮件附件发送?

来自分类Dev

如何从邮件中重新发送附件?

来自分类Dev

使用C#发送的电子邮件中,附件中的zip文件空着

来自分类Dev

打开多个文件以使用mail()php发送邮件

来自分类Dev

发送带有多个PDF文件的邮件php

来自分类Dev

获取AEM自适应表单中的文件附件路径以在JSP中使用以发送带有附件的电子邮件?

来自分类Dev

CSV文件附件无法在Android中随电子邮件一起发送

来自分类Dev

使用OpenCart 2.1.1.1用PHP发送附件

来自分类Dev

使用ajax将多个文件附件发送到php文件

来自分类Dev

PHP邮件PDF附件的文件损坏

来自分类Dev

PHP电子邮件附件文件

来自分类Dev

如何在Java中发送带有多个附件的邮件

来自分类Dev

Excel VBA发送带有多个附件的电子邮件

来自分类Dev

使用smpt发送带有多个附件的电子邮件

来自分类Dev

使用 PHP 邮件发送的邮件未显示在我的邮件已发送文件夹中

来自分类Dev

用 HTML 发送 PHP 邮件

来自分类Dev

如何使用Mailgun(来自Python)发送带有多个附件和自定义文件名的邮件

来自分类Dev

用PHP发送电子邮件中的标题问题

来自分类Dev

发送python电子邮件时添加excel文件附件

来自分类Dev

Java发送带有附件的文件的电子邮件

Related 相关文章

  1. 1

    如何在php中的单个邮件中发送多个附件

  2. 2

    附加多个文件时,Unix邮件中的附件丢失

  3. 3

    尝试通过邮件发送多个文本附件

  4. 4

    使用Python中的Mailgun发送多个文件附件

  5. 5

    PHPMailer发送邮件,但不附带文件附件

  6. 6

    PHPMailer发送邮件,但不附带文件附件

  7. 7

    在电子邮件附件中仅获取一个文件,而不是多个附件

  8. 8

    使用 PHP 在邮件中发送附件

  9. 9

    发送多个附件 PHP imap 只发送 1 个附件

  10. 10

    如何使用PHP生成多个PDF并通过电子邮件附件发送?

  11. 11

    如何从邮件中重新发送附件?

  12. 12

    使用C#发送的电子邮件中,附件中的zip文件空着

  13. 13

    打开多个文件以使用mail()php发送邮件

  14. 14

    发送带有多个PDF文件的邮件php

  15. 15

    获取AEM自适应表单中的文件附件路径以在JSP中使用以发送带有附件的电子邮件?

  16. 16

    CSV文件附件无法在Android中随电子邮件一起发送

  17. 17

    使用OpenCart 2.1.1.1用PHP发送附件

  18. 18

    使用ajax将多个文件附件发送到php文件

  19. 19

    PHP邮件PDF附件的文件损坏

  20. 20

    PHP电子邮件附件文件

  21. 21

    如何在Java中发送带有多个附件的邮件

  22. 22

    Excel VBA发送带有多个附件的电子邮件

  23. 23

    使用smpt发送带有多个附件的电子邮件

  24. 24

    使用 PHP 邮件发送的邮件未显示在我的邮件已发送文件夹中

  25. 25

    用 HTML 发送 PHP 邮件

  26. 26

    如何使用Mailgun(来自Python)发送带有多个附件和自定义文件名的邮件

  27. 27

    用PHP发送电子邮件中的标题问题

  28. 28

    发送python电子邮件时添加excel文件附件

  29. 29

    Java发送带有附件的文件的电子邮件

热门标签

归档