【问题标题】:PHP Mail() function dealing with HTML / Text and attachments处理 HTML / 文本和附件的 PHP Mail() 函数
【发布时间】:2013-01-10 13:03:21
【问题描述】:

我正在为我的电子邮件使用 PHP mail() 函数,现在我想添加新内容但不知道如何处理..

我创建的基本 PHP 邮件类如下:

<?php

class Send_Mail {

    private $to = array();
    private $subject = null;
    private $from = null;
    private $replyTo = null;
    private $type = self::TEXT;

    const HTML = 0;
    const TEXT = 1;

    public function __construct() {
    }

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

    public function setType($type) {
        if($type == self::HTML || $type == self::TEXT) {
            $this->type = $type;
        }
    }

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

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

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

    public function send() {

        if  (   $this->to == array()    || 
                $this->from == null     ||
                $this->message == null
            ) {
                trigger_error("The email can't be sent ! One of the mandatory fields at least isn't set !");
                return false;
        }
        else {

            $this->to       = implode(',', $this->to);

            $this->subject = ($this->subject == null) ? "" : $this->subject;

            $headers    = array();
            $headers[]  = "MIME-Version: 1.0" . "\r\n";

            if($this->type == self::HTML) {
                $headers[]  = "Content-type: text/html; charset=UTF-8" . "\r\n";
            } else {
                $headers[]  = "Content-type: text/plain; charset=UTF-8" . "\r\n";
            }

            $headers[]  = "To: " . $this->to . "\r\n";
            $headers[]  = "From: " . $this->from . "\r\n";


            $headers[]  = "Reply-To: " . $this->from . "\r\n";

            return mail($this->to, $this->subject, $this->message, implode('', $headers));
        }
    }

}

*/
?>

所以基本上我无法发送附件.. 我还想添加 multiple BCC 和 multiple CC 支持。

除了 PHP 文档中所说的行不应超过 70 个字符这一事实外。如果我使用 wordwrap 会破坏 HTML 标记,我该如何处理? :(

【问题讨论】:

  • 我会寻找一个很好的类来做到这一点
  • 不要使用mail() 函数,尤其是对于附件等复杂任务。使用像 phpMailer 这样的像样的邮件程序类。另请参阅此答案:stackoverflow.com/questions/12301358/…

标签: php email word-wrap


【解决方案1】:

提供带有附件的电子邮件并非易事,因为您需要实现多个部分正文。

最好使用库来解决这个问题。例如:

http://framework.zend.com/manual/1.12/en/zend.mail.attachments.html

https://pear.php.net/manual/de/package.mail.mail-mime.example.php

要不然你自己写

附件,看这个

http://www.shayanderson.com/php/simple-php-mail-class-with-attachment.htm

有关 CC 或 BCC 等其他电子邮件标题,请阅读此内容

http://www.htmlite.com/php029.php

【讨论】:

  • 我不喜欢框架,Zend 为您提供了大量的可能性,但它并不针对一种特定类型的网站,因此会导致大量内存使用。在大型网站上使用它肯定会导致内存问题,但这是另一个问题!
  • 我仍然建议你使用一个框架(在 zend 的情况下,你只需要使用你需要的部分,来处理整个 MVC 的东西)。但我用一些提示来提升我的答案,以提升你自己的班级。
猜你喜欢
  • 2011-05-18
  • 2021-06-03
  • 1970-01-01
  • 2020-01-27
  • 1970-01-01
  • 2017-07-25
  • 2013-01-06
  • 1970-01-01
  • 2011-09-10
相关资源
最近更新 更多