2012-02-02 190 views
0
if(mysql_affected_rows()==1) 
      { 
       $msg="To activate your account, please click on the following link:\n\n 

http://localhost:80/activate.php?email=".urlencode($email)."&key=".$activation; 
       if(mail($email,"Registration Confirmation", $msg, "From: [email protected]\r\nX- 

Mailer: php")) 
       { 
        echo '<div> Thank you for registering. A confirmation email has been sent to '. 

$email.'.Please click on the link to activate your account then</div>'; 
       } 
      } 

我使用的代碼片段從[email protected]發送電子郵件至$電子郵件([email protected]),但我沒有收到任何電子郵件的雅虎帳戶。儘管如此,瀏覽器中顯示的回顯消息表明郵件成功。此外,我試圖直接通過Outlook從[email protected]發送電子郵件到雅虎帳戶,它工作正常。請有人幫我修復該源代碼片段或任何類型的額外設置,以使郵件功能的工作?非常感謝。從本地主機到外部電子郵件帳戶發送電子郵件

編輯:順便說一句,我還設置了我的php.ini的STMP腳本塊如下,

[mail function] 
; For Win32 only. 
; http://php.net/smtp 
SMTP = mail.mycompany.com 
; http://php.net/smtp-port 
smtp_port = 25 

; For Win32 only. 
; http://php.net/sendmail-from 
sendmail_from = [email protected] 

我的問題是設置我的本地計算機的本地郵件服務器,我editted PHP。如上所述,但它仍然不起作用。

[解決]

<?php 
function SendMail($from, $to, $subject, $body) 
{ 

require("class.phpmailer.php"); 

$mail = new PHPMailer(); 

$mail->IsSMTP();          
$mail->Host = "mail.mycompany.com"; 
$mail->SMTPAuth = true;  
$mail->Username = "My name"; // SMTP username 
$mail->Password = "mypassword"; // SMTP password = one used in Outlook 

$mail->From = "[email protected]"; 
$mail->FromName = "Registration Confirmation Email"; 
$mail->AddAddress($to); 


$mail->IsHTML(true);         // set email format to HTML 

$mail->Subject = $subject; 
$mail->Body = $body; 


if(!$mail->Send()) 
{ 
    echo "Message could not be sent. <p>"; 
    echo "Mailer Error: " . $mail->ErrorInfo; 

    return false; 
} 

return true; 
} 
?> 

我包括上面的代碼剪斷證明使用PHPMailer的一個小而工作示例。感謝下面這篇文章中介紹給我的人。

+0

我不知道我可以從本地主機發送郵件! – 2012-02-02 07:59:50

+0

您的代碼是否託管在Windows系統上? – Repox 2012-02-02 08:08:20

+0

您能否提供有關正在發生的事情的更多詳細信息?任何錯誤?任何異常?這對了解您的問題至關重要。 – 2012-02-02 08:09:22

回答

0

你應該看看郵件標題。大部分時間的原因可以是:

  • 跳過回覆/從頭部
  • 跳過X - 梅勒

欲瞭解更多詳情,請參閱here

編輯

好吧,我想我知道問題出在哪裏可能。

LAMP和WAMP有很大不同。 Linux系統傾向於有sendmail庫,它允許你發送郵件到其他系統而不需要配置任何東西。 Windows沒有。

因此,您應該安裝SMTP服務器,以便將郵件發送到外部源。看看這個問題details

此外,如果您有需要測試此功能,則應該查看Papercut,這在測試系統時是不可或缺的。

如果您可以發送純文本mail(),您應該也可以從庫中發送郵件。不是相反的方式。如果您使用已有的SMTP/IMAP服務器來執行此操作,例如Gmail中。

2

看看PHPMailer的這是很好的,非常容易使用,你可以從你現有的電子郵件帳戶發送電子郵件-S(如Gmail)

1

試試這一個,從這個腳本,你也可以從發送電子郵件您的本地文件到外部電子郵件。

require_once('class.phpmailer.php'); 
define('SMTPSERVER', 'mail.yourcompany.com');// sec. smtp server 
define('SMTPUSER', '[email protected]'); // sec. smtp username 
define('SMTPPWD', '123456'); // sec. password 

$useremail = '[email protected]'; 
$msg = 'your text here'; 
$from = '[email protected]'; 
$mailTest=new EmailService(); 
if ($mailTest->generalMailer($useremail, $from, 'Yoursite.com', 'Your company name', $msg)) { 
} else { 
    if (!$mailTest->generalMailer($useremail, $from, 'Yoursite.com', 'Your company name', $msg)) { 
     if (!empty($error)) echo $error; 
    } else { 
     echo 'Yep, the message is send (after hard working)'; 
    } 
} 
header("location:index.php?email_msg=Email sent successfully"); 
0

是否確定您的本地計算機上安裝了MTA(郵件傳輸代理=郵件服務器)?否則,您需要使用ISP的SMTP服務器。

大多數情況下,這將是mail.yourprovider.comsmtp.yourprovider.com(與Outlook中用於發送郵件的主機名相同)。 PHP不能自行發送郵件,它需要一個MTA(本地或遠程)來執行此操作。

+0

我編輯我的OP來清除SMTP服務器(在我訪問我的Outlook中的帳戶屬性後)。 – user1151711 2012-02-02 08:56:55

相關問題