phpmailer邮箱发送插件中文说明

 更新时间:2016年11月25日 17:01  点击:1734
本文章详细的介绍了关于phpmailer邮箱发送插件中文说明,有需要的朋友可以参考一下,phpmailer是一款强大的邮件发送功能。

$AltBody–属性
出自:PHPMailer::$AltBody
文件:class.phpmailer.php
说明:该属性的设置是在邮件正文不支持HTML的备用显示
AddAddress–方法
出自:PHPMailer::AddAddress(),文件:class.phpmailer.php
说明:增加收件人。参数1为收件人邮箱,参数2为收件人称呼。例 AddAddress(“xiaoxiaoxiaoyu@xiaoxiaoyu.cn”,”xiaoxiaoyu”),但参数2可选,AddAddress(xiaoxiaoxiaoyu@xiaoxiaoyu.cn)也是可以的。
函数原型: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中引用
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连接[/color]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

本文章介绍了关于在thinkphp中利用了phpmailer来实现邮件发送的详细教程,有需要的朋友可以参考一下。

本文所使用的是ThinkPHP 2.1版和 PHPMailer 5.1版。(后者建议您直接从本博下载,因为我们不能保证下面的代码在所有版本的PHPMailer中都能正常运行)

下面是具体步骤:

第一步、添加PHPMailer类库

点击此处下载

将下载后的文件解压,将PHPMail目录移动至ThinkPHP目录中的Vendor内。(请确保class.phpmailer.php文件就在ThinkPHPVendorPHPMailerclass.phpmailer.php)

第二步、添加发送邮件函数

在项目目录中的Common文件夹中的common.php文件(如果没有请创建)添加如下代码:

 代码如下 复制代码

<?php
/**********
 * 发送邮件 *
 **********/
function SendMail($address,$title,$message)
{
    vendor('PHPMailer.class#PHPMailer');

    $mail=new PHPMailer();
    // 设置PHPMailer使用SMTP服务器发送Email
    $mail->IsSMTP();

    // 设置邮件的字符编码,若不指定,则为'UTF-8'
    $mail->CharSet='UTF-8';

    // 添加收件人地址,可以多次使用来添加多个收件人
    $mail->AddAddress($address);

    // 设置邮件正文
    $mail->Body=$message;

    // 设置邮件头的From字段。
    $mail->From=C('MAIL_ADDRESS');

    // 设置发件人名字
    $mail->FromName='LilyRecruit';

    // 设置邮件标题
    $mail->Subject=$title;

    // 设置SMTP服务器。
    $mail->Host=C('MAIL_SMTP');

    // 设置为“需要验证”
    $mail->SMTPAuth=true;

    // 设置用户名和密码。
    $mail->Username=C('MAIL_LOGINNAME');
    $mail->Password=C('MAIL_PASSWORD');

    // 发送邮件。
    return($mail->Send());
}

?>


第三步、配置邮箱信息

编辑Conf目录下的config.php,在return array添加如下内容

 代码如下 复制代码

    'MAIL_ADDRESS'=>'xxx@126.com', // 邮箱地址
    'MAIL_SMTP'=>'smtp.126.com', // 邮箱SMTP服务器
    'MAIL_LOGINNAME'=>'xxx', // 邮箱登录帐号
    'MAIL_PASSWORD'=>'xxx', // 邮箱密码

其中的邮箱登录帐号可能需要包括@后面的内容,请自行尝试^_^

第四步、在Action中发送邮件

由于ThinkPHP会自动加载common.php中的函数,所以在需要发送邮件的时候,只需要使用如下代码即可。

 代码如下 复制代码

SendMail("xxx@xxx.com","邮件标题","邮件正文");

至此,本教程已经结束。撒花~~~

欢迎成功配置的同学SendMail("dreamrunner@foxmail.com","我也会用ThinkPHP发送邮件啦","哇啦哇啦~~");

还有一些说明

如果邮件正文中需要换行怎么办呢?其实,最简单的方法是——

SendMail("xxx@xxx.com","邮件标题","邮件正文
我换行啦~~!");

 

QQ邮箱(含foxmail)和网易的126、163都测试成功。因为近期访问GMail比较吃力,所以没有测试。听说GMail要求必须使用SSL,有这方面需要的同学自行谷歌百度PHPMailer的方法。

 

利用php教程 mail()进行邮件发送实现方法

mail() 函数允许您从脚本中直接发送电子邮件。

如果邮件的投递被成功地接收,则返回 true,否则返回 false。

语法
mail(to,subject,message,headers,parameters)

参数 描述
to 必需。规定邮件的接收者。
subject 必需。规定邮件的主题。该参数不能包含任何换行字符。
message 必需。规定要发送的消息。
headers 必需。规定额外的报头,比如 From, Cc 以及 Bcc。
parameters 必需。规定 sendmail 程序的额外参数。

简单的发送html内容

<html>
  <head>
  <title>Simple Send Mail Form</title>
  </head>
  <body>
  <h1>Mail Form</h1>
  <form name="form1" method="post" action="SimpleEmail.php">
  <table>
      <tr><td><b>To</b></td><td><input type="text" name="mailto" size="35"></td></tr>
      <tr><td><b>Subject</b></td>
          <td><input type="text" name="mailsubject" size="35"></td></tr>
      <tr><td><b>Message</b></td>
          <td><textarea name="mailbody" cols="50" rows="7"></textarea></td>
      </tr>
      <tr><td colspan="2">
            <input type="submit" name="Submit" value="Send">
          </td>
      </tr>
   </table>
 </form>
 </body>
 </html>
 
 
<!-- SimpleEmail.php
  <?php
    if (empty ($mailto) ) {
       die ( "Recipient is blank! ") ;
    }

    if (empty ($mailsubject) ){
       $mailsubject=" " ;
    }

    if (empty ($mailbody) ) {
       $mailbody=" " ;
    }

    $result = mail ($mailto, $mailsubject, $mailbody) ;

    if ($result) {
       echo "Email sent successfully!" ;
    }else{
       echo "Email could not be sent." ;
    }
?>

带上抄送与密送功能

HTML>
  <HEAD>
  <TITLE>Send email with CC and BCC</TITLE>
  </HEAD>
  <BODY>
  <FORM action="sendemailWithCC_BCC.php" method=post name=form1>
  <TABLE>
    <TBODY>
    <TR>
      <TD>
       <DIV align=right><b>To</b></DIV></TD>
      <TD>
        <p>Name <INPUT name=mailtoname size=35><BR>E-mail
                <INPUT name=mailtomail size=35></p></TD></TR>
    <TR>
      <TD>
        <DIV align=right><b>CC</b></DIV></TD>
      <TD><INPUT name=mailcc size=35> </TD></TR>
    <TR>
      <TD>
        <DIV align=right><b>BCC</b></DIV></TD>
      <TD><INPUT name=mailbcc size=35> </TD></TR>
    <TR>
      <TD>
        <DIV align=right><b>Priority</b></DIV></TD>
      <TD><SELECT name=mailpriority>
            <OPTION value=1>Highest</OPTION>
            <OPTION value=2>High</OPTION>
            <OPTION selected value=3>Normal</OPTION>
            <OPTION value=4>Low</OPTION>
            <OPTION value=5>Lowest</OPTION>
          </SELECT>
      </TD></TR>
    <TR>
      <TD><DIV align=right><b>Subject</b></DIV></TD>
      <TD><INPUT name=mailsubject size=35></TD></TR>
    <TR>
      <TD>
        <DIV align=right><b>Message</b> </DIV></TD>
      <TD><TEXTAREA cols=50 name=mailbody rows=7></TEXTAREA> </TD></TR>
    <TR>
      <TD colSpan=2>
        <DIV align=center><INPUT name=Submit type=submit value=Submit></DIV>
    </TD>
    </TR>
   </TBODY>
   </TABLE>
  </FORM>
  </BODY>
  </HTML>
 
 
 
 
<!-- sendemailWithCC_BCC.php

  <html>
  <head>
  <title>Mail Sent</title>
  </head>
  <body>
  <?php
 
    $message= " " ;
    if (empty ( $mailtoname) || empty ( $mailtomail) ) {
       die ( "Recipient is blank! ") ;
    }else{
       $to = $mailtoname . " <" . $mailtomail . ">" ;
    }
   
    if ( empty ( $mailsubject) ) {
      $mailsubject=" ";
    }

    if (($mailpriority>0) && ($mailpriority<6)) {
       $mailheader = "X-Priority: ". $mailpriority ."n";
    }

    $mailheader.= "From: " . "Sales Team <sales@yourdomain.com>n";
    $mailheader.= "X-Sender: " . "support@yourdomain.comn";
    $mailheader.= "Return-Path: " . "support@yourdomain.comn";

    if (!empty($mailcc)) {
      $mailheader.= "Cc: " . $mailcc ."n";
    }

    if (!empty($mailbcc)) {
      $mailheader.= "Bcc: " . $mailbcc ."n";
    }
   
    if (empty($mailbody)) {
      $mailbody=" ";
    }
 
    $result = mail ($to, $mailsubject, $mailbody, $mailheader);
    echo "<center><b>Mail sent to ". "$to". "<br>";
    echo $mailsubject. "<br>";
    echo $mailbody. "<br>";
    echo $mailheader. "<br>";
    if ($result) {
       echo "<p><b>Email sent successfully!</b></p>";
    }else{
       echo "<p><b>Email could not be sent. </b></p>";
    }
  ?>
  <div align="center">
  <table><tr><td width="66"><div align="right"><b>To</b></div></td>
             <td width="308"><b><?php echo $mailtoname . " [". $mailtomail . " ]";?></b></td></tr>
        
         <tr><td width="66"><div align="right"><b>CC</b></div></td>
             <td width="308"><b><?php echo $mailcc;?></b></td></tr>
         <tr><td width="66"><div align="right"><b>BCC</b></div></td>
             <td width="308"><b><?php echo $mailbcc; ?></b></td></tr>
         <tr><td width="66"><div align="right"><b>Priority</b></div></td>
             <td width="308"><b><?php echo $mailpriority;?></b></td></tr>
         <tr><td width="66"><div align="right"><b>Subject </b></div></td>
             <td width="308"><b><?php echo $mailsubject;?></b></td></tr>
         <tr><td width="66"><div align="right"><b>Message</b></div></td>
             <td width="308"><b><?php echo $mailbody;?></b></td></tr>
  </table>
  </div>
  </body>
  </html>


在 message 参数规定的消息中,行之间必须以一个 LF(n)分隔。每行不能超过 70 个字符。

(Windows 下)当 PHP 直接连接到 SMTP 服务器时,如果在一行开头发现一个句号,则会被删掉。要避免此问题,将单个句号替换成两个句号。

 

下面是一款婚恋网站的邮箱发送实例代码,有需要的朋友可以下载

<?php教程
//if ($err) {echo "发送邮件失败,原因:<br>";foreach($err as $a){echo $a."<br>";}}

class wrzc_netmail {
/*  var $localhost;
 var $smtp_accname;
 var $smtp_password;
 var $smtp_host;
 var $from;
 var $fromname;
 */ function send($to, $subject = 'no subject', $body) {
 $localhost = $this->localhost;
 $smtp_accname = $this->smtp_accname;
 $smtp_password = $this->smtp_password;
 $smtp_host = $this->smtp_host;
 $from = $this->from;
 $fromname = $this->fromname;
 $lb = "rn";
 $headers = "content-type: text/html;charset="gbk"";
 $headers.= $lb;
 $headers.= "content-transfer-encoding: base64";
 $hdr = explode($lb, $headers);
 if ($body) {
 $bdy = preg_replace("/^./", "..", explode($lb, $body));}
 $smtp[] = array("ehlo ".$localhost.$lb, "220,250", "ehlo error: ");
 $smtp[] = array("auth login".$lb, "334", "auth error: ");
 $smtp[] = array(base64_encode($smtp_accname).$lb, "334", "authentification error: ");
 $smtp[] = array(base64_encode($smtp_password).$lb, "235", "authentification error: ");
 $smtp[] = array("mail from: <".$from.">".$lb, "250", "mail from error: ");
 $smtp[] = array("rcpt to: <".$to.">".$lb, "250", "rcpt to error: ");
 $smtp[] = array("data".$lb, "354", "data error: ");
 $smtp[] = array("from: ".$fromname." <".$from.">".$lb, "", "");
 $smtp[] = array("subject: ".$subject.$lb, "", "");
 $smtp[] = array("to: ".$to.$lb, "", "");
 foreach ($hdr as $h) {
 $smtp[] = array($h.$lb, "", "");}
 $smtp[] = array($lb, "", "");
 if ($bdy) {
 foreach ($bdy as $b) {
 $smtp[] = array(base64_encode($b.$lb).$lb, "", "");}}
 $smtp[] = array(".".$lb, "250", "data(end)error: ");
 $smtp[] = array("quit".$lb, "221", "quit error: ");
 $fp = @fsockopen($smtp_host, 25);
 if (!$fp)
 return "error: cannot conect to '".$smtp_host."' by port 25";
 while ($result = @fgets($fp, 1024)) {
 if (substr($result, 3, 1) == " ") {
 break;}}
 $result_str;
 foreach ($smtp as $req) {
 @fputs($fp, $req[0]);
 if ($req[1]) {
 while ($result = @fgets($fp, 1024)) {
 if (substr($result, 3, 1) == " ") {
 break;}};
 if (!strstr($req[1], substr($result, 0, 3))) {
 $result_str[] = $req[2].$result;}}}
 @fclose($fp);
 return $result_str;}
 function setlocalhost($localhost) {$this->localhost = $localhost;}
 function setsmtp_accname($smtp_accname) {$this->smtp_accname = $smtp_accname;}
 function setsmtp_password($smtp_password) {$this->smtp_password = $smtp_password;}
 function setsmtp_host($smtp_host) {$this->smtp_host = $smtp_host;}
 function setfrom($from) {$this->from = $from;}
 function setfromname($fromname) {$this->fromname = $fromname;}
}
//unset
?>

php教程 fsockopen邮箱发送实例代码

<?
//ok的邮箱发送。
include "smtp.class.php";
//$smtps教程erver = "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;
}
?>

邮箱发送类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("(^|(rn))(.)", "1.3", $body);
$header .= "mime-version:1.0rn";
if($mailtype=="html"){
$header .= "content-type:text/htmlrn";
}
$header .= "to: ".$to."rn";
if ($cc != "") {
$header .= "cc: ".$cc."rn";
}
$header .= "from: $from<".$from.">;rn";
$header .= "subject: ".$subject."rn";
$header .= $additional_headers;
$header .= "date: ".date("r")."rn";
$header .= "x-mailer:by redhat (php/".phpversion().")rn";
list($msec, $sec) = explode(" ", microtime());
$header .= "message-id: <".date("ymdhis", $sec).".".($msec*1000000).".".$mail_from.">;rn";
$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 hostn");
}
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."rn".$body);
$this->smtp_debug(">; ".str_replace("rn", "n".">; ", $header."n>; ".$body."n>; "));
return true;
}
function smtp_eom()
{
fputs($this->sock, "rn.rn");
$this->smtp_debug(". [eom]n");
return $this->smtp_ok();
}
function smtp_ok()
{
$response = str_replace("rn", "", fgets($this->sock, 512));
$this->smtp_debug($response."n");
if (!ereg("^[23]", $response)) {
fputs($this->sock, "quitrn");
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."rn");
$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("([ trn])+", "", $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");
?>

更多关于fsockopen函数请查看

http://www.111cn.net/phper/php-function/33796.htm

[!--infotagslink--]

相关文章

  • NodeJS实现阿里大鱼短信通知发送

    本文给大家介绍的是nodejs实现使用阿里大鱼短信API发送消息的方法和代码,有需要的小伙伴可以参考下。...2016-01-20
  • PHP测试成功的邮件发送案例

    mail()函数的作用:连接到邮件服务器,利用smtp协议,与该服务器交互并投邮件。注意:1、mail函数不支持esmtp协议,---即,只能直投,不能登陆2、由上条,我们只能直投至最终的收件服务器地址.而该地址,又是在PHP.ini中指定的,所...2015-10-30
  • php邮件发送的两种方式

    这篇文章研究的主要内容就是使用PHP来发送电子邮件,总结为以下两种方法:一、使用PHP内置的mail()函数<&#63;php $to = "test@163.com"; //收件人 $subject = "Test"; //主题 $message = "This is a test mail!"; //正文...2015-10-30
  • c# 实现发送邮件的功能

    这篇文章主要介绍了c# 如何实现发送邮件的功能,文中示例代码非常详细,帮助大家更好的理解和学习,感兴趣的朋友可以了解下...2020-07-07
  • php邮件发送的两种方式

    这篇文章研究的主要内容就是使用PHP来发送电子邮件,总结为以下两种方法:一、使用PHP内置的mail()函数<&#63;php $to = "test@163.com"; //收件人 $subject = "Test"; //主题 $message = "This is a test mail!"; //正文...2015-10-30
  • python实现企业微信定时发送文本消息的实例代码

    这篇文章主要介绍了python实现企业微信定时发送文本消息的实例代码,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下...2020-11-25
  • PHP测试成功的邮件发送案例

    mail()函数的作用:连接到邮件服务器,利用smtp协议,与该服务器交互并投邮件。注意:1、mail函数不支持esmtp协议,---即,只能直投,不能登陆2、由上条,我们只能直投至最终的收件服务器地址.而该地址,又是在PHP.ini中指定的,所...2015-10-30
  • phpMailer 发送邮件

    //原创:www.111cn.net 注明:转载说明来处www.111cn.net // 昨天听一网友说用php 里面的mail发邮件发不出去,我想一般都是发不了的,现在大多数据邮件提供商都不准那样了...2016-11-25
  • php中利用curl smtp发送邮件实例

    本文章来介绍人一下关于与我们不同的发送邮件的方法我们来利用php curl stmp来实现邮件的发送程序。 $ telnet 邮箱SMTP服务地址 25 Trying 邮箱服务IP地址......2016-11-25
  • Python基于httpx模块实现发送请求

    这篇文章主要介绍了Python基于httpx模块实现发送请求,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下...2020-07-08
  • node.js 基于 STMP 协议和 EWS 协议发送邮件

    这篇文章主要介绍了node.js 基于 STMP 协议和 EWS 协议发送邮件的示例,帮助大家更好的理解和使用node.js,感兴趣的朋友可以了解下...2021-02-15
  • php定时发送邮件

    <?php // 请求 PHPmailer类 文件 require_once("class.phpmailer.php"); //发送Email函数 function smtp_mail ( $sendto_email, $subject, $body, $extra_hd...2016-11-25
  • 解决PHPMailer错误SMTP Error: Could not connect to SMTP host的办法

    PHPMailer发邮件时提示SMTP Error: Could not connect to SMTP host错误是smtp服务器的问题我们一起来看看关于SMTP Error: Could not connect to SMTP host问题的解...2016-11-25
  • php天翼开放平台短信发送接口实现

    临时性需求,研究了一下天翼开发平台的东西,用来发送验证码还是不错的,但是每日限额不多,所以很鸡肋,但是保证100%到达 买的话还是蛮贵的,代码没有做任何优化处理,只是测试是...2016-11-25
  • C#实现异步发送邮件的方法

    这篇文章主要介绍了C#实现异步发送邮件的方法,涉及C#异步操作与邮件发送的技巧,非常具有实用价值,需要的朋友可以参考下...2020-06-25
  • Qt实现UDP多线程数据处理及发送的简单实例

    本文主要介绍了Qt实现UDP多线程数据处理及发送的简单实例,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2021-10-21
  • phpmailer发送邮件 SMTP Error: Could not authenticate 错误

    今天在使用phpmailer发送邮件时居然提示SMTP Error: Could not authenticate,这个感觉是smtp设置的问题,下面我在网上找到了几种解决办法。 今天在使用phpmailer发...2016-11-25
  • phpmailer 发送邮件实例代码

    header("Content-type:text/html;charset=utf-8"); include('phpmailer/class.phpmailer.php'); include('phpmailer/class.smtp.php'); $mail = new PHPMailer();...2016-11-25
  • C++实现含附件的邮件发送功能

    这篇文章主要为大家详细介绍了C++实现含附件的邮件发送功能,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2020-04-25
  • phpmailer邮件发送实例(163邮箱 126邮箱 yahoo邮箱)

    phpmailer是一个非常优秀的php邮箱发送插件了,他可以几乎实现任何邮箱登录发送,下面我介绍163邮箱 126邮箱 yahoo邮箱的发送方法。 准备工作: 我们必须注册一个邮...2016-11-25