php 发送邮件函数

<?php
/**
 * 记录数据
 */
function runlog($mode = ‘SMTP‘,$b = ‘‘,$c = ‘‘,$d=‘‘) {

}

/**
 * 发送邮件
 * @param $toemail 收件人email
 * @param $subject 邮件主题
 * @param $message 正文
 */

function sendmail($toemail, $subject, $message) {

	$config = array (
		‘mailsend‘ => 2,
		‘maildelimiter‘ => 1,
		‘mailusername‘ => 1,
		‘server‘ => ‘smtp.qq.com‘,
		‘port‘ => 25,
		‘mail_type‘ => 1,
		‘auth‘ => 1,
		‘from‘ => ‘[email protected]‘,
		‘auth_username‘ => ‘[email protected]‘,
		‘auth_password‘ => ‘password‘,
	);	

	$charset   = ‘utf-8‘;
	$mail      = $config;
	$from      = $config[‘from‘];
	$mail_type = $config[‘mail_type‘];

	//mail 发送模式
	if($mail_type==0) {
		$headers  = ‘MIME-Version: 1.0‘ . "\r\n";
		$headers .= ‘Content-type: text/html; charset=‘.$charset.‘‘ . "\r\n";
		$headers .= ‘From: <‘.$from.‘>‘ . "\r\n";
		mail($toemail, $subject, $message, $headers);
		return true;
	}
	//邮件头的分隔符
	$maildelimiter = $mail[‘maildelimiter‘] == 1 ? "\r\n" : ($mail[‘maildelimiter‘] == 2 ? "\r" : "\n");
	//收件人地址中包含用户名
	$mailusername = isset($mail[‘mailusername‘]) ? $mail[‘mailusername‘] : 1;
	//端口
	$mail[‘port‘] = $mail[‘port‘] ? $mail[‘port‘] : 25;
	$mail[‘mailsend‘] = $mail[‘mailsend‘] ? $mail[‘mailsend‘] : 1;

	//发信者
	$email_from = $from == ‘‘ ? ‘=?‘.$charset.‘?B?‘."?= <".$from.">" : (preg_match(‘/^(.+?) \<(.+?)\>$/‘,$from, $mats) ? ‘=?‘.$charset.‘?B?‘.base64_encode($mats[1])."?= <$mats[2]>" : $from);

	$email_to = preg_match(‘/^(.+?) \<(.+?)\>$/‘,$toemail, $mats) ? ($mailusername ? ‘=?‘.$charset.‘?B?‘.base64_encode($mats[1])."?= <$mats[2]>" : $mats[2]) : $toemail;;

	$email_subject = ‘=?‘.$charset.‘?B?‘.base64_encode(preg_replace("/[\r|\n]/", ‘‘, $subject)).‘?=‘;
	$email_message = chunk_split(base64_encode(str_replace("\n", "\r\n", str_replace("\r", "\n", str_replace("\r\n", "\n", str_replace("\n\r", "\r", $message))))));

	$headers = "From: $email_from{$maildelimiter}X-Priority: 3{$maildelimiter}X-Mailer: PHPCMS-V9 {$maildelimiter}MIME-Version: 1.0{$maildelimiter}Content-type: text/html; charset=".$charset."{$maildelimiter}Content-Transfer-Encoding: base64{$maildelimiter}";

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

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

	fputs($fp, ($mail[‘auth‘] ? ‘EHLO‘ : ‘HELO‘)." phpcms\r\n");
	$lastmessage = fgets($fp, 512);
	if(substr($lastmessage, 0, 3) != 220 && substr($lastmessage, 0, 3) != 250) {
		runlog(‘SMTP‘, "($mail[server]:$mail[port]) HELO/EHLO - $lastmessage", 0);
		return false;
	}

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

	if($mail[‘auth‘]) {
		fputs($fp, "AUTH LOGIN\r\n");
		$lastmessage = fgets($fp, 512);
		if(substr($lastmessage, 0, 3) != 334) {
			runlog(‘SMTP‘, "($mail[server]:$mail[port]) AUTH LOGIN - $lastmessage", 0);
			return false;
		}

		fputs($fp, base64_encode($mail[‘auth_username‘])."\r\n");
		$lastmessage = fgets($fp, 512);
		if(substr($lastmessage, 0, 3) != 334) {
			runlog(‘SMTP‘, "($mail[server]:$mail[port]) USERNAME - $lastmessage", 0);
			return false;
		}

		fputs($fp, base64_encode($mail[‘auth_password‘])."\r\n");
		$lastmessage = fgets($fp, 512);
		if(substr($lastmessage, 0, 3) != 235) {
			runlog(‘SMTP‘, "($mail[server]:$mail[port]) PASSWORD - $lastmessage", 0);
			return false;
		}

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

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

	fputs($fp, "RCPT TO: <".preg_replace("/.*\<(.+?)\>.*/", "\\1", $toemail).">\r\n");
	$lastmessage = fgets($fp, 512);
	if(substr($lastmessage, 0, 3) != 250) {
		fputs($fp, "RCPT TO: <".preg_replace("/.*\<(.+?)\>.*/", "\\1", $toemail).">\r\n");
		$lastmessage = fgets($fp, 512);
		runlog(‘SMTP‘, "($mail[server]:$mail[port]) RCPT TO - $lastmessage", 0);
		return false;
	}

	fputs($fp, "DATA\r\n");
	$lastmessage = fgets($fp, 512);
	if(substr($lastmessage, 0, 3) != 354) {
		runlog(‘SMTP‘, "($mail[server]:$mail[port]) DATA - $lastmessage", 0);
		return false;
	}

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

	fputs($fp, "Date: ".gmdate(‘r‘)."\r\n");
	fputs($fp, "To: ".$email_to."\r\n");
	fputs($fp, "Subject: ".$email_subject."\r\n");
	fputs($fp, $headers."\r\n");
	fputs($fp, "\r\n\r\n");
	fputs($fp, "$email_message\r\n.\r\n");
	$lastmessage = fgets($fp, 512);
	if(substr($lastmessage, 0, 3) != 250) {
		runlog(‘SMTP‘, "($mail[server]:$mail[port]) END - $lastmessage", 0);
	}
	fputs($fp, "QUIT\r\n");
	return true;
}

?>

php 发送邮件函数

时间: 2024-11-13 07:52:53

php 发送邮件函数的相关文章

python 发送邮件函数模块

发送邮件函数功能 1 #!/usr/bin/env python 2 # -*- coding:utf-8 -*- 3 import smtplib 4 from email.mime.text import MIMEText 5 from email.utils import formataddr 6 def email(message): 7 msg = MIMEText(message, 'plain', 'utf-8') 8 msg['From'] = formataddr(["大大&q

shell发送邮件函数

#发送邮件函数 function send_mail(){ #定义邮件发送列表 maillist=( [email protected] [email protected] [email protected] ) if [ "$1" == "gateway" ];then         for mail in ${maillist[*]};do                 echo "无法ping通网关,请检查网络" | mail -s &

发送邮件函数

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Net.Mail; namespace AIMSCommon { /// <summary> /// 发送邮件 /// </summary> public class SMTPManager { /// <summary> /// 发送邮件 /// </summary&

python笔记- 发送邮件

依赖: Python代码实现发送邮件,使用的模块是smtplib.MIMEText,实现代码之前需要导入包: import smtplib from email.mime.text import MIMEText 使用163邮件发送邮件,具体代码实现如下: import smtplib from email.mime.text import MIMEText ''' 发送邮件函数,默认使用163smtp :param mail_host: 邮箱服务器,16邮箱host: smtp.163.com

PHP中发送邮件的几种方法总结

1. 使用 mail() 函数 没什么好讲的,就是使用系统自带的smtp系统来发送,一般是使用sendmail来发.这个按照各个系统不同而定.使用参考手册. 2. 使用管道的形式 昨天刚测试成功,使用本地的qmail来发送邮件. /* 使用qmail发送邮件函数 */function send_check_mail($email, $subject,$uid,$buffer){ $command =  "/var/qmail/bin/qmail-inject ".$email; //q

PHP_thinkphp框架使用PHPMailer实现发送邮件的功能(转+修改+亲测)

第一步.添加PHPMailer类库 将下载后的文件解压,将PHPMail目录移动至ThinkPHP目录中的Vendor内.(在核心文件里面,建个文件夹名称为phpmailer) 第二步.添加发送邮件函数 在项目目录中的Common文件夹中的common.php文件(也是在核心文件里面的common,或者你喜欢放哪里就哪里,能调用就行)添加如下代码: <?php /** * 邮件发送函数 */function SendMail($address,$title,$message) { vendor(

[Python3]SMTP发送邮件

概述 在本文中,主要介绍使用smtplib进行文本格式.HTML格式和带附件的邮件发送处理. 导入smtplib模块 import smtplib 关键函数说明 # 创建smtp对象 smtp = smtplib.SMTP([host [, port [, localhost]]] ) # 参数说明 # host: smtp服务地址,例如126邮箱的是:smtp.126.com # port: smtp服务端口 # localhost: 如果你的smtp服务在本机,则只需指定localhost即

Delphi发送邮件...

///首先在控件栏定位到:Indy Clients加入控件IdSMTP ///再定位到:Indy Misc加入控件IdMessage ///发送邮件函数 procedure TForm1.SendMail(YYuser: string;YYpass:string); begin try IdSMTP1.AuthenticationType:=atLogin; //设置登陆类型 IdSMTP1.Username:='[email protected]'; //设置登陆帐号 IdSMTP1.Pass

【C#】新建服务自动发送邮件

---windows服务,---自动发送邮件 邮件发送code #region 发送邮件函数 public void SendMailUseZj() { System.Net.Mail.MailMessage msg = new System.Net.Mail.MailMessage(); string[] mailToUsers = ConfigurationManager.AppSettings["mailToUser"].Split(','); for (int i = 0; i