邮件设置求助,有关html和text格式的
发布网友
发布时间:2022-05-01 16:23
我来回答
共1个回答
热心网友
时间:2022-06-19 12:00
邮件有很多中显示形式,显示形式主要取决于其MIME类型,每种显示形式有相应的内容,他们之间用分割符号分开。一下是一个构造邮件主体内容的类:
<?php
//
// Definition of tsNotificationMailHandle
r class
//
// Created on: <01-Nov-2002 13:51:17
amos>
//
// SOFTWARE NAME: eZ Publish
// SOFTWARE RELEASE: 4.0.1
// BUILD VERSION: 22260
// COPYRIGHT NOTICE: Copyright (C) 1999-2008 eZ Systems AS
// SOFTWARE LICENSE: GNU General Public License v2.0
// NOTICE: >
// This program is free
software; you can redistribute it and/or
// modify it under the terms of
version 2.0 of the GNU General
// Public License as published
by the Free Software Foundation.
//
// This program is distributed
in the hope that it will be useful,
// but WITHOUT ANY WARRANTY;
without even the implied warranty of
// MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the
// GNU General Public License
for more details.
//
// You should have received a
copy of version 2.0 of the GNU General
// Public License along with
this program; if not, write to the Free
// Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston,
// MA 02110-1301, USA.
//
//
class tsNotificationMailHandler
{
public
function __construct( $plainText = false, $htmlText = false )
{
$this->setPlainText( $plainText );
$this->setHtmlText( $htmlText );
}
function
setPlainText( $text )
{
$this->plainText = $text;
}
function
setHtmlText( $text )
{
$this->htmlText = $text;
}
function
getRandomBoundary($offset = 0)
{
srand(time()+$offset);
return ( "----" . ( md5( rand() ) ) );
}
function
build()
{
$mailBody = false;
$bodyBoundary = $this->getRandomBoundary();
//add the
body and boundary of the mail
if
($this->plainText != "")
{
$mailBody .=
"--".$bodyBoundary. "\n";
$textHeader
= $this->formatTextHeader();
$mailBody .=
$textHeader;
}
if
($this->htmlText != "")
{
$mailBody .=
"--".$bodyBoundary. "\n";
$htmlHeader
= $this->formatHTMLHeader();
$mailBody .=
$htmlHeader;
}
$mailBody .=
"\n--".$bodyBoundary. "--";
return
$mailBody ;
}
function
formatTextHeader()
{
$outTextHeader = "";
$outTextHeader .= "Content-Type: text/plain;
charset=utf-8\n";
$outTextHeader .= "Content-Transfer-Encoding: 7bit\n\n";
$outTextHeader .= $this->plainText. "\n";
return $outTextHeader;
}
function
formatHTMLHeader()
{
$outHTMLHeader = "";
$outHTMLHeader .= "Content-Type: text/html; charset=utf-8\n";
$outHTMLHeader .= "Content-Transfer-Encoding: 7bit\n\n";
$outHTMLHeader .= $this->htmlText. "\n";
return $outHTMLHeader;
}
private
$htmlText;
private
$plainText;
}
两个私有属性分别存放两种类型的内容,内容是在构造函数中传参的,调用自身的build()方法去构造最终的显示内容并且返回。
在外部可以这样调用:
$mail->extractEmail( $addressItem, $email, $name
);
$mail->addBcc( $email, $name );
$mail->addReceiver( $email, $name );
$mail->setContentType('multipart/alternative','utf-8',false,false,$parameters['boundary']);
$mail->setSender( $emailSender );
$mail->setSubject( $subject );
$mail->setBody( $body );
这个$body 就是调用类中的build方法返回的内容。
这个$mail对象的类型不定,这儿只是一个例子,mail的具体类型可以自己写,无非就是设置头部,设置发送人,接受人之类的信息。
需要特别注意的是内容是纯文本和html两种格式的头部设置邮件的类型为:multipart/alternative;另一个参数是分割服,boundary,这个分割符可以调用上边类的方法getRandomBoundary()。
最终邮件源码发送出来以后就是下面这样的 形式,分割符号把不同的内容分割开来:
From - Thu Oct 14 14:44:52 2010
X-Account-Key: account3
X-UIDL: 1tbiEAVqTUiNYqIyFAAAsR
X-Mozilla-Status: 0001
X-Mozilla-Status2: 00000000
X-Mozilla-Keys:
Received: from xiuji.cai (unknown [220.248.96.170])
by mx14 (Coremail) with SMTP id QMCowLBbLySDprZMTt4mBw--.179S2;
Thu, 14 Oct 2010 14:43:16 +0800 (CST)
Received: from xiuji.cai (localhost [127.0.0.1])
by xiuji.cai (8.14.3/8.14.3) with ESMTP id o9E6hkhO024008
for <xiujixin@163.com>; Thu, 14 Oct 2010 14:43:46 +0800
Received: (from hannes@localhost)
by xiuji.cai (8.14.3/8.14.3/Submit) id o9E6hkIe024007;
Thu, 14 Oct 2010 14:43:46 +0800
X-Authentication-Warning: xiuji.cai: hannes set sender to jinxiudaxin@hotmail.com using -f
To: xiujixin@163.com
Subject: Article "rrrrrrr" was published [admin.trustedsources.co.uk - China Blog]
Date: Thu, 14 Oct 2010 06:43:46 +0000
From: Hannes Cai <jinxiudaxin@hotmail.com>
MIME-Version: 1.0
Content-Type: multipart/alternative; charset=utf-8; boundary="----c0459eef8a3391dc84f2418f0d8722a1"
Content-Transfer-Encoding:
8bit
Content-Disposition: inline
User-Agent: eZ Publish, Version 4.0.1
Message-ID: <node.16075.eznotification@admin.trustedsources.co.uk>
References: <node.1.eznotification@admin.trustedsources.co.uk>
<node.2.eznotification@admin.trustedsources.co.uk>
<node.15808.eznotification@admin.trustedsources.co.uk>
<node.15809.eznotification@admin.trustedsources.co.uk>
In-Reply-To:
<node.15809.eznotification@admin.trustedsources.co.uk>
X-Coremail-Antispam: 1Uf129KBjDUn29KB7ZKAUJUUUUU529EdanIXcx71UUUUU7v73
VFW2AGmfu7jjvjm3AaLaJ3UbIYCTnIWIevJa73UjIFyTuYvjxUyppBDUUUU
------c0459eef8a3391dc84f2418f0d8722a1
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 7bit
This email is to inform you that a new item has been published at admin.trustedsources.co.uk.
The item can be viewed by using the URL below.
rrrrrrr - Hannes Cai http://admin.trustedsources.co.uk/blog/china/rrrrrrr
If you do not want to continue receiving these notifications,
please change your settings at:
http://admin.trustedsources.co.uk/notification/settings
-----------------
admin.trustedsources.co.uk notification system
------c0459eef8a3391dc84f2418f0d8722a1
Content-Type: text/html; charset=utf-8
Content-Transfer-Encoding: 7bit
<html>
<head>
</head>
<body>
<font color="#00ff00">this is the html template!</font>
</body>
</html>
------c0459eef8a3391dc84f2418f0d8722a1--