zoukankan      html  css  js  c++  java
  • PHP发送邮件示例

    一、通过mail()

    此方法,需要配置邮件服务器

    参考代码:

    <?php
     $toaddress = 'test@163.com';
     $subjects = 'just test mail';
     $mailcontent = wordwrap('test miali content!',70);//到了70个字符就换行
     $fromaddress = 'From: 123@163.com';
     mail($toaddress,$subjects,$mailcontent,$fromaddress);
     ?>

    二、开源项目PHPMailer实现邮件发送

    项目地址:http://code.google.com/a/apache-extras.org/p/phpmailer/downloads/list

       里面有示例,参考代码:

    <?php
     /**
     * Simple example script using PHPMailer with exceptions enabled
     * @package phpmailer
     * @version $Id$
     */
     
     require '../class.phpmailer.php';
     
     try {
     	$mail = new PHPMailer(true); //New instance, with exceptions enabled
     
     	$body             = file_get_contents('contents.html');
     	$body             = preg_replace('/\\\\/','', $body); //Strip backslashes
     
     	$mail->IsSMTP();                           // tell the class to use SMTP
     	$mail->SMTPAuth   = true;                  // enable SMTP authentication
     	$mail->Port       = 25;                    // set the SMTP server port
     	$mail->Host       = "smtp.qq.com"; // SMTP server
     	$mail->Username   = "yourname@qq.com";     // SMTP server username
     	$mail->Password   = "password";            // SMTP server password
     
     	//$mail->IsSendmail();  // tell the class to use Sendmail
     
     	$mail->AddReplyTo("name@domain.com","First Last");
     
     	$mail->From       = "yourname@qq.com";
     	$mail->FromName   = "First Last";
     
     	$to = "mail@qq.com";
     
     	$mail->AddAddress($to);
     
     	$mail->Subject  = "First PHPMailer Message";
     
     	$mail->AltBody    = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
     	$mail->WordWrap   = 80; // set word wrap
     
     	$mail->MsgHTML($body);
     
     	$mail->IsHTML(true); // send as HTML
     
     	if(!$mail->Send())
     		echo "错误原因: " . $mail->ErrorInfo;
     	echo 'Message has been sent.';
     } catch (phpmailerException $e) {
     	echo $e->errorMessage();
     }
     ?>


  • 文章声明
  • 作者:Owen
  • 出处: http://www.cnblogs.com/owenyang
  • 本文版权归作者,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。该博客同步发在 HEXO-博客
查看全文
  • 相关阅读:
    Java 中字符串的格式化
    Awk学习笔记
    Spring任务调度实战之Quartz Simple Trigger
    Subclipse vs. Subversive
    使用OpenSSL生成自用证书
    httpclient处理页面跳转
    巧用TableDiff(转)
    DBCC了解页面结构
    取最新一条SQL优化
    SQL 2008 到 SQL2012的镜像
  • 原文地址:https://www.cnblogs.com/owenyang/p/3579125.html
  • Copyright © 2011-2022 走看看