PHP Mail()函数处理HTML / Text和附件


PHP Mail() function dealing with HTML / Text and attachments

我正在使用PHP mail()函数来处理我的邮件,现在我想添加新的东西,但不知道如何处理。

我创建的基本PHP Mail类如下:

<?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));
        }
    }
}
*/
?>

所以基本上我不能发送附件。我还想添加多个密件和多个抄送支持。

以及PHP文档中所说的行长度不应超过70个字符的事实。如果我使用wordwrap,我该如何处理知道我可以打破HTML标签?(

提供带有附件的电子邮件不是那么容易的,因为您需要实现多个部分主体。

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

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

对于其他邮件标题,如抄送或密件,请阅读此

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