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-博客
查看全文
  • 相关阅读:
    2016"百度之星"
    codeforces 55 div2 C.Title 模拟
    codeforces 98 div2 C.History 水题
    codeforces 97 div2 C.Replacement 水题
    codeforces 200 div2 C. Rational Resistance 思路题
    bzoj 2226 LCMSum 欧拉函数
    hdu 1163 九余数定理
    51nod 1225 余数的和 数学
    bzoj 2818 gcd 线性欧拉函数
    Codeforces Round #332 (Div. 2)D. Spongebob and Squares 数学
  • 原文地址:https://www.cnblogs.com/owenyang/p/3579125.html
  • Copyright © 2011-2022 走看看