在php中发邮件会用到mail函数 ,但是大多数情情况mail函数是不可用的,我们有插件来实例了phpmailer来实现发送邮件。
代码如下 |
复制代码 |
require("class.phpmailer.php");
$mail = new phpmailer();
$mail->charset='utf-8';
$address = $_post['address'];
$mail->issmtp(); // set mailer to use smtp
$mail->host = "mail.xxx.com"; // specify main and backup server
$mail->smtpauth = true; // turn on smtp authentication
$mail->username = "phpmailer@xxx.com"; // smtp username
$mail->password = "******"; // smtp password
$mail->from = "这里应该可以填写你想要填写的邮箱";
$mail->fromname = "这里是要显示的名称";
$mail->addaddress("$address", "");
//$mail->addaddress(""); // name is optional
//$mail->addreplyto("", "");
//$mail->wordwrap = 50; // set word wrap to 50 characters
//$mail->addattachment("/var/tmp/file.tar.gz"); // add p_w_uploads
//$mail->addattachment("/tmp/image.jpg", "new.jpg"); // optional name
//$mail->ishtml(true); // set email format to html
$mail->subject = "phpmailer测试邮件";
$mail->body = "hello,这是松子的测试邮件";
$mail->altbody = "this is the body in plain text for non-html mail clients";
if(!$mail->send())
{
echo "message could not be sent. <p>";
echo "mailer error: " . $mail->errorinfo;
exit;
}
echo "message has been sent";
|
/*
$altbody --属性
出自:phpmailer ::$altbody
文件:class.phpmailer .php
说明:该属性的设置是在邮件正文不支持html的备用显示
addaddress --方法
出自:phpmailer ::addaddress(),文件:class.phpmailer .php
说明:增加收件人。参数1为收件人邮箱,参数2为收件人称呼。例addaddress("to@163.com","to name"),但参数2可选,addaddress(to@163.com )也是可以的。
函数原型:public function addaddress($address, $name = '') {}
addattachment --方法
出自:phpmailer ::addattachment()
文件:class.phpmailer .php。
说明:增加附件。
参数:路径,名称,编码,类型。其中,路径为必选,其他为可选
函数原型:
addattachment($path, $name = '', $encoding = 'base64', $type = 'application/octet-stream'){}
addbcc --方法
出自:phpmailer ::addbcc()
文件:class.phpmailer .php
说明:增加一个密送。抄送和密送的区别请看[smtp发件中的密送和抄送的区别 ] 。
参数1为地址,参数2为名称。注意此方法只支持在win32下使用smtp,不支持mail函数
函数原型:public function addbcc($address, $name = ''){}
addcc -- 方法
出自:phpmailer ::addcc()
文件:class.phpmailer .php
说明:增加一个抄送。抄送和密送的区别请看[smtp发件中的密送和抄送的区别 ] 。
参数1为地址,参数2为名称注意此方法只支持在win32下使用smtp,不支持mail函数
函数原型:public function addcc($address, $name = '') {}
addcustomheader --方法
出自:phpmailer ::addcustomheader()
文件:class.phpmailer .php
说明:增加一个自定义的e-mail头部。
参数为头部信息
函数原型:public function addcustomheader($custom_header){}
addembeddedimage -- 方法
出自:phpmailer ::addembeddedimage()
文件:class.phpmailer .php
说明:增加一个嵌入式图片
参数:路径,返回句柄[,名称,编码,类型]
函数原型:public function addembeddedimage($path, $cid, $name = '', $encoding = 'base64', $type = 'application/octet-stream') {}
提示:addembeddedimage(picture_path. "index_01.jpg ", "img_01 ", "index_01.jpg ");
在html中引用<img src= "cid:img_01 ">
addreplyto --方法
出自:phpmailer :: addreplyto()
文件:class.phpmailer .php
说明:增加回复标签,如"reply-to"
参数1地址,参数2名称
函数原型:public function addreplyto($address, $name = '') {}
addstringattachment -方法
出自:phpmailer :: addstringattachment()
文件:class.phpmailer .php
说明:增加一个字符串或二进制附件(adds a string or binary attachment (non-filesystem) to the list.?)
参数:字符串,文件名[,编码,类型]
函数原型:public function addstringattachment($string, $filename, $encoding = 'base64', $type = 'application/octet-stream') {}
authenticate --方法
出自:smtp::authenticate()
文件:class.smtp.php
说明:开始smtp认证,必须在hello()之后调用,如果认证成功,返回true,
参数1用户名,参数2密码
函数原型:public function authenticate($username, $password) {}
b开头
$body --属性
出自:phpmailer ::$body
文件: class.phpmailer .php
说明:邮件内容,html或text格式
c开头
$charset --属性
出自:phpmailer ::$charset
文件:class.phpmailer .php
说明:邮件编码,默认为iso-8859-1
$confirmreadingto --属性
出自:phpmailer ::$confirmreadingto 文件class.phpmailer .php
说明:回执?
$contenttype --属性
出自:phpmailer ::$contenttype
文件: class.phpmailer .php
说明:文档的类型,默认为"text/plain"
$crlf --属性
出自:phpmailer ::$contenttype
文件:class.phpmailer .php
说明:smtp回复结束的分隔符(smtp reply line ending?)
class.phpmailer .php --对象
出自:class.phpmailer .php
文件: class.phpmailer .php
说明:phpmailer 对象
class.smtp.php --对象
出自:class.smtp.php 文件: class.smtp.php
说明:smtp功能的对象
clearaddresses --方法
出自:phpmailer ::clearaddresses()
文件: class.phpmailer .php
说明:清除收件人,为下一次发件做准备。返回类型是void
clearallrecipients --方法
出自:phpmailer ::clearallrecipients()
文件: class.phpmailer .php
说明:清除所有收件人,包括cc(抄送)和bcc(密送)
clearattachments --方法
出自:phpmailer ::clearattachments()
文件: class.phpmailer .php
说明:清楚附件
clearbccs --方法
出自:phpmailer ::clearbccs() 文件 class.phpmailer .php
说明:清楚bcc (密送)
clearcustomheaders --方法
出自:phpmailer ::clearcustomheaders()
文件: class.phpmailer .php
说明:清楚自定义头部
clearreplytos --方法
出自:phpmailer ::clearreplytos()
文件: class.phpmailer .php
说明:清楚回复人
close --方法
出自:smtp::close()
文件: class.smtp.php
说明:关闭一个smtp连接
connect --方法
出自:smtp::connect()
文件: class.smtp.php
说明:建立一个smtp连接mailer.html
$contenttype --属性
出自:phpmailer ::$contenttype
文件: class.phpmailer .php
说明:文档的类型,默认为"text/plain"
d开头
$do_debug --属性
出自:smtp::$do_debug
文件:class.smtp.php
说明:smtp调试输出
data -方法
出自:smtp::data()
文件:class.smtp.php
说明:向服务器发送一个数据命令和消息信息(sendsthemsg_datatotheserver)
e开头
$encoding --属性
出自:phpmailer ::$encoding
文件:class.phpmailer .php
说明:设置邮件的编码方式,可选:"8bit","7bit","binary","base64",和"quoted-printable".
$errorinfo --属性
出自:phpmailer ::$errorinfo
文件:class.phpmailer .php
说明:返回邮件smtp中的最后一个错误信息
expand --方法
出自:smtp::expand()
文件:class.smtp.php
说明:返回邮件列表中所有用户。成功则返回数组,否则返回 false(expandtakesthenameandaskstheservertolistallthepeoplewhoaremembersofthe_list_.expandwillreturnbackandarrayoftheresultorfalseifanerroroccurs.)
f开头:
$from --属性
出自:phpmailer ::$from文件class.phpmailer .php
说明:发件人e-mail地址
$fromname --属性
出自:phpmailer ::$fromname
文件:class.phpmailer .php
说明:发件人称呼
h开头:
$helo --属性
出自:phpmailer ::$helo
文件:class.phpmailer .php
说明:设置smtphelo,默认是$hostname(setsthesmtpheloofthemessage(defaultis$hostname).)
$host --属性
出自:phpmailer ::$host
文件:class.phpmailer .php
说明:设置smtp服务器,格式为:主机名[端口号],如smtp1.example.com:25和smtp2.example.com都是合法的
$hostname --属性
出自:phpmailer ::$hostname
文件:class.phpmailer .php
说明:设置在message-id和andreceivedheaders中的hostname并同时被$helo使用。如果为空,默认为server_name或'localhost.localdomain"
hello --方法
出自:smtp::hello()
文件:class.smtp.php
说明:向smtp服务器发送helo命令
help --方法
出自:smtp::help()
文件:class.smtp.php
说明:如果有关键词,得到关键词的帮助信息
i开头:
iserror --方法
出自:phpmailer ::iserror()
文件:class.phpmailer .php
说明:返回是否有错误发生
ishtml --方法
出自:phpmailer ::ishtml()
文件:class.phpmailer .php
说明:设置信件是否是html格式
ismail --方法
出自:phpmailer ::ismail()
文件:class.phpmailer .php
说明:设置是否使用php的mail函数发件
isqmail --方法
出自:phpmailer ::isqmail()
文件:class.phpmailer .php
说明:设置是否使用qmailmta来发件
issendmail-- 方法
出自:phpmailer ::issendmail()
文件:class.phpmailer .php
说明:是否使用$sendmail程序来发件
issmtp--方法
出自:phpmailer ::issmtp()
文件:class.phpmailer .php
说明:是否使用smtp来发件
m开头:
$mailer --属性
出自:phpmailer ::$mailer
文件:class.phpmailer .php
说明:发件方式,("mail","sendmail",or"smtp").中的一个
mail --方法
出自:smtp::mail()
文件:class.smtp.php
说明:从$from中一个邮件地址开始处理,返回true或false。如果是true,则开始发件
n开头:
noop-- 方法
出自:smtp::noop()
文件:class.smtp.php
说明:向smtp服务器发送一个noop命令
p开头:
$password --属性
出自:phpmailer ::$password
文件:class.phpmailer .php
说明:设置smtp的密码
$plugindir --属性
出自:phpmailer ::$plugindir
文件:class.phpmailer .php
说明:设置phpmailer 的插件目录,仅在smtpclass不在phpmailer 目录下有效
$port --属性
出自:phpmailer ::$port
文件:class.phpmailer .php
说明:设置smtp的端口号
$priority --属性
出自:phpmailer ::$priority
文件:class.phpmailer .php
说明:设置邮件投递优先等级。1=紧急,3=普通,5=不急
phpmailer --对象
出自:phpmailer
文件:class.phpmailer .php
说明:phpmailer -phpemailtransportclass
q开头
quit --方法
出自:smtp::quit()
文件:class.smtp.php
说明:向服务器发送quit命令,如果没有错误发生。那么关闭sock,不然$close_on_error为true
r开头
recipient --方法
出自:smtp::recipient()
文件:class.smtp.php
说明:使用to向smtp发送rcpt命令,参数为:$to
reset --方法
出自:smtp::reset()
文件:class.smtp.php
说明:发送rset命令从而取消处理中传输。成功则返回true,否则为false
s开头:
$sender --属性
出自:phpmailer ::$sender
文件:class.phpmailer .php
说明:setsthesenderemail(return-path)ofthemessage.ifnotempty,willbesentvia-ftosendmailoras'mailfrom'insmtpmode.
$sendmail --属性
出自:phpmailer ::$sendmail
文件:class.phpmailer .php
说明:设置发件程序的目录
$smtpauth --属性
出自:phpmailer ::$smtpauth
文件:class.phpmailer .php
说明:设置smtp是否需要认证,使用username和password变量
$smtpdebug --属性
出自:phpmailer ::$smtpdebug
文件:class.phpmailer .php
说明:设置smtp是否调试输出?
$smtpkeepalive --属性
出自:phpmailer ::$smtpkeepalive
文件:class.phpmailer .php
说明:在每次发件后不关闭连接。如果为true,则,必须使用smtpclose()来关闭连接
$smtp_port --属性
出自:smtp::$smtp_port
文件:class.smtp.php
说明:设置smtp端口
$subject --属性
出自:phpmailer ::$subject
文件:class.phpmailer .php
说明:设置信件的主题
send --方法
出自:smtp::send()
文件:class.smtp.php
说明:从指定的邮件地址开始一个邮件传输
send --方法
出自:phpmailer ::send()
文件:class.phpmailer .php
说明:创建邮件并制定发件程序。如果发件不成功,则返回false,请使用errorinfo来查看错误信息
sendandmail --方法
出自:smtp::sendandmail()
文件:class.smtp.php
说明:从指定的邮件地址开始一个邮件传输
sendormail --方法
出自:smtp::sendormail()
文件:class.smtp.php
说明:从指定的邮件地址开始一个邮件传输
setlanguage --方法
出自:phpmailer ::setlanguage()
文件:class.phpmailer .php
说明:设置phpmailer 错误信息的语言类型,如果无法加载语言文件,则返回false,默认为english
smtp --方法
出自:smtp::smtp()
文件:class.smtp.php
说明:初始化一个对象以便数据处于一个已知的状态
smtp --对象
出自:smtp
文件:class.smtp.php
说明:smtp对象
smtpclose --方法
出自:phpmailer ::smtpclose()
文件:class.phpmailer .php
说明:如果有活动的smtp则关闭它。
t开头
$timeout --属性
出自:phpmailer ::$timeout
文件:class.phpmailer .php
说明:设置smtp服务器的超时(单位:秒)。注意:在win32下,该属性无效
turn --方法
出自:smtp::turn()
文件:class.smtp.php
说明:这是一个可选的smtp参数,目前phpmailer 并不支持他,可能未来支持
u开头
$username --属性
出自:phpmailer ::$username
文件:class.phpmailer .php
说明:设置smtp用户名
v开头
$version --属性
出自:phpmailer ::$version
文件:class.phpmailer .php
说明:返回phpmailer 的版本
verify --方法
出自:smtp::verify()
文件:class.smtp.php
说明:通过服务器检查用户名是否经过验证
w开头:
$wordwrap --属性
出自:phpmailer ::$wordwrap
文件:class.phpmailer .php
说明:设置每行最大字符数,超过改数后自动换行
*/
?>
代码如下 |
复制代码 |
function send_msg($to,$subject,$body) {
$send_addr = 'admin@test.com'; //发送人地址
$header = "from: admin <".$send_addr."> "; //设置email头
ini_set('sendmail_from',$send_addr);
mail($to,$subject,$body,$header);
}
|
pop3邮箱登录
代码如下 |
复制代码 |
function pop3_login($host,$username,$password)
{
global $debug;
if(empty($host)) {
return false;
}
if($debug)
echo "open hostname: ".$host.",port: ".$port." ";
$conn = @fsockopen($host,110,$err_no,$err_str,5);
if(!$conn) {
return false;
}
$ret_info = fgets($conn,1024);
if(substr($ret_info,0,3) == "+ok") {
if(login($conn,$username,$password)) {
return true;
}
}
return false;
}
|
smtp登录验证函数
代码如下 |
复制代码 |
function smtp_login($host,$username,$password)
{
global $debug;
if(empty($host)) {
return false;
}
if($debug)
echo "open hostname: ".$host.",port: ".$port." ";
$conn = @fsockopen($host,25,$err_no,$err_str,5);
if(!$conn) {
return false;
}
$ret_info = fgets($conn,1024);
if(substr($ret_info,0,3) == "220") {
fputs($conn,"helo localhost ");
if(substr(fgets($conn,1024),0,3) == "250") {
if(login($conn,$username,$password,25)) {
return true;
}
}
}
return false;
}
|
imap登录验证函数
代码如下 |
复制代码 |
function imap_login($host,$username,$password)
{
global $debug;
if(empty($host)) {
return false;
}
if($debug)
echo "open hostname: ".$host.",port: ".$port." ";
$conn = @fsockopen($host,143,$err_no,$err_str,5);
if(!$conn) {
return false;
}
$ret_info = fgets($conn,1024);
if(strpos($ret_info,"ok")) {
fputs($conn,"a001 login $username $password ");
$ret = fgets($conn,1024);
if(strpos($ret,"login ok")) {
return true;
}
}
return false;
}
|
这是一款利用了php来发送邮件的代码,他不但可以发送普通邮件同时还支持发送的邮件带附件哦,很好使用,只要用户更正一下SMTP服务器 SMTP服务器端口 SMTP服务器的用户邮箱 SMTP服务器的用户帐号 SMTP服务器的用户密码,即邮箱密码 邮件格式(HTML/TXT),TXT为文本邮件,就可以发送邮件了哦。
*/
require_once ('common/email.class.php');
代码如下 |
复制代码 |
//这里以smiley_face@126.com邮箱为例,使用者需要自己修改
$smtps教程erver = "smtp.126.com";//smtp服务器
$smtpserverport =25;//smtp服务器端口
$smtpusermail = "smiley_face@126.com";//smtp服务器的用户邮箱
$smtpuser = "smiley_face";//smtp服务器的用户帐号
$smtppass = "smileyface1224";//smtp服务器的用户密码,即邮箱密码
$mailtype = "html";//邮件格式(html/txt),txt为文本邮件
$smtp = new smtp($smtpserver,$smtpserverport,true,$smtpuser,$smtppass);//true表示使用身份验证,否则不使用身份验证.
$smtp->debug = false;//是否显示发送的调试信息
//common/email.class.php 代码如下
header("content-type: text/html; charset=utf-8");
class smtp
{
/* public variables */
var $smtp_port;
var $time_out;
var $host_name;
var $log_file;
var $relay_host;
var $debug;
var $auth;
var $user;
var $pass;
/* private variables */
var $sock;
/* constractor */
function smtp($relay_host = "", $smtp_port = 25,$auth = false,$user,$pass)
{
$this->debug = false;
$this->smtp_port = $smtp_port;
$this->relay_host = $relay_host;
$this->time_out = 30; //is used in fsockopen()
#
$this->auth = $auth;//auth
$this->user = $user;
$this->pass = $pass;
#
$this->host_name = "localhost"; //is used in helo command, localhost
$this->log_file ="";
$this->sock = false;
}
/* main function */
function sendmail($to, $from, $subject = "", $body = "", $mailtype, $cc = "", $bcc = "", $additional_headers = "")
{
$mail_from = $this->get_address($this->strip_comment($from));
$body = preg_replace("/(^|( ))(\.)/", "\1.\3", $body);
$header .= "mime-version:1.0 ";
if($mailtype=="html"){
$header .= "content-type:text/html ";
}
$header .= "to: ".$to." ";
if ($cc != "") {
$header .= "cc: ".$cc." ";
}
$header .= "from: $from<".$from."> ";
$header .= "subject: ".$subject." ";
$header .= $additional_headers;
$header .= "date: ".date("r")." ";
$header .= "x-mailer:by redhat (php/".phpversion().") ";
list($msec, $sec) = explode(" ", microtime());
$header .= "message-id: <".date("ymdhis", $sec).".".($msec*1000000).".".$mail_from."> ";
$to = explode(",", $this->strip_comment($to));
if ($cc != "") {
$to = array_merge($to, explode(",", $this->strip_comment($cc)));
}
if ($bcc != "") {
$to = array_merge($to, explode(",", $this->strip_comment($bcc)));
}
$sent = true;
foreach ($to as $rcpt_to) {
$rcpt_to = $this->get_address($rcpt_to);
if (!$this->smtp_sockopen($rcpt_to)) {
$this->log_write("error: cannot send email to ".$rcpt_to." ");
$sent = false;
continue;
}
if ($this->smtp_send($this->host_name, $mail_from, $rcpt_to, $header, $body)) {
$this->log_write("e-mail has been sent to <".$rcpt_to."> ");
} else {
$this->log_write("error: cannot send email to <".$rcpt_to."> ");
$sent = false;
}
fclose($this->sock);
$this->log_write("disconnected from remote host ");
}
echo "<br>";
echo $header;
return $sent;
}
/* private functions */
function smtp_send($helo, $from, $to, $header, $body = "")
{
if (!$this->smtp_putcmd("helo", $helo)) {
return $this->smtp_error("sending helo command");
}
#auth
if($this->auth){
if (!$this->smtp_putcmd("auth login", base64_encode($this->user))) {
return $this->smtp_error("sending helo command");
}
if (!$this->smtp_putcmd("", base64_encode($this->pass))) {
return $this->smtp_error("sending helo command");
}
}
#
if (!$this->smtp_putcmd("mail", "from:<".$from.">")) {
return $this->smtp_error("sending mail from command");
}
if (!$this->smtp_putcmd("rcpt", "to:<".$to.">")) {
return $this->smtp_error("sending rcpt to command");
}
if (!$this->smtp_putcmd("data")) {
return $this->smtp_error("sending data command");
}
if (!$this->smtp_message($header, $body)) {
return $this->smtp_error("sending message");
}
if (!$this->smtp_eom()) {
return $this->smtp_error("sending <cr><lf>.<cr><lf> [eom]");
}
if (!$this->smtp_putcmd("quit")) {
return $this->smtp_error("sending quit command");
}
return true;
}
function smtp_sockopen($address)
{
if ($this->relay_host == "") {
return $this->smtp_sockopen_mx($address);
} else {
return $this->smtp_sockopen_relay();
}
}
function smtp_sockopen_relay()
{
$this->log_write("trying to ".$this->relay_host.":".$this->smtp_port." ");
$this->sock = @fsockopen($this->relay_host, $this->smtp_port, $errno, $errstr, $this->time_out);
if (!($this->sock && $this->smtp_ok())) {
$this->log_write("error: cannot connenct to relay host ".$this->relay_host." ");
$this->log_write("error: ".$errstr." (".$errno.") ");
return false;
}
$this->log_write("connected to relay host ".$this->relay_host." ");
return true;;
}
function smtp_sockopen_mx($address)
{
$domain = ereg_replace("^.+@([^@]+)$", "\1", $address);
if (!@getmxrr($domain, $mxhosts)) {
$this->log_write("error: cannot resolve mx "".$domain."" ");
return false;
}
foreach ($mxhosts as $host) {
$this->log_write("trying to ".$host.":".$this->smtp_port." ");
$this->sock = @fsockopen($host, $this->smtp_port, $errno, $errstr, $this->time_out);
if (!($this->sock && $this->smtp_ok())) {
$this->log_write("warning: cannot connect to mx host ".$host." ");
$this->log_write("error: ".$errstr." (".$errno.") ");
continue;
}
$this->log_write("connected to mx host ".$host." ");
return true;
}
$this->log_write("error: cannot connect to any mx hosts (".implode(", ", $mxhosts).") ");
return false;
}
function smtp_message($header, $body)
{
fputs($this->sock, $header." ".$body);
$this->smtp_debug("> ".str_replace(" ", " "."> ", $header." > ".$body." > "));
return true;
}
function smtp_eom()
{
fputs($this->sock, " . ");
$this->smtp_debug(". [eom] ");
return $this->smtp_ok();
}
function smtp_ok()
{
$response = str_replace(" ", "", fgets($this->sock, 512));
$this->smtp_debug($response." ");
if (!preg_match("/^[23]/", $response)) {
fputs($this->sock, "quit ");
fgets($this->sock, 512);
$this->log_write("error: remote host returned "".$response."" ");
return false;
}
return true;
}
|
代码如下 |
复制代码 |
function smtp_mail ($sendto_email,$subject,$body) {
global $mailconfig,$_cfg;
$mail = new phpmailer();
$mail->issmtp();
$mail->host = $mailconfig['smtps教程ervers'];//smtp servers
$mail->smtpauth = true;// 启用smtp验证功能
$mail->username = $mailconfig['smtpusername'];
$mail->password = $mailconfig['smtppassword'];
$mail->from =$mailconfig['smtpfrom'];
$mail->fromname =$_cfg['site_name'];
$mail->charset = "gb2312";
$mail->encoding = "base64";
$mail->addaddress($sendto_email,"");//收件人地址,可以替换成任何想要接收邮件的email信箱,格式是addaddress("收件人email","收件人姓名")
$mail->addreplyto($mailconfig['addreplyto'],"");//增加回复标签addreplyto
$mail->ishtml(true);
$mail->subject = $subject;
$mail->body =$body;
$mail->altbody ="text/html";//该属性的设置是在邮件正文不支持html的备用显示
if(!$mail->send())
{
return false;
//echo "邮件发送有误 <p>";
//echo "邮件错误信息: " . $mail->errorinfo;
}
else {
return true;
}
}
方法二
<?php
class email {
//---设置全局变量
var $mailto = ""; // 收件人
var $mailcc = ""; // 抄送
var $mailbcc = ""; // 秘密抄送
var $mailfrom = ""; // 发件人
var $mailsubject = ""; // 主题
var $mailtext = ""; // 文本格式的信件主体
var $mailhtml = ""; // html格式的信件主体
var $mailattachments = ""; // 附件
/* 函数setto($inaddress) :用于处理邮件的地址 参数 $inaddress
为包涵一个或多个字串,email地址变量,使用逗号来分割多个邮件地址
默认返回值为true
**********************************************************/
function setto($inaddress){
//--用explode()函数根据","对邮件地址进行分割
$addressarray = explode( ",",$inaddress);
//--通过循环对邮件地址的合法性进行检查
for($i=0;$icheckemail($addressarray[$i])==false) return false; }
//--所有合法的email地址存入数组中
$this->mailto = implode($addressarray, ",");
return true; }
/**************************************************
函数 setcc($inaddress) 设置抄送人邮件地址
参数 $inaddress 为包涵一个或多个邮件地址的字串,email地址变量,
使用逗号来分割多个邮件地址 默认返回值为true
**************************************************************/
function setcc($inaddress){
//--用explode()函数根据","对邮件地址进行分割
$addressarray = explode( ",",$inaddress);
//--通过循环对邮件地址的合法性进行检查
for($i=0;$icheckemail($addressarray[$i])==false) return false; }
//--所有合法的email地址存入数组中
$this->mailcc = implode($addressarray, ",");
return true; }
/***************************************************
函数setbcc($inaddress) 设置秘密抄送地址 参数 $inaddress 为包涵一个或多
个邮件地址的字串,email地址变量,使用逗号来分割多个邮件地址 默认返回值为
true
******************************************/
function setbcc($inaddress){
//--用explode()函数根据","对邮件地址进行分割
$addressarray = explode( ",",$inaddress);
//--通过循环对邮件地址的合法性进行检查
for($i=0;$i<count($addressarray);$i++)
{ if($this->checkemail($addressarray[$i])==false)
return false;
}
//--所有合法的email地址存入数组中
$this->mailbcc = implode($addressarray, ",");
return true;
}
/*****************************************************************
函数setfrom($inaddress):设置发件人地址 参数 $inaddress 为包涵邮件
地址的字串默认返回值为true
***************************************/
function setfrom($inaddress){
if($this->checkemail($inaddress)){
$this->mailfrom = $inaddress;
return true;
} return false; }
/**********************
函数 setsubject($insubject) 用于设置邮件主题参数$insubject为字串,
默认返回的是true
*******************************************/
function setsubject($insubject){
if(strlen(trim($insubject)) > 0){
$this->mailsubject = ereg_replace( "n", "",$insubject);
return true; }
return false; }
/****************************************************
函数settext($intext) 设置文本格式的邮件主体参数 $intext 为文本内容默
认返回值为true
****************************************/
function settext($intext){
if(strlen(trim($intext)) > 0){
$this->mailtext = $intext;
return true; }
return false;
}
/**********************************
函数sethtml($inhtml) 设置html格式的邮件主体参数$inhtml为html格式,
默认返回值为true
************************************/
function sethtml($inhtml){
if(strlen(trim($inhtml)) > 0){
$this->mailhtml = $inhtml;
return true; }
return false; }
/**********************
函数 setattachments($inattachments) 设置邮件的附件 参数$inattachments
为一个包涵目录的字串,也可以包涵多个文件用逗号进行分割 默认返回值为true
*******************************************/
function setattachments($inattachments){
if(strlen(trim($inattachments)) > 0){
$this->mailattachments = $inattachments;
return true; }
return false; }
/*********************************
函数 checkemail($inaddress) :这个函数我们前面已经调用过了,主要就是
用于检查email地址的合法性
*****************************************/
function checkemail($inaddress){
return (ereg( "^[^@ ]+@([a-za-z0-9-]+.)+([a-za-z0-9-]{2}|net|com|gov|mil|org|edu|int)$",$inaddress));
}
/*************************************************
函数loadtemplate($infilelocation,$inhash,$informat) 读取临时文件并且
替换无用的信息参数$infilelocation用于定位文件的目录
$inhash 由于存储临时的值 $informat 由于放置邮件主体
***********************************************************/
function loadtemplate($infilelocation,$inhash,$informat){
/* 比如邮件内有如下内容: dear ~!username~,
your address is ~!useraddress~ */
//--其中"~!"为起始标志"~"为结束标志
$templatedelim = "~";
$templatenamestart = "!";
//--找出这些地方并把他们替换掉
$templatelineout = ""; //--打开临时文件
if($templatefile = fopen($infilelocation, "r")){
while(!feof($templatefile)){
$templateline = fgets($templatefile,1000);
$templatelinearray = explode($templatedelim,$templateline);
for( $i=0; $i<count($templatelinearray);$i++){
//--寻找起始位置
if(strcspn($templatelinearray[$i],$templatenamestart)==0){
//--替换相应的值
$hashname = substr($templatelinearray[$i],1);
//--替换相应的值
$templatelinearray[$i] = ereg_replace($hashname,(string)$inhash[$hashname],$hashname);
}
}
//--输出字符数组并叠加
$templatelineout .= implode($templatelinearray, "");
} //--关闭文件fclose($templatefile);
//--设置主体格式(文本或html)
if( strtoupper($informat)== "text" )
return($this->settext($templatelineout));
else if( strtoupper($informat)== "html" )
return($this->sethtml($templatelineout));
} return false;
}
/*****************************************
函数 getrandomboundary($offset) 返回一个随机的边界值
参数 $offset 为整数 – 用于多管道的调用 返回一个md5()编码的字串
****************************************/
function getrandomboundary($offset = 0){
//--随机数生成
srand(time()+$offset);
//--返回 md5 编码的32位 字符长度的字串
return ( "----".(md5(rand()))); }
/********************************************
函数: getcontenttype($infilename)用于判断附件的类型
**********************************************/
function getcontenttype($infilename){
//--去除路径
$infilename = basename($infilename);
//--去除没有扩展名的文件
if(strrchr($infilename, ".") == false){
return "application/octet-stream";
}
//--提区扩展名并进行判断
$extension = strrchr($infilename, ".");
switch($extension){
case ".gif": return "image/gif";
case ".gz": return "application/x-gzip";
case ".htm": return "text/html";
case ".html": return "text/html";
case ".jpg": return "image/jpeg";
case ".tar": return "application/x-tar";
case ".txt": return "text/plain";
case ".zip": return "application/zip";
default: return "application/octet-stream";
}
return "application/octet-stream";
}
/**********************************************
函数formattextheader把文本内容加上text的文件头
*****************************************************/
function formattextheader(){ $outtextheader = "";
$outtextheader .= "content-type: text/plain;
charset=us-asciin";
$outtextheader .= "content-transfer-encoding: 7bitnn";
$outtextheader .= $this->mailtext. "n";
return $outtextheader;
} /************************************************
函数formathtmlheader()把邮件主体内容加上html的文件头
******************************************/
function formathtmlheader(){
$outhtmlheader = "";
$outhtmlheader .= "content-type: text/html;
charset=us-asciin";
$outhtmlheader .= "content-transfer-encoding: 7bitnn";
$outhtmlheader .= $this->mailhtml. "n";
return $outhtmlheader;
}
/**********************************
函数 formatattachmentheader($infilelocation) 把邮件中的附件标识出来
********************************/
function formatattachmentheader($infilelocation){
$outattachmentheader = "";
//--用上面的函数getcontenttype($infilelocation)得出附件类型
$contenttype = $this->getcontenttype($infilelocation);
//--如果附件是文本型则用标准的7位编码
if(ereg( "text",$contenttype)){
$outattachmentheader .= "content-type: ".$contenttype. ";n";
$outattachmentheader .= ' name="'.basename($infilelocation). '"'. "n";
$outattachmentheader .= "content-transfer-encoding: 7bitn";
$outattachmentheader .= "content-disposition: attachment;n";
$outattachmentheader .= ' filename="'.basename($infilelocation). '"'. "nn";
$textfile = fopen($infilelocation, "r");
while(!feof($textfile)){
$outattachmentheader .= fgets($textfile,1000);
}
//--关闭文件 fclose($textfile);
$outattachmentheader .= "n";
}
//--非文本格式则用64位进行编码
else{ $outattachmentheader .= "content-type: ".$contenttype. ";n";
$outattachmentheader .= ' name="'.basename($infilelocation). '"'. "n";
$outattachmentheader .= "content-transfer-encoding: base64n";
$outattachmentheader .= "content-disposition: attachment;n";
$outattachmentheader .= ' filename="'.basename($infilelocation). '"'. "nn";
//--调用外部命令uuencode进行编码
exec( "uuencode -m $infilelocation nothing_out",$returnarray);
for ($i = 1; $i<(count($returnarray)); $i++){
$outattachmentheader .= $returnarray[$i]. "n";
}
} return $outattachmentheader;
}
/******************************
函数 send()用于发送邮件,发送成功返回值为true
************************************/
function send(){
//--设置邮件头为空
$mailheader = "";
//--添加抄送人
if($this->mailcc != "")
$mailheader .= "cc: ".$this->mailcc. "n";
//--添加秘密抄送人
if($this->mailbcc != "")
$mailheader .= "bcc: ".$this->mailbcc. "n";
//--添加发件人
if($this->mailfrom != "")
$mailheader .= "from: ".$this->mailfrom. "n";
//---------------------------邮件格式------------------------------
//--文本格式
if($this->mailtext != "" && $this->mailhtml == "" && $this->mailattachments == ""){
return mail($this->mailto,$this->mailsubject,$this->mailtext,$mailheader);
}
//--html或text格式
else if($this->mailtext != "" && $this->mailhtml != "" && $this->mailattachments == ""){
$bodyboundary = $this->getrandomboundary();
$textheader = $this->formattextheader();
$htmlheader = $this->formathtmlheader();
//--设置 mime-版本
$mailheader .= "mime-version: 1.0n";
$mailheader .= "content-type: multipart/alternative;n";
$mailheader .= ' boundary="'.$bodyboundary. '"';
$mailheader .= "nnn";
//--添加邮件主体和边界
$mailheader .= "--".$bodyboundary. "n";
$mailheader .= $textheader;
$mailheader .= "--".$bodyboundary. "n";
//--添加html标签
$mailheader .= $htmlheader;
$mailheader .= "n--".$bodyboundary. "--";
//--发送邮件
return mail($this->mailto,$this->mailsubject, "",$mailheader);
}
//--文本加html加附件
else if($this->mailtext != "" && $this->mailhtml != "" && $this->mailattachments != ""){
$attachmentboundary = $this->getrandomboundary();
$mailheader .= "content-type: multipart/mixed;n";
$mailheader .= ' boundary="'.$attachmentboundary. '"'. "nn";
$mailheader .= "this is a multi-part message in mime format.n";
$mailheader .= "--".$attachmentboundary. "n";
$bodyboundary = $this->getrandomboundary(1);
$textheader = $this->formattextheader();
$htmlheader = $this->formathtmlheader();
$mailheader .= "mime-version: 1.0n";
$mailheader .= "content-type: multipart/alternative;n";
$mailheader .= ' boundary="'.$bodyboundary. '"';
$mailheader .= "nnn";
$mailheader .= "--".$bodyboundary. "n";
$mailheader .= $textheader;
$mailheader .= "--".$bodyboundary. "n";
$mailheader .= $htmlheader;
$mailheader .= "n--".$bodyboundary. "--";
//--获取附件值
$attachmentarray = explode( ",",$this->mailattachments);
//--根据附件的个数进行循环
for($i=0;$i<count($attachmentarray);$i++){
//--分割 $mailheader .= "n--".$attachmentboundary. "n";
//--附件信息
$mailheader .= $this->formatattachmentheader($attachmentarray[$i]);
}
$mailheader .= "--".$attachmentboundary. "--";
return mail($this->mailto,$this->mailsubject, "",$mailheader);
}
return false;
}
}
?>
|
使用方法:
<?
代码如下 |
复制代码 |
include "email.class"
$mail->setto("a@a.com"); //收件人
$mail-> setcc("b@b.com,[url=mailto:c@c.com]c@c.com[/url]"); //抄送
$mail-> setcc("d@b.com,[url=mailto:e@c.com]e@c.com[/url]"); //秘密抄送
$mail->setfrom("[url=mailto:f@f.com]f@f.com[/url]");//发件人
$mail->setsubject("主题") ; //主题
$mail->settext("文本格式") ;//发送文本格式也可以是变量
$mail->sethtml("html格式") ;//发送html格式也可以是变量
$mail->setattachments("c:a.jpg") ;//添加附件,需表明路径
$mail->send(); //发送邮件
|
?>
文章提供一款简单的php自带的邮件发送函数来发送电子邮件,功能非常简单,只要简单的带参数就好,但是必须配置好服务器的mail功能才可以。
?>
代码如下 |
复制代码 |
<html>
<head>
<title>发信给网管</title>
</head>
<body>
<h2 align="center">网管收信</h2><br>
<hr><br>
<center>
<form action="mail.php">
发件人:<input type="text" name="from" size=25><br>
主题:<input type="text" name="subject" size=20><br>
内容:
<textarea name="content" cols=80 rows=15>你好,站长:</textarea><br>
<input type="submit" value="寄出"> <input type="reset" value="重写"><br>
</form>
</body></html>
mail.php文件代码
<?php
if (empty($from) or empty($subject) or empty($content)) {
echo "没有完成填写,请<a href='mail.html'>返回</a>";
}
$body="[主题] $subjectn";
$body.="[发件人] $fromn";
$body.=$content;
$deal=mail("guoanyuan@eyou.com",$subject,$body,"from:$from");
if ($deal) {echo "寄件成功!";}else{echo "寄件失败!!!";}
?>
|
关于php mail函数
mail 简介
http 函数允许您从脚本中直接发送电子邮件。
需求
要使邮件函数可用,php 需要已安装且正在运行的邮件系统。要使用的程序是由 php.ini 文件中的配置设置定义的。
安装
邮件函数是 php 核心的组成部分。无需安装即可使用这些函数。
运行时配置
邮件函数的行为受 php.ini 的影响。
mail 配置选项
名称 默认 描述 可更改
smtp "localhost" windows 专用:smtp 服务器的 dns 名称或 ip 地址。 php_ini_all
smtp_port "25" windows 专用:smtp 段口号。自 php 4.3 起可用。 php_ini_all
sendmail_from null windows 专用:规定从 php 发送的邮件中使用的 "from" 地址。 php_ini_all
sendmail_path null unix 系统专用:规定sendmail 程序的路径(通常 /usr/sbin/sendmail 或 /usr/lib/sendmail)