簡體   English   中英

郵件功能不起作用 - PHP

[英]Mail function not working - PHP

我是PHP新手,剛開始學習它。 我正在嘗試使用mail()函數將測試郵件發送到我的Gmail帳戶。

這是我的代碼:

$message = "Hello PHP!";
mail("mygmailaccount@gmail.com", "PHP Test", $message);

但它不起作用。 這就是錯誤的樣子:

點擊此處在網頁中查看。


我見過很多類似的問題,我也嘗試了所有提到的解決方案。
我已經對我的php.ini文件進行了更改,如下所示。

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

; For Win32 only.
; http://php.net/sendmail-from
sendmail_from = mygmailaccount@gmail.com

我在Windows 7(64位)計算機上使用WAMP服務器。
請幫我解決這個問題。 謝謝!

https://github.com/PHPMailer/PHPMailer

這是官方的PHPMailer。

您所要做的就是將所有文件和文件夾上傳到包含郵件php文件的文件夾(包含您顯示的代碼的文件)。 然后看看示例文件夾或查看示例文件夾,如果你需要smtp。你應該已經弄明白了,看起來你有很好的編程技巧。如果你有問題,請評論通知我。

PS。 mail()函數有時不可靠。 我經常面對這個問題,直到我將phpmailer用於我自己的系統。

編輯:完全像這樣。 send.php是我要編寫以下代碼的文件。 包含phpmailer的文件夾

接下來,send.php代碼!!

    <?php
    require 'PHPMailerAutoload.php';
    $sendto ="destinationemail";//Input your own
    $sendfrom ="yourmaskedmail";//input your own
    $topic ="test";
    $passage ="test";
    $mail = new PHPMailer(true);

    //Send mail using gmail
    if($send_using_gmail){
$mail->IsSMTP(); // telling the class to use SMTP
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->SMTPSecure = "ssl"; // sets the prefix to the servier
$mail->Host = "smtp.gmail.com"; // sets GMAIL as the SMTP server
$mail->Port = 465; // set the SMTP port for the GMAIL server
$mail->Username = "*hidden*"; // GMAIL username Input your own
$mail->Password = "*hidden"; // GMAIL password  Input your own
    }

//Typical mail data
$mail->AddAddress($sendto,$sendto);
$mail->SetFrom($sendfrom,$sendfrom);
$mail->Subject =$topic;
$mail->Body =$passage;

try{
$mail->Send();
echo "Success! This email has been sent to ".$sendto. " in the name of ".$sendfrom;
} catch(Exception $e){
//Something went bad
echo "Unable to process your request now, please try again later";
}
?>

更改mailsender,mailreciever和郵件內容。 此外,輸入您的Gmail用戶名和密碼。 之后,運行php,等待你的電子郵件來。 我只是在8分鍾前測試它並且它有效。

看看這個頁面:

http://www.php.net/manual/en/mail.configuration.php

簡短版本:要使您的php.mail功能正常工作,您需要弄清楚如何使用您的服務器發送電子郵件,並確保您在php.ini中的配置是最新的。 (有關詳細信息,請參閱上面的鏈接)。

  • 您的服務器是運行sendmail還是其他郵件代理? 或者根本不運行這樣的東西?
  • 如果是這樣,請確保正確設置路徑。
  • 如果沒有,請使用第三方SMTP服務。 GMail提供它,但問題是它需要身份驗證, php.mail()不支持。 如果您仍想使用它,請參閱此文章php.mail()替代方法。

暫無
暫無

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

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