PHP的邮件功能似乎删除了换行符和标签

时间:2013-04-08 04:54:03

标签: php email newline line-breaks

我有这个助手类,我用它通过PHP发送电子邮件:

<?php

class Mailsend {

    public $from;
    public $to;
    public $subject;
    public $message;

    public static function mail() {
        return new Mailsend();
    }

    public function from($from) {
        $this->from = $from;
        return $this;
    }

    public function fromName($fromName) {
        $this->fromName = $fromName;
        return $this;
    }

    public function to($to) {
        $this->to = $to;
        return $this;
    }

    public function subject($subject) {
        $this->subject = $subject;
        return $this;
    }

    public function message($message) {
        $this->message = $message;
        return $this;
    }

    public function send() {
        $mailFrom = $this->from;
        $mailFromName = $this->fromName;
        $mailTo = $this->to;
        $mailSubject = $this->subject;

        $mailSignature = "\n\n-- \n";
        $mailSignature .= "Vaš, Format.ba - Primjenjene Vještine.\n";
        $mailSignature .= "Za više informacija posjetite nas na: http://www.format.ba/\n";

        $mailBody = $this->message . "\n";
        $mailBody .= $mailSignature;

        $mailHeader = "From: $mailFromName <$mailFrom>\r\n";
        $mailHeader .= "MIME-Version: 1.0\r\n";
        $mailHeader .= "Content-type: text/html; charset=UTF-8\r\n";
        $mailHeader .= "Reply-To: $mailFromName <$mailFrom>\r\n";
        $mailHeader .= "X-Mailer: Mailsend.php\r\n";
        $mailHeader .= "X-Sender-IP: {$_SERVER['REMOTE_ADDR']}\r\n";
        $mailHeader .= "Bcc: $mailFromName <$mailFrom>\r\n";

        $mailParams = "-f$mailFrom";

        $mailBody = str_replace('  ', '', $mailBody);

        return mail($mailTo, $mailSubject, $mailBody, $mailHeader, $mailParams);
    }

    public function valid($email) {
        if (!preg_match("/^[^@]{1,64}@[^@]{1,255}$/", $email)) {
            return false;
        }
        $email_array = explode("@", $email);
        $local_array = explode(".", $email_array[0]);
        for ($i = 0; $i < sizeof($local_array); $i++) {
            if (!preg_match("/^(([A-Za-z0-9!#$%&'*+\/=?^_`{|}~-][A-Za-z0-9!#$%&'*+\/=?^_`{|}~\.-]{0,63})|(\"[^(\\|\")]{0,62}\"))$/", $local_array[$i])) {
                return false;
            }
        }
        if (!preg_match("/^\[?[0-9\.]+\]?$/", $email_array[1])) {
            $domain_array = explode(".", $email_array[1]);
            if (sizeof($domain_array) < 2) {
                return false;
            }
            for ($i = 0; $i < sizeof($domain_array); $i++) {
                if (!preg_match("/^(([A-Za-z0-9][A-Za-z0-9-]{0,61}[A-Za-z0-9])|([A-Za-z0-9]+))$/", $domain_array[$i])) {
                    return false;
                }
            }
        }

        return true;
    }

}

它可以工作,我写了它,因此很容易使用,就像这样的实例:

$message = "Hello, " . $model->fullname . ",

                        Welcome to our Website, Domain.tdl. You are now able to use all of our services which we provide here.

                        Visit: www.domain.tdl - for more information and news about our services!

                        Greets!";

Mailsend::mail()->
    from('info@domain.tdl')->
    fromName('Domain.tdl - Our Website')->
    to('someother@email.tdl')->
    subject('Welcome to Domain.tdl - Our Website')->
    message(trim($message))->send();

正如你从这个确切的例子中可以看到的那样,有很多空格被“空格”缩进,而不是标签。

当我将该消息发送到我的一个电子邮件时,我得到了这个输出:

  

您好,Zlatan欢迎访问我们的网站Domain.tdl。你现在能够   使用我们在此提供的所有服务。访问:www.domain.tdl -   有关我们服务的更多信息和新闻!迎接!

它只是以某种方式剥离了所有换行符和超过两个长度的空格,并将它们合并在一起,因此我失去了原来的电子邮件格式。

我认为Mailsend中的这一行可能会导致问题:

$mailBody = str_replace('  ', '', $mailBody);

但情况并非如此,到目前为止,我还没有找到任何理由,为什么会这样,并且无法阻止它继续下去。

有没有人过期类似的东西?我可能在自己的代码中遗漏了什么吗?

3 个答案:

答案 0 :(得分:4)

<br>

一起使用
$message = "Hello, " . $model->fullname . "<br>,

                    Welcome to our Website, Domain.tdl. You are now able to use all of our services which we provide here.<br>

                    Visit: www.domain.tdl - for more information and news about our services!<br>

                    Greets!";

答案 1 :(得分:2)

使用<br />代替\r\n

添加此标题以使您的内容HTML格式化:

Content-type: text/html; charset=UTF-8

如果要插入标签,请改为使用多个&nbsp;

答案 2 :(得分:2)

您正在使用以下标题:

$mailHeader .= "Content-type: text/html; charset=UTF-8\r\n";

但即将发送纯文本电子邮件。将其更改为

$mailHeader .= "Content-type: text/plain; charset=UTF-8\r\n";

或将您的电子邮件格式化为html