簡體   English   中英

使用GMAIL郵件服務器從PHP運行XAMMP的localhost發送電子郵件

[英]Send email from localhost running XAMMP in PHP using GMAIL mail server

我嘗試使用php mail()函數從localhost發送電子郵件到我的雅虎電子郵件帳戶,返回說我成功發送了電子郵件,但我沒有收到任何電子郵件。 我一直在閱讀並嘗試許多所謂的“簡單方式”來發送電子郵件,但結果令人失望,它們都不適合我。 以下是代碼,配置和錯誤消息。 有人可以用這個啟發我嗎? 謝謝。

PHP代碼

<?php
$to      = 'myemail@yahoo.com';
$subject = 'Fake sendmail test';
$message = 'If we can read this, it means that our fake Sendmail setup works!';
$headers = 'From: myemail@egmail.com' . "\r\n" .
           'Reply-To: myemail@gmail.com' . "\r\n" .
           'X-Mailer: PHP/' . phpversion();

if(mail($to, $subject, $message, $headers)) {
    echo 'Email sent successfully!';
} else {
    die('Failure: Email was not sent!');
}
?>

配置php.ini(我正在使用gmail郵件服務器)

SMTP = smtp.gmail.com
smtp_port = 587
sendmail_from = myemail@gmail.com
sendmail_path =“\\”C:\\ xampp \\ sendmail \\ sendmail.exe \\“ - t”

sendmail.ini的配置

smtp_server = smtp.gmail.com
SMTP_PORT = 587
smtp_ssl = TLS
error_logfile = error.log中
debug_logfile =的debug.log
auth_username=myemail@gmail.com
AUTH_PASSWORD =輸入mypassword
force_sender=myemail@gmail.com

sendmail錯誤日志中的錯誤消息,端口為587

13/10/02 13:36:41:必須先發出STARTTLS命令。 k4sm129639pbd.11 - gsmtp

是給我答案的鏈接:

[安裝]“ 假的sendmail for Windows ”。 如果您不使用XAMPP,可以在此處下載: http//glob.com.au/sendmail/sendmail.zip

 [Modify] the php.ini file to use it (commented out the other lines): [mail function] ; For Win32 only. ; SMTP = smtp.gmail.com ; smtp_port = 25 ; For Win32 only. ; sendmail_from = <e-mail username>@gmail.com ; For Unix only. You may supply arguments as well (default: "sendmail -t -i"). sendmail_path = "C:\\xampp\\sendmail\\sendmail.exe -t" 

(忽略“僅限Unix”位,因為我們實際上使用的是sendmail)

然后,您必須在安裝sendmail的目錄中配置“sendmail.ini”文件:

 [sendmail] smtp_server=smtp.gmail.com smtp_port=25 error_logfile=error.log debug_logfile=debug.log auth_username=<username> auth_password=<password> force_sender=<e-mail username>@gmail.com 

要訪問受雙因素驗證保護的Gmail帳戶,您需要創建特定應用程序的密碼 來源

在php.ini文件中,取消注釋這個

sendmail_path = "\"C:\xampp\sendmail\sendmail.exe\" -t"
;sendmail_path="C:\xampp\mailtodisk\mailtodisk.exe"

並在sendmail.ini中

smtp_server=smtp.gmail.com
smtp_port=465
error_logfile=error.log
debug_logfile=debug.log
auth_username=your@gmail.com
auth_password=yourpassword
force_sender=your@gmail.com
hostname=localhost

配置這個...它會工作......它對我來說很好。

謝謝。

[sendmail]

smtp_server=smtp.gmail.com  
smtp_port=25  
error_logfile=error.log  
debug_logfile=debug.log  
auth_username=myemail@gmail.com 
auth_password=gmailpassword  
force_sender=myemail@gmail.com

需要驗證郵件的用戶名和密碼,然后才能成功從localhost發送郵件

不要忘記為您的Gmail帳戶生成第二個密碼。 您將在代碼中使用此新密碼。 讀這個:

https://support.google.com/accounts/answer/185833

在“如何生成應用密碼”部分下,單擊“應用密碼”,然后在“選擇應用”下選擇“郵件”,選擇您的設備並單擊“生成”。 您的第二個密碼將打印在屏幕上。

最簡單的方法是使用PHPMailer和Gmail SMTP。 配置如下所示。

require 'PHPMailer/PHPMailerAutoload.php';
$mail = new PHPMailer;

$mail->isSMTP();                            
$mail->Host = 'smtp.gmail.com';            
$mail->SMTPAuth = true;                     
$mail->Username = 'Email Address';          
$mail->Password = 'Email Account Password'; 
$mail->SMTPSecure = 'tls';               
$mail->Port = 587;                  

示例腳本和完整源代碼可以在這里找到 - 如何從PHP中的Localhost發送電子郵件

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM