1

我有这个帮助类,我用它来通过 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);

但事实并非如此,到目前为止,我还没有找到任何原因导致这种情况发生并且无法阻止它继续进行。

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

4

3 回答 3

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!";
于 2013-04-08T04:57:25.320 回答
2

使用<br />而不是\r\n.

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

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

当您要插入标签时,请改用多个&nbsp;

于 2013-04-08T05:04:11.727 回答
2

您正在使用以下标头:

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

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

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

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

于 2013-04-08T05:04:22.680 回答