发送带附件的电子邮件

时间:2012-11-22 13:35:41

标签: php function email header attachment

您好我已经在这里遵循了一个教程和几个答案,但是当我尝试将带有附件的电子邮件发送到我的Gmail时作为测试它不起作用。电子邮件:

非洲侏儒刺猬俱乐部新闻信

admin@africanpygmyhedgehogclub.co.uk通过moth.xssl.net

  

Content-Type:text / html;

非洲侏儒刺猬俱乐部新闻   信

     

你好,这是一条测试信息   --1575d7e57c4ae2d9bdd29f847d8f48ea

     

内容类型:image / jpeg;名称= “/ TMP / php7LfGKW”   内容传输编码:base64内容 - 处置:附件   / 9J / 4AAQSkZJRgABAgEASABIAAD / 4RNGRXhpZgAATU0AKgAAAAgABwESAAMAAAABAAEAAAEaAAUA   AAABAAAAYgEbAAUAAAABAAAAagEoAAMAAAABAAMAAAExAAIAAAAUAAAAcgEyAAIAAAAUAAAAhodp   AAQAAAABAAAAnAAAAMgAAAAcAAAAAQAAABwAAAABQWRvYmUgUGhvdG9zaG9wIDcuMAAyMDA5OjA4   OjA4IDIxOjE1OjA5AAAAAAOgAQADAAAAAf // AACgAgAEAAAAAQAAAligAwAEAAAAAQAAAdAAAAAA   AAAABgEDAAMAAAABAAYAAAEaAAUAAAABAAABFgEbAAUAAAABAAABHgEoAAMAAAABAAIAAAIBAAQA   AAABAAABJgICAAQAAAABAAASGAAAAAAAAABIAAAAAQAAAEgAAAAB / 9J / 4AAQSkZJRgABAgEASABI   AAD / 7QAMQWRvYmVfQ00AAv / uAA5BZG9iZQBkgAAAAAH / 2wCEAAwICAgJCAwJCQwRCwoLERUPDAwP   FRgTExUTExgRDAwMDAwMEQwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwBDQsLDQ4NEA4OEBQO   ....

我的代码:

    if(isset($_POST['send_newsletter']))
    {
    $newsletter->store_form_values($_POST);
        $emails = explode(',', $newsletter->newsletter_emails);
    $file = $_FILES;

    $send_email = $newsletter->email_newsletter($emails, $file);

        echo $send_email;
    }

    public function email_newsletter($emails, $file)
    {
        //email address from
    $from = 'admin@africanpygmyhedgehogclub.co.uk';
    //subject
    $subject = "African Pygmy Hedgehog Club News Letter";

    $random_hash = "--" .  md5(date('r', time())) . "\r\n\n";
    $boundry_last = $random_hash;

    //turn emails array into csv
    $bcc = null;
    foreach($emails as $key => $value)
    {
        if($key == 'newsletter_sign_email')
        {
        $bcc .= $value . ',';
        } 
    }

    $bcc = rtrim($bcc);

    //headers
    $headers = 'From: ' . $from . "\r\n";
    $headers .= 'Reply-To: ' . $from . "\r\n";
    $headers .= 'BCC: ' . $bcc . "\r\n";
    $headers .= "MIME-Version: 1.0\r\n";
    $headers .= "Content-Type: multipart/mixed; boundary=\"PHP-mixed-" . $random_hash . "\"";

    //main message body
    $message = "Content-Type: text/html;" . "\r\n";
    $message .= "<h3>African Pygmy Hedgehog Club News Letter</h3>\n\n";
    $message .= $this->formatting($this->newsletter_content) . "\r\n";
    $message .= $random_hash;

    //attachment
    $file = $file['newsletter_attachment'];
    $filename = $file['tmp_name'];
    $filecontents = file_get_contents($filename);
    $message .= "Content-Type: " . $file['type'] . "; name=\"" . $filename . "\"" . "\r\n"; 
    $message .= "Content-Transfer-Encoding: base64" . "\r\n";  
    $message .= "Content-Disposition: attachment" . "\r\n";  
    $message .= chunk_split(base64_encode($filecontents)) . "\r\n";
    $message .= $boundry_last;

    if(mail($from, $subject, $message, $headers))
    {
    $result = "Successfully Sent News Letter";
    }
    else
    {
    $result = "Failed to Send New Letter, please try again!";
    }

    return $result;
    }

2 个答案:

答案 0 :(得分:1)

阅读本文后,您可以解决问题:

http://webcheatsheet.com/php/send_email_text_html_attachment.php

这是非常明智的,但是它非常混乱。

您最好的选择是使用http://swiftmailer.org/发送您的电子邮件,这些电子邮件也支持附件和更多内容。

答案 1 :(得分:0)

如果不运行代码,则看起来邮件消息和附件之间的边界未设置。它似乎没有 -

PHP-Mixed . . .