PHP sendmail

发布时间:2010年06月20日      浏览次数:318 次
最近很多人问我如何发邮件,为什么mail函数无法发出去。我当时感慨了一下下。。。
mail函数是linux下面默认的发邮件函数,而且需要安装mail服务,同时要在PHP.ini里面配置好才能发送。 也有人嫌phpmailer太大,正好手头有一个discuz发邮件的类改的函数。贴上来。。。抛抛砖引引玉,不要砸人就行。
毛主席说:要温柔。。。
PHP代码
<?php

$mail = array(
'server' => 'smtp.163.com',
'port' => 25,
'auth' => 1,
'from' => 膘叔'<xxxxxx%163.com>' , //一定要是这种格式,否则可能发不出去
'auth_username' => '膘叔',//发信人帐号,登录邮箱用的,一般不用加域,如果发不出去就尝试加上域看看
'auth_password' => '发信人密码', //发信人密码
'maildelimiter' => 0, //断行符是用还是\r还是\n,1为,2为\r其它为\n
'mailusername' => 1, //是否显示发信人名字
'sendmail_silent' => 1,
'charset'=>'utf-8', //这就要看你的设定了,utf-8还是gbk(如果这里改成了gbk,那么,本文件一定要另存为ansi格式,否则就是乱码)
'adminname' => '小苟', //显示在邮箱的发件人
'adminemail'=>'admin%admin.com',//如果没有$email_from邮箱,就用这个默认的
);
$actName = '操作名称' ; //网站名称,会自动放在主题里,或者你也可以试着修改这个来让别人知道你的标题,比如“系统回复”,“签收”,等

sendmail('小苟 <ttttt%vip.qq.com >,gouki <10000%qq.com>' , 'hahahaha' , '内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容');
echo( 'over' );

function sendmail ($email_to, $email_subject, $email_message, $email_from = ''){
global $mail , $actName;
function errorlog ($type='',$info=''){
$data = '['.$type.']'.$info . "\n";
file_put_contents( 'mail_log.txt', $data , FILE_APPEND );
}
$maildelimiter = $mail['maildelimiter'] == 1 ? "" : ($mail['maildelimiter'] == 2 ? "\r" : "\n");
$mailusername = isset($mail['mailusername']) ? $mail['mailusername'] : 1;

$email_subject = '=?'.$mail['charset'].'?B?'.base64_encode(str_replace("\r", '', str_replace("\n", '', '['.$actName.'] '.$email_subject))).'?=';
$email_message = chunk_split(base64_encode(str_replace(".", " ..", str_replace("\n", "", str_replace("\r", "\n", str_replace("", "\n", str_replace("\n\r", "\r", $email_message)))))));

$email_from = $email_from == '' ? '=?'.$mail['charset'].'?B?'.base64_encode($mail['adminname'])."?= <$mail[adminemail]>" : (preg_match('/^(.+?) \<(.+?)\>$/',$email_from, $from) ? '=?'.$mail['charset'].'?B?'.base64_encode($from[1])."?= <$from[2]>" : $email_from);

foreach(explode(',', $email_to) as $touser) {
$tousers[] = preg_match('/^(.+?) \<(.+?)\>$/',$touser, $to) ? ($mailusername ? '=?'.$mail['charset'].'?B?'.base64_encode($to[1])."?= <$to[2]>" : $to[2]) : $touser;
}
$email_to = implode(',', $tousers);

$headers = "From: $email_from{$maildelimiter}X-Priority: 3{$maildelimiter}X-Mailer: Gouki! {$maildelimiter}MIME-Version: 1.0{$maildelimiter}Content-type: text/plain; charset={$mail[charset]}{$maildelimiter}Content-Transfer-Encoding: base64{$maildelimiter}";

$mail['port'] = $mail['port'] ? $mail['port'] : 25;

if(!$fp = fsockopen($mail['server'], $mail['port'], $errno, $errstr, 30)) {
errorlog('SMTP', "($mail[server]:$mail[port]) CONNECT - Unable to connect to the SMTP server", 0);
}
stream_set_blocking($fp, true);

$lastmessage = fgets($fp, 512);
if(substr($lastmessage, 0, 3) != '220') {
errorlog('SMTP', "$mail[server]:$mail[port] CONNECT - $lastmessage", 0);
}

fputs($fp, ($mail['auth'] ? 'EHLO' : 'HELO')." gouki ");
$lastmessage = fgets($fp, 512);
if(substr($lastmessage, 0, 3) != 220 && substr($lastmessage, 0, 3) != 250) {
errorlog('SMTP', "($mail[server]:$mail[port]) HELO/EHLO - $lastmessage", 0);
}

while(1) {
if(substr($lastmessage, 3, 1) != '-' || emptyempty($lastmessage)) {
break;
}
$lastmessage = fgets($fp, 512);
}

if($mail['auth']) {
fputs($fp, "AUTH LOGIN");
$lastmessage = fgets($fp, 512);
if(substr($lastmessage, 0, 3) != 334) {
errorlog('SMTP', "($mail[server]:$mail[port]) AUTH LOGIN - $lastmessage", 0);
}

fputs($fp, base64_encode($mail['auth_username'])."");
$lastmessage = fgets($fp, 512);
if(substr($lastmessage, 0, 3) != 334) {
errorlog('SMTP', "($mail[server]:$mail[port]) USERNAME - $lastmessage", 0);
}

fputs($fp, base64_encode($mail['auth_password'])."");
$lastmessage = fgets($fp, 512);
if(substr($lastmessage, 0, 3) != 235) {
errorlog('SMTP', "($mail[server]:$mail[port]) PASSWORD - $lastmessage", 0);
}

$email_from = $mail['from'];
}

fputs($fp, "MAIL FROM: <".preg_replace("/.*\<(.+?)\>.*/", "\1", $email_from).">");
$lastmessage = fgets($fp, 512);
if(substr($lastmessage, 0, 3) != 250) {
fputs($fp, "MAIL FROM: <".preg_replace("/.*\<(.+?)\>.*/", "\1", $email_from).">");
$lastmessage = fgets($fp, 512);
if(substr($lastmessage, 0, 3) != 250) {
errorlog('SMTP', "($mail[server]:$mail[port]) MAIL FROM - $lastmessage", 0);
}
}

$email_tos = array();
foreach(explode(',', $email_to) as $touser) {
$touser = trim($touser);
if($touser) {
fputs($fp, "RCPT TO: <".preg_replace("/.*\<(.+?)\>.*/", "\1", $touser).">");
$lastmessage = fgets($fp, 512);
if(substr($lastmessage, 0, 3) != 250) {
fputs($fp, "RCPT TO: <".preg_replace("/.*\<(.+?)\>.*/", "\1", $touser).">");
$lastmessage = fgets($fp, 512);
errorlog('SMTP', "($mail[server]:$mail[port]) RCPT TO - $lastmessage", 0);
}
}
}

fputs($fp, "DATA");
$lastmessage = fgets($fp, 512);
if(substr($lastmessage, 0, 3) != 354) {
errorlog('SMTP', "($mail[server]:$mail[port]) DATA - $lastmessage", 0);
}

$headers .= 'Message-ID: <'.gmdate('YmdHs').'.'.substr(md5($email_message.microtime()), 0, 6).rand(100000, 999999).'@'.$_SERVER['HTTP_HOST'].">{$maildelimiter}";

fputs($fp, "Date: ".gmdate('r')."");
fputs($fp, "To: ".$email_to."");
fputs($fp, "Subject: ".$email_subject."");
fputs($fp, $headers."");
fputs($fp, "");
fputs($fp, "$email_message.");
fputs($fp, "QUIT");
}
?>
已经写的够详细了,请不要提问,我也不会解答的,谢谢
免责声明:本站相关技术文章信息部分来自网络,目的主要是传播更多信息,如果您认为本站的某些信息侵犯了您的版权,请与我们联系,我们会即时妥善的处理,谢谢合作!