duanchi6377 2015-10-08 09:16
浏览 28
已采纳

php邮件在某些服务器上发送空白内容

I created a class to simplify the use of mail() (with the possibility to add attachments, switch between text and html mode etc).

I'm currently facing this problem :

  • When I try to send a mail from my local server (xampp), everything works fine.
  • When I try to send a mail from a real apache server, the mail is correctly sent, but the content is empty. Everything else is fine, though (sender, recipient, CC, ...).

From the SO questions about this kind of issues, I think that it is related to CRLF, but I can't find exactly where the problem is (and none of the SO questions helped).

This is an example of how I use it :

<?php
    $mail = new Mail();
    $mail->from('** adress **', '** name **');
    $mail->addTo($user->mail);
    $mail->addCC('** CC adress **', '** name **');

    $mail->html = true;
    $mail->subject = '** subject of the mail **';
    $mail->content = '** HTML code **'; // This HTML is valid
    $mail->send();

And here is the class :

<?php
define('CRLF', "
");

// This is used to send mail
class Mail extends Controller
{
    // Boundary
    private $boundary = null;

    // Recipients, RFC 2822
    public $to = [];
    // Subject of the mail, RFC 2047
    public $subject = '';

    // Content of the mail
    public $message = '';
    // Content is HTML ?
    public $html = false;
    // Attachments
    public $attachments = [];

    // Additional headers
    public $headers = [];

    public function __construct()
    {
        $this->boundary = '_'.md5(uniqid(rand())).'_';
        $this->headers = [
            'MIME-Version' => '1.0',
            'Content-Type' => 'multipart/mixed; boundary="'.$this->boundary.'"'
        ];
    }

    public function addTo($mail, $name = null)
    {
        $this->to[] = ($name ? $name.' ' : '').'<'.$mail.'>';
        return $this;
    }

    public function from($mail, $name = null)
    {
        $this->headers['From'] = ($name ? $name.' ' : '').'<'.$mail.'>';
        return $this;
    }

    public function replyTo($mail, $name = null)
    {
        $this->headers['Reply-to'] = ($name ? $name.' ' : '').'<'.$mail.'>';
        return $this;
    }

    public function addCC($mail, $name = null)
    {
        $this->headers['Cc'][] = ($name ? $name.' ' : '').'<'.$mail.'>';
        return $this;
    }

    public function addBC($mail, $name = null)
    {
        $this->headers['Bcc'][] = ($name ? $name.' ' : '').'<'.$mail.'>';
        return $this;
    }

    public function addAttachment($fileName, $name = null)
    {
        $this->attachments[] = [$fileName, ($name === null ? pathinfo($fileName, PATHINFO_FILENAME) : $name)];
        return $this;
    }

    public function send()
    {
        // Add main content
        $fullMessage = '--'.$this->boundary.CRLF.
            'Content-Type: '.($this->html ? 'text/html' : 'text/plain').'; charset=UTF-8'.CRLF.CRLF.
            $this->content;

        // Attachments
        foreach ($this->attachments as $attachment)
        {
            if (!is_readable($attachment[0]))
                throw new Exception('Cannot read file "'.$attachment[0].'"');
            $fullMessage .= CRLF.CRLF.'--'.$this->boundary.$thids->nl.
                'Content-Type: '.mime_content_type($attachment[0]).'; name="'.$attachment[1].'"'.CRLF.
                'Content-Transfer-Encoding: base64'.CRLF.
                'Content-Disposition: attachment; filename="'.$attachment[0].'"'.CRLF.CRLF.
                chunk_split(base64_encode(file_get_contents($attachment[0]))).CRLF;
        }

        // Adding the headers
        $fullHeaders = '';
        foreach ($this->headers as $name => $content)
        {
            if (is_array($content))
                $fullHeaders .= $name.': '.implode(', ', $content).CRLF;
            else
                $fullHeaders .= $name.': '.$content.CRLF;
        }

        return mail(implode(',', $this->to), utf8_decode($this->subject), $fullMessage, $fullHeaders);
    }
}
  • 写回答

1条回答 默认 最新

  • doubeng9567 2015-11-04 11:49
    关注

    The problem came from and . I solved it by replacing all the in the content of the mail by (not in the headers).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?