• 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中“==”与equals的区别以及equals方法的重写
    Java中的Switch....case语句:
    Java中的基本数据类型
    HTTP与HTTPS的区别
    深入理解HTTP协议、HTTP协议原理分析
    IntelliJ IDEA下的使用git
    Spring RestTemplate详解
    Java 适配器(Adapter)模式
    LINUX的ssh免密码配置
    CDH6.2安装配置第一篇:CDH配置本地http服务
  • 原文地址:https://www.cnblogs.com/owenyang/p/3579125.html
  • Copyright © 2020-2023  润新知