smtp.class.php 这个是类把他做保存到一个文件中.
复制代码 代码如下:
<?php
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
$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 = ereg_replace("(^|(\r\n))(\\.)", "\\1.\\3", $body);
$header .= "MIME-Version:1.0\r\n";
if($mailtype=="HTML"){
$header .= "Content-Type:text/html\r\n";
}
$header .= "To: ".$to."\r\n";
if ($cc != "") {
$header .= "Cc: ".$cc."\r\n";
}
$header .= "From: $from<".$from.">;\r\n";
$header .= "Subject: ".$subject."\r\n";
$header .= $additional_headers;
$header .= "Date: ".date("r")."\r\n";
$header .= "X-Mailer:By Redhat (PHP/".phpversion().")\r\n";
list($msec, $sec) = explode(" ", microtime());
$header .= "Message-ID: <".date("YmdHis", $sec).".".($msec*1000000).".".$mail_from.">;\r\n";
$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."\n");
$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.">;\n");
} else {
$this->log_write("Error: Cannot send email to <".$rcpt_to.">;\n");
$sent = FALSE;
}
fclose($this->sock);
$this->log_write("Disconnected from remote host\n");
}
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."\n");
$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."\n");
$this->log_write("Error: ".$errstr." (".$errno.")\n");
return FALSE;
}
$this->log_write("Connected to relay host ".$this->relay_host."\n");
return TRUE;
}
function smtp_sockopen_mx($address)
{
$domain = ereg_replace("^.+@([^@]+)$", "\\1", $address);
if (!@getmxrr($domain, $MXHOSTS)) {
$this->log_write("Error: Cannot resolve MX \"".$domain."\"\n");
return FALSE;
}
foreach ($MXHOSTS as $host) {
$this->log_write("Trying to ".$host.":".$this->smtp_port."\n");
$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."\n");
$this->log_write("Error: ".$errstr." (".$errno.")\n");
continue;
}
$this->log_write("Connected to mx host ".$host."\n");
return TRUE;
}
$this->log_write("Error: Cannot connect to any mx hosts (".implode(", ", $MXHOSTS).")\n");
return FALSE;
}
function smtp_message($header, $body)
{
fputs($this->sock, $header."\r\n".$body);
$this->smtp_debug(">; ".str_replace("\r\n", "\n".">; ", $header."\n>; ".$body."\n>; "));
return TRUE;
}
function smtp_eom()
{
fputs($this->sock, "\r\n.\r\n");
$this->smtp_debug(". [EOM]\n");
return $this->smtp_ok();
}
function smtp_ok()
{
$response = str_replace("\r\n", "", fgets($this->sock, 512));
$this->smtp_debug($response."\n");
if (!ereg("^[23]", $response)) {
fputs($this->sock, "QUIT\r\n");
fgets($this->sock, 512);
$this->log_write("Error: Remote host returned \"".$response."\"\n");
return FALSE;
}
return TRUE;
}
function smtp_putcmd($cmd, $arg = "")
{
if ($arg != "") {
if($cmd=="") $cmd = $arg;
else $cmd = $cmd." ".$arg;
}
fputs($this->sock, $cmd."\r\n");
$this->smtp_debug(">; ".$cmd."\n");
return $this->smtp_ok();
}
function smtp_error($string)
{
$this->log_write("Error: Error occurred while ".$string.".\n");
return FALSE;
}
function log_write($message)
{
$this->smtp_debug($message);
if ($this->log_file == "") {
return TRUE;
}
$message = date("M d H:i:s ").get_current_user()."[".getmypid()."]: ".$message;
if (!@file_exists($this->log_file) || !($fp = @fopen($this->log_file, "a"))) {
$this->smtp_debug("Warning: Cannot open log file \"".$this->log_file."\"\n");
return FALSE;
}
flock($fp, LOCK_EX);
fputs($fp, $message);
fclose($fp);
return TRUE;
}
function strip_comment($address)
{
$comment = "\\([^()]*\\)";
while (ereg($comment, $address)) {
$address = ereg_replace($comment, "", $address);
}
return $address;
}
function get_address($address)
{
$address = ereg_replace("([ \t\r\n])+", "", $address);
$address = ereg_replace("^.*<(.+)>;.*$", "\\1", $address);
return $address;
}
function smtp_debug($message)
{
if ($this->debug) {
echo $message;
}
}
}
function sendmail($smtpserver,$smtpuser,$smtppass,$smtpemailto,$smtpusermail, $mailsubject, $mailbody){
$smtp = new smtp($smtpserver,25,true,$smtpuser,$smtppass);
//$smtp->debug = TRUE;
$smtp->sendmail($smtpemailto, $smtpusermail, $mailsubject, $mailbody, "HTML");
}
//such as
//sendmail("smtp.126.com","test@126.com","password","1034555083@qq.com","test@126.com","title","body");
?>
测试代码:
复制代码 代码如下:
<?
//ok的邮箱发送。
include "smtp.class.php";
//$smtpserver = "SMTP.163.com"; //您的smtp服务器的地址
$smtpserver="smtp.163.com";
$port =25; //smtp服务器的端口,一般是 25
$smtpuser = "你的邮箱@163.com"; //您登录smtp服务器的用户名
$smtppwd = "你邮箱的密码"; //您登录smtp服务器的密码
$mailtype = "TXT"; //邮件的类型,可选值是 TXT 或 HTML ,TXT 表示是纯文本的邮件,HTML 表示是 html格式的邮件
$sender = "你的邮箱@163.com";
//发件人,一般要与您登录smtp服务器的用户名($smtpuser)相同,否则可能会因为smtp服务器的设置导致发送失败
$smtp = new smtp($smtpserver,$port,true,$smtpuser,$smtppwd,$sender);
$smtp->debug = true; //是否开启调试,只在测试程序时使用,正式使用时请将此行注释
$to = "你要发给的那个人的邮箱地址"; //收件人
$subject = "你好";
$body = "你发送的内容 ";
$send=$smtp->sendmail($to,$sender,$subject,$body,$mailtype);
if($send==1){
echo "邮件发送成功";
}else{
echo "邮件发送失败<br/>";
//echo "原因:".$this->smtp->logs;
}
?>
相关推荐:
智能AI生成文章释放创作新可能
好用的人工智能AI软件推荐,让你的生活更智能!
SEO运维:提升网站排名的核心战略,邢台网站建设优化建站
在线AI写文:开启高效创作新时代
揭开“好的AI软件”背后的秘密:让生活和工作更智能的利器
seO经理是什么岗位,seo经理招聘 ,ai写作重复被查
什么是seo长尾词,什么是seo长尾词的概念 ,绿茶ai换脸杨超越
pbootcms前端翻译插件-轻松实现网站多语言支持,拓展全球市场,st ai绘画
自动生成文章的AI软件,助力内容创作的未来
ChatGPT宕机两小时,OpenAI紧急修复,用户期待AI恢复正常服务,oppo小布ai
SEO大量优化:如何通过精准策略提升网站流量,突破搜索引擎排名瓶颈,吕梁本地网站推广平台
文章疑似AI生成怎么办?如何辨别并应对AI生成文章的挑战
seo稿件是什么意思,seo文章写作要求 ,ai写作未来展望和展望
Bing搜索的注意事项-提高搜索效率与准确性,轻松获取所需信息,最近ai写作软件推荐
SEO优化与SEM广告:提升品牌曝光与流量的双重利器,ai接回头
seo管理系统是什么,seo网站管理 ,过度圆ai
AI优化文章:如何利用人工智能提升写作效率和质量
文章写作AI:让创作更高效、精准的智能助手
SEO笔记:如何打造高效的SEO策略提升网站排名,网站优化优质服务方案
SEO那么重要,企业如何通过它脱颖而出?,自媒体推广营销的概念
SEO考核:如何通过精准的SEO优化提升网站排名与流量,茶艺营销推广方案怎么写
2025年整站SEO排名优化策略:让你的网站脱颖而出,id排版ai
seo资源是什么意思,seo资源怎么打开 ,梅州ai自动插件加工
SEO韩国:为您开启国际市场的增长之门,seo文章标题有哪些
ChatGPT:如果您正在使用VPN,这些技巧您一定要知道!,AI换脸*H
在线AI文章生成器开启智能创作新时代
ChatGPT无法加载?检查您的网络设置并尝试重启,轻松解决常见问题!,ai招生广告设计
SEO建站,开启网站优化的全新篇章!,网络营销推广合作方式
怎么用AI生成文章免费版,高效创作从此开始!
SEO怎么设置:让你的网站快速登上搜索引擎首页的秘密,Ai压印分色
SEO中权重是什么意思?让你迅速网站排名的核心秘密!,长颈鹿智能AI点读机
如何识别文章是否由AI写作:技巧与方法解析
SEO优化10种策略:提升网站排名的有效方法,帅气ai男头白底
2025百度收录优化:提升网站排名,助力企业数字化转型,第一ai做
怎么降低文章的AI生成率:打造更真实、更有价值的内容
ChatGPT显示503:如何应对AI服务不可用的困境?,ai宝贝宝贝
seo种草什么意思,seo yoo na ,三维地震反演AI
ChatGPT无法使用?了解原因及解决方法,轻松恢复智能对话体验!,ai满版图案
AI智能时代的到来:如何利用人工智能推动生活与商业创新,ai相册下载
文章AI扩写:突破创作瓶颈,提升写作效率的秘密武器
seo要学会什么,seo要学多长时间 ,NTU AI 录取
seo需要买什么,seo需要考虑什么 ,ai做表头
SEO部:开启数字化营销新纪元的幕后英雄,株洲营销推广是什么公司
撰写稿子的AI,写作的“超级助手”来了!
seo适用于什么领域,seo适用于什么领域中 ,ai智能翻译写作机器人v1.0
SEO但是,这些常见误区你真的知道吗?,凤岗网站建设开发
ChatGPT:引领人工智能对话新时代的智能助手,Ai shiang
ChatGPT发生故障,背后隐藏着哪些不为人知的原因与挑战?,硬件ai和软件ai
ChatGPT无法加载?检查您的网络设置并尝试重启,轻松解决连接问题!,ai制作艺术字
什么是seo技术,什么是seo及seo的作用 ,AI倒放仓鼠