0

为什么下面的代码不向我发送邮件?错误是什么?

<?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 服务器上运行的。

4

5 回答 5

0

试试这个

$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 代码中。

于 2012-07-31T05:35:50.860 回答
0

您可能必须使用许多邮件服务器所要求的 SMTP 身份验证来发送邮件。检查此链接以获取更多详细信息。

于 2012-07-31T05:41:27.360 回答
0

可能是你的php配置不完整,见C://xampp/php/php.ini中:

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

这是用于激活电子邮件。可能你的设置是:

;sendmail_path = "\"C:\xampp\sendmail\sendmail.exe\" -t"
于 2012-07-31T05:50:09.933 回答
0

我建议你使用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>");
}
于 2012-07-31T05:51:20.937 回答
-1

尽管我没有更改您的代码中的任何内容。请试试这个

<?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>';

} ?>
于 2012-07-31T05:50:41.730 回答