编程笔记2:PHPMail

在学习Luke Welling与L.Thomson的 《PHP and MySQL Web Development》的第四章时需要创建一个可以发送Email的HTML:

D:\PHPProj\PHPWeb\Class1\1.2\processfeedback.php

<?php 
    $name=addslashes(trim($_POST['name']));
    $email=addslashes(trim($_POST['email']));
    $feed=addslashes(trim($_POST['feedback']));

    $toaddress="532968708@qq.com";
    $subject="Feedback from web site";
    $mailcontent="Customer name :".$name."\n".
                "Customer email :".$email."\n".
                "Customer comments:\n".$feedback."\n";
    $fromaddress="From:myphp_test@sina.com";
    $issend=mail($toaddress,$subject,$mailcontent,$fromaddress);
    if($issend)
{
  echo '<br /> sucessful <br />';
}else
{
   echo '<br /> failed <br />';
}
?>
<!DOCTYPE HTML>
<html lang="en-US">
<head>
    <meta charset="UTF-8">
    <title>Bob's A P -Feedback Submitted</title>
</head>
<body>
    <h1>Feedback submitted</h1>
    <p>U feedback has been sent.</p>
</body>
</html>

使用mail函数的时候出现了一系列的错误,发现因为没有下载Sendmail所以导致不能使用mail()函数,同时我们要用到smtp服务,还需要class.smtp.php的支持:sendmail下载地址:http://glob.com.au/ ,class.smtp.php下载地址:http://www.phpclasses.org/browse/file/920.html
1.D:\PHPProj\php-5.6.10x64\sendmail\sendmail.ini的配置:

smtp_server=smtp.sina.com
smtp_port=25
smtp_ssl=SSL

pop3_server=pop.sina.com
pop3_username=myphp_test@sina.com
pop3_password=PW


force_sender=myphp_test@sina.com

2.PHP.ini的配制:

SMTP = smtp.sina.com
; http://php.net/smtp-port
smtp_port = 25

; For Win32 only.
; http://php.net/sendmail-from
sendmail_from = myphp_test@sina.com
sendmail_path = "D:/PHPProj/php-5.6.10x64/sendmail/"

重启httpd,出现mail发送出去但是接收方无法得到的问题,还需要解决:我们又下载了phpmailer类http://sourceforge.net/projects/phpmailer/files/phpmailer-ml/PHPMailer-ML_v1.7.1/尝试通过sock发送邮件,来判断问题是否出在程序本身。使用以下的程序来检验

?php     
/** 
* Simple example script using PHPMailer with exceptions enabled 
* @package phpmailer 
* @version $Id$ 
*/    

//require '_lib/class.phpmailer.php';  
define('ROOT' , pathinfo(__FILE__, PATHINFO_DIRNAME));
require_once(ROOT . '/_lib/class.phpmailer.php');   
include("./_lib/class.smtp.php");    
try {     
    $mail = new PHPMailer(true); //New instance, with exceptions enabled     
    date_default_timezone_set("Asia/Shanghai");//设定时区东八区
    $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.sina.com"; // SMTP server     
    $mail->Username    = "myphp_test@sina.com";     // SMTP server username     
    $mail->Password    = "12345555";            // SMTP server password     

    $mail->IsSendmail();  // tell the class to use Sendmail     

    $mail->AddReplyTo("myphp_test@sina.com","sina");     

    $mail->From        = "myphp_test@sina.com";     
    $mail->FromName    = "sina";     

    $to = "myphp_test@sina.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     

    $mail->Send();     
    echo 'Message has been sent.';     
} catch (phpmailerException $e) {     
    echo $e->errorMessage();     
}     
?>    

提示可以发送email,但是另一方没有email,出现问题。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值