使用mail()发送电子邮件

时间:2014-05-31 23:15:28

标签: php email html-email

我正在制作电子邮件发件人类,我遇到了一些问题:

  • 某些电子邮件服务(例如Russian mail.ru)不了解邮件的编码并在Windows-1251字符集中显示我的UTF-8邮件。 GMail就是这样。
  • 所有电子邮件服务都将text / html显示为纯文本。
  • 也打破了多部分邮件。

另外我应该注意,我是使用GMail SMTP服务器从OpenServer上的本地服务器构建发送它 我的意见问题是 mybullshitcode 或smth中的内容类型标题。

Theres a code:

class _EMAIL
{
    protected $type;
    protected $to;
    protected $subject;
    protected $plain;
    protected $html;
    protected $charset;
    protected $headers = array(
    'MIME-Version'  => '1.0',
    // 'Content-type'   => '',
    // 'From'           => '',
    // 'Cc'         => '',
    // 'Bcc'            => '',
    // 'Reply-To'       => '',
    // 'Subject'        => '',
    // 'Return-Path'    => ''
    );

    public function __construct($to, $subj, $datacharset, $sendcharset)
    {
        $this->to = self::hencode($to, $datacharset, $sendcharset);
        $this->subject = self::hencode($subj, $datacharset, $sendcharset);
        $this->headers['Subject'] = $this->subject;
        $this->datacharset = $datacharset;
        $this->sendcharset = $sendcharset;
    }
    public function setHeader($el, $val)
    {
        $this->headers[$el] = $val;
    }
    public function setPlain($message)
    {
        $this->plain = iconv($this->datacharset, $this->sendcharset, $message);
    }
    public function setHTML($message)
    {
        $this->html = iconv($this->datacharset, $this->sendcharset, $message);
    }


    /* Static */
    public static function send($to, $mail)
    {
        if ($mail->plain !== null && $mail->html !== null)
        {
            $r = sha1(uniqid());
            $mail->headers['Content-Type'] = "multipart/alternative; boundary=$r";
            $message = "--$r
Content-Type: text/plain; charset=".$mail->sendcharset."
Content-Transfer-Encoding: 7bit

".$mail->plain."

--$r
Content-Type: text/html; charset=".$mail->sendcharset."
Content-Transfer-Encoding: 7bit

".$mail->html."

--$r--";
        } else if ($mail->html !== null) {
            $mail->headers['Content-Type'] = 'text/html; charset='.$mail->sendcharset;
            $message = $mail->html;
        } else if ($mail->plain !== null) {
            $mail->headers['Content-Type'] = 'text/plain; charset='.$mail->sendcharset;
            $message = $mail->plain;
        } else {
            return FALSE;
        }
        $headers = '';
        $mail->to = $mail->to." <$to>";
        $mail->headers['X-Mailer'] = 'PHP/'.phpversion().' - Blabla '.Core::build_info();
        foreach ($mail->headers as $key=>$val)
        {
            $headers .= $key.': '.$val.'\r\n';
        }
        return mail($to, $mail->subject, $message, $headers);
    }

    private static function hencode($str, $data_charset, $send_charset=FALSE)
    {
        if ($send_charset && $data_charset != $send_charset)
        {
            $str = iconv($data_charset, $send_charset, $str);
        } else {
            $send_charset = $data_charset;
        }
        return '=?'.$send_charset.'?B?'.base64_encode($str).'?=';
    }
}

我尝试在base64_encode中创建subjto标头 - 似乎没有希望。

所有的帮助将不胜感激!

UPD:Quoted-printable variant

    public function setPlain($message)
    {
        $this->plain = quoted_printable_encode(iconv($this->datacharset, $this->sendcharset, $message));
    }
    public function setHTML($message)
    {
        $this->html = quoted_printable_encode(iconv($this->datacharset, $this->sendcharset, $message));
    }

            $message = "--$r
Content-Type: text/plain; charset=".$mail->sendcharset."
Content-Transfer-Encoding: quoted-printable

".$mail->plain."

--$r
Content-Type: text/html; charset=".$mail->sendcharset."
Content-Transfer-Encoding: quoted-printable

".$mail->html."

--$r--";

UPD2:使用PHPMailer ... GMail电子邮件主题与西里尔字符最终。

  

这是主题。 ÐÑ,озаголовок!

身体可以接受:

  

这是以粗体显示的HTML邮件正文! Такоесообщение!

代码:

$mail = new _EMAIL;
$mail->From = 'admin@ss133d.ru';
$mail->FromName = 'SS133D Administration';
$mail->addAddress($email, $login);
$mail->addReplyTo('admin@ss133d.ru', 'SS133D Administration');
$mail->isHTML(true);

$mail->Subject = 'Here is the subject. Это заголовок!';
$mail->Body    = 'This is the HTML message body <b>in bold!</b>. Такое <b>сообщение</b>!';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients. А это без HTML!';

所以也许我需要使用iconv? Theres subj将UTF-8转换为Windows-1252 ......

2 个答案:

答案 0 :(得分:2)

您是否考虑过使用phpmailer Here

它为您完成所有工作但需要在配置值中使用您的电子邮件和电子邮件密码。但它的安全和您的信息不会被释放,因此您可以选择使用。

答案 1 :(得分:0)

根据http://otvet.mail.ru/question/92734494,你应该做两件事:

$headers[] = "Content-type: text/plain; charset=utf-8"; 

并将html页面的编码设置为utf-8。因此,您应该将以下内容添加为PHP代码中的第一行。

header('Content-type: text/plain; charset=utf-8');

根据sending mail in UTF-8,您还应该使用quoted_printable_encode()并使用Content-Transfer-Encoding: quoted-printable代替Content-Transfer-Encoding: 7bit