0

嗨,我在这里学习了一个教程和几个答案,但是当我尝试将带有附件的电子邮件发送到我的 gmail 作为测试时,它仍然不起作用。电子邮件:

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

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

"

内容类型:文本/html;

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

您好,这是一条测试消息--1575d7e57c4ae2d9bdd29f847d8f48ea

内容类型:图片/jpeg;name="/tmp/php7LfGKW" Content-Transfer-Encoding: base64 Content-Disposition: attachment /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;
    }
4

2 回答 2

1

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

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

它非常简洁且切中要害 - 尽管它非常混乱。

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

于 2012-11-22T13:43:38.370 回答
0

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

PHP-Mixed . . .
于 2012-11-22T13:47:39.120 回答