smtp邮件发送一例

smtp邮件发送一例

test_smtp.php

require("smtp.php");

$smtp=new smtp_class;

$smtp->host_name="mail.xiaocui.com";
$smtp->localhost="localhost";
$from="webmaster@xiaocui.com";
$to="root@xiaocui.com";
if($smtp->SendMessage(
$from,
array(
$to
),
array(
"From: $from",
"To: $to",
"Subject: Testing Manuel Lemos' SMTP class"
),
"Hello $to,nnIt is just to let you know that your SMTP class is working just fine.nnBye.n"))
echo "Message sent to $to OK.n";
else
echo "Cound not send the message to $to.nError: ".$smtp->error."n"
?>

smtp.php

class smtp_class
{
var $host_name="";
var $host_port=25;
var $localhost="";
var $timeout=0;
var $error="";
var $debug=1;
var $esmtp=1;
var $esmtp_host="";
var $esmtp_extensions=array();
var $maximum_piped_recipients=100;

/* private variables - DO NOT ACCESS */

var $state="Disconnected";
var $connection=0;
var $pending_recipients=0;

/* Private methods - DO NOT CALL */

Function OutputDebug($message)
{
echo $message,"
n";
}

Function GetLine()
{
for($line="";;)
{
if(feof($this->connection))
{
$this->error="reached the end of stream while reading from socket";
return(0);
}
if(($data=fgets($this->connection,100))==false)
{
$this->error="it was not possible to read line from socket";
return(0);
}
$line.=$data;
$length=strlen($line);
if($length>=2
&& substr($line,$length-2,2)=="rn")
{
$line=substr($line,0,$length-2);
if($this->debug)
$this->OutputDebug("< $line");
return($line);
}
}
}

Function PutLine($line)
{
if($this->debug)
$this->OutputDebug("> $line");
if(!fputs($this->connection,"$linern"))
{
$this->error="it was not possible to write line to socket";
return(0);
}
return(1);
}

Function PutData($data)
{
if(strlen($data))
{
if($this->debug)
$this->OutputDebug("> $data");
if(!fputs($this->connection,$data))
{
$this->error="it was not possible to write data to socket";
return(0);
}
}
return(1);
}

Function VerifyResultLines($code,$responses="")
{
if(GetType($responses)!="array")
$responses=array();
Unset($match_code);

while(($line=$this->GetLine($this->connection)))
{
if(IsSet($match_code))
{
if(strcmp(strtok($line," -"),$match_code))
{
$this->error=$line;
return(0);
}
}
else
{
$match_code=strtok($line," -");
if(GetType($code)=="array")
{
for($codes=0;$codes if($codes>=count($code))
{
$this->error=$line;
return(0);
}
}
else
{
if(strcmp($match_code,$code))
{
$this->error=$line;
return(0);
}
}
}
$responses[]=strtok("");
if(!strcmp($match_code,strtok($line," ")))
return(1);
}
return(-1);
}

Function FlushRecipients()
{
if($this->pending_sender)
{
if($this->VerifyResultLines("250")<=0)
return(0);
$this->pending_sender=0;
}
for(;$this->pending_recipients;$this->pending_recipients--)
{
if($this->VerifyResultLines(array("250","251"))<=0)
return(0);
}
return(1);
}

/* Public methods */

Function Connect()
{
$this->error=$error="";
$this->esmtp_host="";
$this->esmtp_extensions=array();
if(!($this->connection=($this->timeout ? fsockopen($this->host_name,$this->host_port,&$errno,&$error,$this->timeout) : fsockopen($this->host_name,$this->host_port))))
{
switch($error)
{
case -3:
$this->error="-3 socket could not be created";
return(0);
case -4:
$this->error="-4 dns lookup on hostname "".$host_name."" failed";
return(0);
case -5:
$this->error="-5 connection refused or timed out";
return(0);
case -6:
$this->error="-6 fdopen() call failed";
return(0);
case -7:
$this->error="-7 setvbuf() call failed";
return(0);
default:
$this->error=$error." could not connect to the host "".$this->host_name.""";
return(0);
}
}
else
{
if(!strcmp($localhost=$this->localhost,"")
&& !strcmp($localhost=getenv("SERVER_NAME"),"")
&& !strcmp($localhost=getenv("HOST"),""))
$localhost="localhost";
$success=0;
if($this->VerifyResultLines("220")>0)
{
if($this->esmtp)
{
$responses=array();
if($this->PutLine("EHLO $localhost")
&& $this->VerifyResultLines("250",&$responses)>0)
{
$this->esmtp_host=strtok($responses[0]," ");
for($response=1;$response {
$extension=strtoupper(strtok($responses[$response]," "));
$this->esmtp_extensions[$extension]=strtok("");
}
$success=1;
}
}
if(!$success
&& $this->PutLine("HELO $localhost")
&& $this->VerifyResultLines("250")>0)
$success=1;
}
if($success)
{
$this->state="Connected";
return(1);
}
else
{
fclose($this->connection);
$this->connection=0;
$this->state="Disconnected";
return(0);
}
}
}

Function MailFrom($sender)
{
if(strcmp($this->state,"Connected"))
{
$this->error="connection is not in the initial state";
return(0);
}
$this->error="";
if(!$this->PutLine("MAIL FROM: <".$sender.">"))
return(0);
if(!IsSet($this->esmtp_extensions["PIPELINING"])
&& $this->VerifyResultLines("250")<=0)
return(0);
$this->state="SenderSet";
if(IsSet($this->esmtp_extensions["PIPELINING"]))
$this->pending_sender=1;
$this->pending_recipients=0;
return(1);
}

Function SetRecipient($recipient)
{
switch($this->state)
{
case "SenderSet":
case "RecipientSet":
break;
default:
$this->error="connection is not in the recipient setting state";
return(0);
}
$this->error="";
if(!$this->PutLine("RCPT TO:<".$recipient.">"))
return(0);
if(IsSet($this->esmtp_extensions["PIPELINING"]))
{
$this->pending_recipients++;
if($this->pending_recipients>=$this->maximum_piped_recipients)
{
if(!$this->FlushRecipients())
return(0);
}
}
else
{
if($this->VerifyResultLines(array("250","251"))<=0)
return(0);
}
$this->state="RecipientSet";
return(1);
}

Function StartData()
{
if(strcmp($this->state,"RecipientSet"))
{
$this->error="connection is not in the start sending data state";
return(0);
}
$this->error="";
if(!$this->PutLine("DATA"))
return(0);
if($this->pending_recipients)
{
if(!$this->FlushRecipients())
return(0);
}
if($this->VerifyResultLines("354")<=0)
return(0);
$this->state="SendingData";
return(1);
}

Function PrepareData($data,&$output)
{
$length=strlen(&$data);
for($output="",$position=0;$position<$length;)
{
$next_position=$length;
for($current=$position;$current<$length;$current++)
{
switch($data[$current])
{
case "n":
$next_position=$current+1;
break 2;
case "r":
$next_position=$current+1;
if($data[$next_position]=="n")
$next_position++;
break 2;
}
}
if($data[$position]==".")
$output.=".";
$output.=substr(&$data,$position,$current-$position)."rn";
$position=$next_position;
}
}

Function SendData($data)
{
if(strcmp($this->state,"SendingData"))
{
$this->error="connection is not in the sending data state";
return(0);
}
$this->error="";
return($this->PutData(&$data));
}

Function EndSendingData()
{
if(strcmp($this->state,"SendingData"))
{
$this->error="connection is not in the sending data state";
return(0);
}
$this->error="";
if(!$this->PutLine("rn.")
|| $this->VerifyResultLines("250")<=0)
return(0);
$this->state="Connected";
return(1);
}

Function ResetConnection()
{
switch($this->state)
{
case "Connected":
return(1);
case "SendingData":
$this->error="can not reset the connection while sending data";
return(0);
case "Disconnected":
$this->error="can not reset the connection before it is established";
return(0);
}
$this->error="";
if(!$this->PutLine("RSET")
|| $this->VerifyResultLines("250")<=0)
return(0);
$this->state="Connected";
return(1);
}

Function Disconnect($quit=1)
{
if(!strcmp($this->state,"Disconnected"))
{
$this->error="it was not previously established a SMTP connection";
return(0);
}
$this->error="";
if(!strcmp($this->state,"Connected")
&& $quit
&& (!$this->PutLine("QUIT")
|| $this->VerifyResultLines("221")<=0))
return(0);
fclose($this->connection);
$this->connection=0;
$this->state="Disconnected";
return(1);
}

Function SendMessage($sender,$recipients,$headers,$body)
{
if(($success=$this->Connect()))
{
if(($success=$this->MailFrom($sender)))
{
for($recipient=0;$recipient {
if(!($success=$this->SetRecipient($recipients[$recipient])))
break;
}
if($success
&& ($success=$this->StartData()))
{
for($header_data="",$header=0;$header $header_data.=$headers[$header]."rn";
if(($success=$this->SendData($header_data."rn")))
{
$this->PrepareData($body,&$body_data);
$success=$this->SendData($body_data);
}
if($success)
$success=$this->EndSendingData();
}
}
$disconnect_success=$this->Disconnect($success);
if($success)
$success=$disconnect_success;
}
return($success);
}

};

?>  

时间: 2024-08-24 12:19:26

smtp邮件发送一例的相关文章

smtp邮件发送一例_php基础

test_smtp.php <?require("smtp.php"); $smtp=new smtp_class; $smtp->host_name="mail.xiaocui.com";$smtp->localhost="localhost";$from="webmaster@xiaocui.com";$to="root@xiaocui.com";if($smtp->SendMe

PHP实现支持SSL连接的SMTP邮件发送类

 这篇文章主要介绍了PHP实现支持SSL连接的SMTP邮件发送类,实例分析了php实现smtp邮件发送类的原理与技巧,以及支持SSL连接的方法,需要的朋友可以参考下     本文实例讲述了PHP实现支持SSL连接的SMTP邮件发送类.分享给大家供大家参考.具体如下: 该实例代码测试过了gmail和QQ邮箱的SMTP.具体代码如下: 代码如下: <?php /** * 邮件发送类 * 支持发送纯文本邮件和HTML格式的邮件,可以多收件人,多抄送,多秘密抄送,带附件(单个或多个附件),支持到服务器的

PHP实现支持SSL连接的SMTP邮件发送类_php技巧

本文实例讲述了PHP实现支持SSL连接的SMTP邮件发送类.分享给大家供大家参考.具体如下: 该实例代码测试过了gmail和QQ邮箱的SMTP.具体代码如下: 复制代码 代码如下: <?php /** * 邮件发送类 * 支持发送纯文本邮件和HTML格式的邮件,可以多收件人,多抄送,多秘密抄送,带附件(单个或多个附件),支持到服务器的ssl连接 * 需要的php扩展:sockets.Fileinfo和openssl. * 编码格式是UTF-8,传输编码格式是base64 * @example *

email-django smtp邮件发送问题

问题描述 django smtp邮件发送问题 #setting.py配置 #邮件配置 EMAIL_HOST = 'smtp.163.com' #SMTP地址 EMAIL_PORT = 25 #SMTP端口 EMAIL_HOST_USER = '****@163.com' #我自己的邮箱 EMAIL_HOST_PASSWORD = '****' #我的邮箱密码 EMAIL_USE_TLS = False send_mail( 'Feedback from your site, topic: %s'

smtp邮件发送,qq邮箱图片不显示

问题描述 smtp邮件发送,qq邮箱图片不显示,图片是互联网存在的图片 解决方案 解决方案二:是当作附件发了么??邮件中的图片,貌似都是<imgsrc=url...>是网络的图片,

用c#写的smtp邮件发送类

smtp //**********************Created by Chen**************************using System;using System.IO;using System.Text;using System.Net;using System.Net.Sockets;using System.Collections;using System.Collections.Specialized;using KSN.Exceptions;using KS

C# SMTP邮件发送

邮件发送在网站应用程序中经常会用到,包括您现在看到的博客,在添加评论后,系统会自动发送邮件通知到我邮箱的,把系统发送邮件的功能整理了下,做了一个客户端Demo,希望对有需要的童鞋有所帮助: 核心代码: 001 using System; 002 using System.Net; 003 using System.Net.Mail; 004 using System.Text; 005   006 namespace HC.Email 007 { 008     /// <summary> 0

ASP.NET Core 1.0实现邮件发送功能_实用技巧

准备将一些项目迁移到 asp.net core 先从封装类库入手,在遇到邮件发送类时发现在 asp.net core 1.0中并示提供SMTP相关类库,于是网上一搜发现了MailKit  好东西一定要试一下,何况是开源,下面是代码可实现SMTP邮件发送:  using MailKit.Net.Smtp; using MailKit.Security; using MimeKit; using System.Threading.Tasks; namespace ConsoleApp1 { publ

WordPress网站实现邮件发送功能例子

windows主机是无法支持mail()函数的,所以无法使用WordPress自带的邮箱服务,而发送邮件的功能对于一个网站来说是必不可少的,对于使用WordPress建站的站长来说,除了可以使用主机邮箱功能发送邮件外,也可以用WP的插件或者直接修改WP的代码在WP网站中实现邮件发送功能. 下面我们就来讨论下用WP的SMTP插件和修改WP代码的两种实现邮件发送功能的方法. 1.使用WP的SMTP插件实现邮件功能 WP 的SMTP插件是国人制作的,设置界面非常简单,是一款全中文的的插件. WP SM