Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/email/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
php邮件在某些服务器上发送空白内容_Php_Email_Server - Fatal编程技术网

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

php邮件在某些服务器上发送空白内容,php,email,server,Php,Email,Server,我创建了一个类来简化邮件的使用,可以添加附件,在文本和html模式之间切换等等 我目前面临的问题是: 当我尝试从本地服务器xampp发送邮件时,一切正常。 当我尝试从真正的apache服务器发送邮件时,邮件已正确发送,但内容为空。其他一切都很好,尽管发件人、收件人、抄送等等。。。。 从关于这类问题的一些问题来看,我认为这与CRLF有关,但我无法找到问题的确切位置,所有这些问题都没有帮助 这是我如何使用它的一个示例: <?php $mail = new Mail(); $ma

我创建了一个类来简化邮件的使用,可以添加附件,在文本和html模式之间切换等等

我目前面临的问题是:

当我尝试从本地服务器xampp发送邮件时,一切正常。 当我尝试从真正的apache服务器发送邮件时,邮件已正确发送,但内容为空。其他一切都很好,尽管发件人、收件人、抄送等等。。。。 从关于这类问题的一些问题来看,我认为这与CRLF有关,但我无法找到问题的确切位置,所有这些问题都没有帮助

这是我如何使用它的一个示例:

<?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();
下面是课堂:

<?php
define('CRLF', "\r\n");

// 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);
    }
}
问题来自\r\n和\n。
我通过将邮件内容中的所有内容替换为\r\n而不是标题中的内容来解决此问题。

请考虑检查您的测试和生产环境配置。也许php构建中存在差异