问答文章1 问答文章501 问答文章1001 问答文章1501 问答文章2001 问答文章2501 问答文章3001 问答文章3501 问答文章4001 问答文章4501 问答文章5001 问答文章5501 问答文章6001 问答文章6501 问答文章7001 问答文章7501 问答文章8001 问答文章8501 问答文章9001 问答文章9501

邮件设置求助,有关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--
声明声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。E-MAIL:11247931@qq.com
人类如何才能悬浮在空中? - 知乎 人体悬浮术是真的存在吗 人体怎样才能真正的在空中悬浮 小孩c蛋白反应高是什么原因 (2014?南海区二模)如图所示,物重G为2000N,小红用800N的拉力花2s的时间... 苹果13系列升级iOS15.5好吗? 出生2o12年11月20日2点姓石光字辈取名 求龙凤胎名字:2012年6月21日(农历5月初3)凌晨0点40分出生,大的为龙,小... by和take有什么区别 takeby后面跟交通工具的区别 坐高铁从常州到北京需要多少时间,有采纳哦,急急急 哪位能帮我翻译一下 明天上海至北京的高铁中有哪几班会在徐州停靠? g122高铁有没有插座 开源是什么意思啊?随便说两句就行求大神帮助 如何在电脑上找我的iphone 小红伞 for linux这段readme是什么意思(关于安装) 办理了停息挂账五年以后还能再办信用卡吗 quartus II 5.0版本的过期了怎么办?如何才能长久使用? 徐州至北京高铁的到站时间及票价 开源的软件定义 徐州至北京高铁票价多少 哈弗H6(红标)虚拟仪表的开源软件包许可证使用信息是什么? third party software or free software license information这句话是什么意思啊 徐州到南京高铁车次 廊坊到北京的高铁有没有下午发车的 大概下午几点呢 垂延五笔怎么打 如何查阅历史上报纸的新闻 如何查大公报的旧报纸 苏州到北京高铁时刻表 苏州蠡口至南京高铁几点钟 如何申请 ILO3 的试用版本 LICENSE 以及如何在服务器上 如何通过电脑查找我的iphone Java程序制作完成后,怎么打包成EXE格式的啊? 城市户口子女继承农村父母房子拆迁时怎样赔 海底世界的大鱼吃小鱼怎么找不到? 农村继承舅舅房子,房产证也是自己名字的,拆迁赔偿时宅基地可以赔偿吗? 中国扇博物馆的展示陈列 已故父亲名下房产动迁怎么赔付? 海洋里的各种生物形成一种什么样的食物链? 杭州扇子博物馆在哪里 遗产房屋拆迁纠纷怎么解决 团扇展览的目的和要求 世界上有名的扇子博物馆有哪些? 城市人口继承的农村房屋拆迁时,和村民补偿一样吗 大鱼为什么会吃小鱼? 杭州剑伞扇博物馆地址 梦幻海底之大鱼吃小鱼第45关怎么过啊 如果父亲现在把老房子的继承权公证给我,这期间房子拆迁了怎么办? 动物都是怎样在海洋中生存、捕食、繁衍的?