簡體   English   中英

為什么mail()不發送電子郵件?

[英]Why mail() not sending email?

為什么下面的代碼不向我發送郵件? 有什么錯誤?

<?php
if(isset($_POST['name'])){
    $msg="Name: ".$_POST['name']."\n Email: ".$_POST['email']."\n Address: ".$_POST['city']."\n Phone: ".$_POST['phone'];
    mail('sganake@gmail.com', 'New Trial Request', $msg);
    echo '<h2 align="center" style="color:green">Thank you for your message.</h2>';

} ?>

我沒有錯。 只是我還沒有在收件箱中收到我的電子郵件。 它正在IIS服務器上運行。

嘗試這個

$headers = 'From: from@address.com' . "\r\n";
$validate = mail('sganake@gmail.com', 'New Trial Request', $msg, $headers);

if($validate)
{
  echo '<h2 align="center" style="color:green">Thank you for your message.</h2>';
}
else
{
  echo '<h2 align="center" style="color:red">Something went wrong.</h2>';
}

如果出現'Something went wrong' ,則說明問題出在您的郵件服務器上,而不是PHP代碼中。

您可能必須使用許多郵件服務器要求的使用SMTP身份驗證發送郵件。 檢查此鏈接以獲取更多詳細信息。

可能是您的php配置不完整,請參見C://xampp/php/php.ini中的內容:

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

這用於激活電子郵件。 可能是您的設置是:

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

我建議您使用PEAR :: Mail軟件包。 然后,您可以通過SMTP發送電子郵件。

require_once "Mail.php";

$from = "your@gmail.com";
$to = "sganake@gmail.com";
$subject = "New Trial Request";
$body = "Name $name, Address $address ...";
$host = "ssl://smtp.gmail.com";
$port = 465;
$username = "your@gmail.com";
$password = "password";

$headers = array(
    'From' => $from,
    'To' => $to,
    'Subject' => $subject
);
$smtp = Mail::factory(
    'smtp',
    array(
        'host' => $host,
        'port' => $port,
        'auth' => true,
        'username' => $username,
        'password' => $password
    )
);

$mail = $smtp->send($to, $headers, $body);

if (PEAR::isError($mail)) {
    echo("<p>" . $mail->getMessage() . "</p>");
} else {
    echo("<p>Message has been sent!</p>");
}

盡管我沒有更改您的代碼中的任何內容。 請嘗試這個

<?php
if(isset($_POST['name'])){

    $to = 'sganake@gmail.com';
    $subject = 'the subject';
    $message = 'hello';

    $msg='Name:'.$_POST{name}. "\r\n".
         'Email: '.$_POST{email}. "\r\n".
        'Address: '.$_POST{city}. "\r\n".
         'Phone: '.$_POST{phone}. "\r\n";

    $headers = 'From: webmaster@example.com' . "\r\n" .
    'Reply-To: webmaster@example.com' . "\r\n" .
    'X-Mailer: PHP/' . phpversion();

    $sent = mail($to, $subject, $message, $headers);
    var_dump($sent) // just to debug
    echo '<h2 align="center" style="color:green">Thank you for your message.</h2>';

} ?>

暫無
暫無

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

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