php 利用phpmailer 在线发送邮件

 更新时间:2016年11月25日 17:01  点击:2057

html代码

<form action="lead_mail_send.php" method="Post" enctype="multipart/form-data" name="myfm">
  <table width="80%" border="0" align="center" cellspacing="6">
    <tr>
      <td width="10%">邮箱:</td>
      <td width="90%"><label>
        <input name="mail" type="text" id="mail" size="18" />
      </label></td>
    </tr>
    <tr>
      <td>电话:</td>
      <td><input name="tel" type="text" id="tel" size="18" /></td>
    </tr>
    <tr>
      <td>内容:</td>
      <td><label>
        <textarea name="bodys" id="bodys" cols="45" rows="5"></textarea>
      </label></td>
    </tr>
    <tr>
      <td>&nbsp;</td>
      <td><label>
        <input name="button2" type="submit" class="but01" id="button2" value="发布提交" onclick="return check();" />
        <input name="button3" type="reset" class="but02" id="button3" value="清楚建议" />
      </label></td>
    </tr>
  </table>
</form>
去down.111cn.net教程下载一个phpmailer源码教程

require("mail/class.phpmailer.php"); 
 
 $mail = new PHPMailer(); 
 $mail->IsSMTP();                                      // set mailer to use SMTP
 $mail->Host = "smtp.163.com";  // smtp1.example.com;smtp2.example.comspecify main and backup server
 $mail->SMTPAuth = true;     // turn on SMTP authentication
 $mail->Username = "mailangel123";  // SMTP username
 $mail->Password = "*******"; // SMTP password
 
 
 $mail->From = "mailangel123@163.com";
 $mail->FromName = "人才自荐";
 $Subject =PostGet('tel',1);
 $jmail =PostGet('mail',1);
 $body =PostGet('bodys',1);
  $array ='zjj@oeoa.cn';
  
  $mail->AddReplyTo("mailangel123@163.com", "此邮箱无需回复!");
  $mail->AddAddress($array,'您好!');
  $mail->WordWrap = 50;
  $mail->CharSet="GB2312";                                  // set word wrap to 50 characters
  //$mail->AddAttachment("/var/tmp/file.tar.gz");         // add attachments
  //$mail->AddAttachment("/tmp/image.jpg", "new.jpg");    // optional name
  $mail->IsHTML(true);                                  // set email format to HTML
  
  $mail->Subject = $Subject;
  $mail->Body    = $body.'<br>'.$jmail;
  //$mail->AltBody = "This is the body in plain text for non-HTML mail clients";
  
  if(!$mail->Send())
  {
     echo "Message could not be sent. <p>";
     echo "Mailer Error: " . $mail->ErrorInfo;
     $mail->ClearAddresses();  
     $mail->ClearAttachments();
    
  }
  else
  {
   echo "您好,你的信息己经发送到我们负责人信箱,请等候佳声。<a href="javascript教程:window.close();">点击

关闭</a>";
  }

  
 
 /* echo "<script>alert('发关".$count."邮件成功');</script>"; */

?>
// 本站原创转载注明来源www.111cn.net

POP3收取邮件的类在前面的文章中已经给大家做了详细的介绍,下面我们来看看如何应用这个类:

 代码如下 复制代码


  <?

  include("pop3.inc.php教程");

  $host="pop.china.com";

  $user="boss_ch";

  $pass="026007";

  $rec=new pop3($host,110,2);

  if (!$rec->open()) die($rec->err_str);

  echo "open ";

  if (!$rec->login($user,$pass)) die($rec->err_str);

  echo "login";

  if (!$rec->stat()) die($rec->err_str);

  echo "共有".$rec->messages."封信件,共".$rec->size."字节大小
";

  

  if ($rec->messages>0)

   {

   if (!$rec->listmail()) die($rec->err_str);

   echo "有以下信件:
";

   for ($i=1;$i<=count($rec->mail_list);$i++)

   {

   echo "信件".$rec->mail_list[$i][num]."大小:".$rec->mail_list[$i][size]."<BR>";

   }

   $rec->getmail(1);

   echo "邮件头的内容:
";

   for ($i=0;$i<count($rec->head);$i++)

   echo htmlspecialchars($rec->head[$i])."
";

   echo "邮件正文 :<BR>";

   for ($i=0;$i<count($rec->body);$i++)

   echo htmlspecialchars($rec->body[$i])."
";

   }

  $rec->close();

  ?>



  如果你把pop3类中的debug设为true的话,你还可以看到程序与pop3服务器是如何对话的,用于正在调试的程序来说,这样显得更为直观。



  小结

  从以上的这个实例我们可以看到PHP真的是网站开发的一个功能非常强大的工具,但是也可以感觉到,PHP做为一种混合形的语言,其面对对象的开发与其它的工具如java相比还存在不少让人遗憾的地方。这个pop类的实现也还有不少需要改进之处,欢迎各位同仁们指导。

header("Content-type:text/html;charset=utf-8");
include('phpmailer/class.phpmailer.php');
include('phpmailer/class.smtp.php');

$mail = new PHPMailer();  
 
$mail->IsSMTP();                                      // set mailer to use SMTP  
$mail->Host = "smtp.sohu.com";  // SMTP服务器  
$mail->Port = 25;
$mail->SMTPAuth = true;     // SMTP认证?  
$mail->Username = "yourmail@sohu.com";  // 用户名  
$mail->Password = "yourmail168"; // 密码  
$mail->From = "spr_zsql@163.com"; //发件人地址  
$mail->FromName = "test"; //发件人  
$mail->AddAddress("yourmail@qq.com", "test"); //收件人地址,收件人名称  
 
 
 
$mail->WordWrap = 50;                                 //   
//$mail->AddAttachment("/var/tmp/file.tar.gz");         // 附件  
//$mail->AddAttachment("/tmp/image.jpg", "new.jpg");    // 附件,新文件名  
$mail->IsHTML(true);                                  // HTML格式  
 
$mail->Subject    = "测试";
$mail->Body       = "测试";
              
if(!$mail->Send())
{
 echo "Mailer Error: " . $mail->ErrorInfo;
 echo "发送邮件错误!";
}else{
 echo "邮件发送成功!";
}

<?php

class AttachmentMail {
 private $from = 'yours@email.com';
 private $from_name = 'Your Name';
 private $reply_to = 'yours@email.com';
 private $to = '';
 private $subject = '';
 private $message = '';
 private $attachment = '';
 private $attachment_filename = '';

 public function __construct($to, $subject, $message, $attachment = '', $attachment_filename = '') {
  $this -> to = $to;
  $this -> subject = $subject;
  $this -> message = $message;
  $this -> attachment = $attachment;
  $this -> attachment_filename = $attachment_filename;
 }

 public function mail() {
  if (!empty($this -> attachment)) {
   $filename = empty($this -> attachment_filename) ? basename($this -> attachment) : $this -> attachment_filename ;
   $path = dirname($this -> attachment);
   $mailto = $this -> to;
   $from_mail = $this -> from;
   $from_name = $this -> from_name;
   $replyto = $this -> reply_to;
   $subject = $this -> subject;
   $message = $this -> message;

      $file = $path.'/'.$filename;
      $file_size = filesize($file);
      $handle = fopen($file, "r");
      $content = fread($handle, $file_size);
      fclose($handle);
      $content = chunk_split(base64_encode($content));
      $uid = md5(uniqid(time()));
      $name = basename($file);
      $header = "From: ".$from_name." <".$from_mail."> ";
      $header .= "Reply-To: ".$replyto." ";
      $header .= "MIME-Version: 1.0 ";
      $header .= "Content-Type: multipart/mixed; boundary="".$uid."" ";
      $header .= "This is a multi-part message in MIME format. ";
      $header .= "--".$uid." ";
      $header .= "Content-type:text/plain; charset=iso-8859-1 ";
      $header .= "Content-Transfer-Encoding: 7bit ";
      $header .= $message." ";
      $header .= "--".$uid." ";
      $header .= "Content-Type: application/octet-stream; name="".$filename."" "; // use diff. tyoes here
      $header .= "Content-Transfer-Encoding: base64 ";
      $header .= "Content-Disposition: attachment; filename="".$filename."" ";
      $header .= $content." ";
      $header .= "--".$uid."--";

      if (mail($mailto, $subject, "", $header)) {
       return true;
      } else {
          return false;
      }
  } else {
      $header = "From: ".($this -> from_name)." <".($this -> from)."> ";
      $header .= "Reply-To: ".($this -> reply_to)." ";
      if (mail($this -> to, $this -> subject, $this -> message, $header)) {
       return true;
      } else {
          return false;
      }

  }
 }
}


?>

调用方法
<?php
require ('Email-Attachment.php');

$sendit = new AttachmentEmail('marry@example.com', 'Merry Christmas!', 'Hi', '/home/racker/gift.jpg');
$sendit -> mail();
?>

 

这个例子说明如何发送电子邮件使用SMTP身份验证。此特定示例使用Gmail发送。为通过SMTP发送如需Gmail要求的SMTP TLS身份验证。幸运的是,当我们使用梨邮件认证,连接自动TLS的。

这个例子之间的区别,只是在前面的示例使用SMTP是增加以下SMTP参数。

$smtp_params["auth"]     = true;
$smtp_params["username"] = "user@gmail.com";
$smtp_params["password"] = "pass";
<?
        include('Mail.php');
        include('Mail/mime.php');
 
        // Constructing the email
        $sender = "user@gmail.com";                                             // Your email address
        $recipient = "Leigh <leigh@no_spam.net>";                               // The Recipients name and email address
        $subject = "Test Email";                                                // Subject for the email
        $text = 'This is a text message.';                                      // Text version of the email
        $html = '<html><body><p>This is a html message</p></body></html>';      // HTML version of the email
        $crlf = "n";
        $headers = array(
                        'From'          => $sender,
                        'Return-Path'   => $sender,
                        'Subject'       => $subject
                        );
 
        // Creating the Mime message
        $mime = new Mail_mime($crlf);
 
        // Setting the body of the email
        $mime->setTXTBody($text);
        $mime->setHTMLBody($html);
 
        // Add an attachment
        $file = "Hello World!";
        $file_name = "Hello text.txt";
        $content_type = "text/plain";
        $mime->addAttachment ($file, $content_type, $file_name, 0);
 
        // Set body and headers ready for base mail class
        $body = $mime->get();
        $headers = $mime->headers($headers);
 
        // SMTP authentication params
        $smtp_params["host"]     = "smtp.gmail.com";
        $smtp_params["port"]     = "25";
        $smtp_params["auth"]     = true;
        $smtp_params["username"] = "user@gmail.com";
        $smtp_params["password"] = "pass";
 
        // Sending the email using smtp
        $mail =& Mail::factory("smtp", $smtp_params);
        $result = $mail->send($recipient, $headers, $body);
        if($result === 1)
        {
          echo("Your message has been sent!");
        }
        else
        {
          echo("Your message was not sent: " . $result);
        }
?>
[!--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
  • c#使用netmail方式发送邮件示例

    这篇文章主要介绍了c#使用netmail方式发送邮件的示例,大家参考使用吧...2020-06-25
  • PHP测试成功的邮件发送案例

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

    PHPMailer在SAE上无法发送邮件怎么回事呢,我们以前在php5.2.7版本中使用了PHPMailer是可以发,但移到sae中发现无法发邮件了,那么此问题如何解决 在SAE上直接用5.2.7...2016-11-25
  • 整理几个android后台发送邮件的方法

    本文我们整理了三个android后台发送邮件的方法及示例,第一个是不借助Intent在android后台发送Email,第二个是用在收集应用的异常信息,第三个是分享一个android后台发送邮...2016-09-20
  • Perl中使用MIME::Lite发送邮件实例

    这篇文章主要介绍了Perl中使用MIME::Lite发送邮件实例,本文介绍了使用sendmail方式发送、发送HTML格式邮件、smtp方式发送邮件等内容,需要的朋友可以参考下...2020-06-29
  • 网上找到的两个PHP发送邮件的例子,很不错,贴出来给初学者参考吧(不知道是否有兄弟曾贴过),呵呵(2

    Advanced Example Here we will show the full capabilities of the PHP mail function. PHP Code: <?php echo "<html><body>"; $recipient = "Kris Arndt <karn@nu...2016-11-25
  • PHP利用Jmail组件实现发送邮件

    学过asp的朋友可能知道jmail组件是使用在asp中一个常用的邮箱发送功能,在php中如果想调用jmail功能我们需要使用com组件来操作。 我们先来介绍格式 代码如...2016-11-25
  • phpMailer 发送邮件

    //原创:www.111cn.net 注明:转载说明来处www.111cn.net // 昨天听一网友说用php 里面的mail发邮件发不出去,我想一般都是发不了的,现在大多数据邮件提供商都不准那样了...2016-11-25
  • C#编程实现发送邮件的方法(可添加附件)

    这篇文章主要介绍了C#编程实现发送邮件的方法,具备添加附件的功能,涉及C#文件传输及邮件发送的相关技巧,具有一定参考借鉴价值,需要的朋友可以参考下...2020-06-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