2011-10-20 75 views
0

我已经做了一个HTML电子邮件,一切工作到目前为止。我打算用PHP发送它(使用mail();函数)。但是,当我这样做时,邮件没有到达hotmail和Gmail帐户。我搜索了一下,有人建议使用PHPmailer。有人可以帮我用phpmailer吗?

我下载了PHPmailer并将其安装在我的服务器上。到现在为止还挺好。但现在我有以下代码:

<?php 
set_include_path('.:c:\domains\mydomain\wwwroot\phpmailer\phpmailer.inc.php'); 
set_include_path('.:c:\domains\mydomain\wwwroot\phpmailer\smtp.inc.php'); 

require("phpmailer.inc.php"); 


$mail = new PHPMailer(); 

$mail->IsHTML(true); 
$mail->From  = "[email protected]"; 
$mail->AddAddress("[email protected]"); 

$mail->Subject = "An HTML Message"; 
$mail->Body  = "Hello, <b>my friend</b>! \n\n This message uses HTML entities!"; 

if($mail->Send()) { 
    echo 'Message is sent'; 

} else { 
    echo 'Message was not sent..'; 
echo 'Mailer error: ' . $mail->ErrorInfo; 
} 
?> 

我有几个问题:

  • 输出告诉我的邮件没有发送,但它是。
  • 我得到两个主题
  • 如果我添加更多的HTML(如表),它仍然被拒绝hotmail(也可能是gmail)。

此外,我看到有一个SMTP功能。如何使用这个功能?我是否需要写下我自己的SMTP?

如果有人能帮助我,会很开心!

编辑:

class SMTP { 
    var $SMTP_PORT = 25; # the default SMTP PORT 
    var $CRLF = "\r\n"; # CRLF pair 

    var $smtp_conn;  # the socket to the server 
    var $error;   # error if any on the last call 
    var $helo_rply;  # the reply the server sent to us for HELO 

    var $do_debug;  # the level of debug to perform 

    /* 
    * SMTP() 
    * 
    * Initialize the class so that the data is in a known state. 
    */ 
    function SMTP() { 
     $this->smtp_conn = 0; 
     $this->error = null; 
     $this->helo_rply = null; 

     $this->do_debug = 0; 
    } 
+0

通过服务器你的意思是'远程server'? 你的set_inclide_path包含本地服务器路径?和PHPMailer将无法在您的本地服务器上工作 –

+0

Nah,服务器我的意思是我的网站。对不起 – Frank

回答

0

我在不同的项目中使用PHPMailer的。

我建议你通过SMTP发送的电子邮件,当然PHPMailer的支持它:

$mail = new PHPMailer(); 
$mail->IsSMTP();  // send via SMTP 
$mail->Host = "smtp.gmail.com" //(or the smtp server you use) 
$mail->Port = "465" //(gmail uses secure smtp so that runs on port 465) 
$mail->SMTPAuth = "true" //we need to autenticate to the server 
$mail->SMTPSecure = "ssl" //we use ssl to protected the flow of info 
$mail->Username = "[email protected]" //account 
$mail->Password = "password" //password 


//build the message 
$mail->IsHTML(true); 
$mail->From  = "[email protected]"; 
$mail->AddAddress("[email protected]"); //who receives the mail 
$mail->Subject = "An HTML Message"; 
$mail->Body  = "Hello, <b>my friend</b>! \n\n This message uses HTML entities!"; 

if($mail->Send()) { 
    echo 'Message is sent'; 

} 
else { 
    echo 'Message was not sent..'; 
echo 'Mailer error: ' . $mail->ErrorInfo; 
} 
+0

非常感谢。 1个问题:如果我使用SMTP,我只能使用现有的电子邮件地址?用邮件();你也可以使用不存在的电子邮件地址.... – Frank

+0

当然。但是,即使您在配置发件人地址时使用SMTP,也可以指定与用于通过smtp进行连接的地址不同的地址:$ mail-> From =“[email protected]”; –

+0

达姆,我无法让它工作。我得到这个错误:致命错误:无法在C:\路径\到\ my \ domain \ smtp.inc.php在第26行中重新声明类SMTP。请参阅我编辑代码 – Frank