无法使用php中的mail()从localhost发送邮件

时间:2016-09-16 09:25:24

标签: php html email smtp localhost

尝试使用mail()从localhost发送邮件,不会收到任何邮件。我正在使用XAMPP。它显示邮件已发送但我看不到任何邮件。我是PHP的新手。

HTML:

<form name='form' ng-app="" role="form" method="post" action="submit.php">
    <div class="form-group col-sm-12">
        <input type="text" ng-model="name" required name="name" maxlength="20" placeholder="Name" ng-pattern="/^[A-Za-z\s]+$/"/>
    </div>
    <div class="form-group col-sm-12">
        <input type="text" name="email" required ng-model="email" placeholder="Email Address" ng-pattern="/^[_a-z0-9]+(\.[_a-z0-9]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$/"/>
    </div>
    <div class="form-group col-sm-12">
        <textarea type="text" ng-model="message" name="message" placeholder="Message"/></textarea>
    </div>
    <button type="submit" id="submit" value="Send" class="btn btn-primary" ng-disabled="form.$invalid">Send Message</button>
    <div class="form-group col-sm-12 alert-danger">
        <span class="error" ng-show="form.email.$error.pattern">ENTER A VALID EMAIL-ID</span>
        <span class="error" ng-show="form.name.$error.pattern">ENTER A VALID NAME</span>
    </div>
</form>

PHP:

<?php
    $name = $_POST['name'];
    $email = $_POST['email'];
    $message = $_POST['message'];
    $from = 'Demo Contact Form';
    $to = 'myemail@hotmail.com';
    $subject = 'Message from Contact Demo ';

    $body = "From: $name\n E-Mail: $email\n Message:\n $message";
    // If there are no errors, send the email
    if (mail($to, $subject, $body, $from)) {
        $result = '<div class="alert alert-success">Thank You! I will be in touch</div>';
    } else {
        $result = '<div class="alert alert-danger">Sorry there was an error sending your message. Please try again later</div>';
    }
?>
<div class="col-sm-10 col-sm-offset-2">
    <?php echo $result; ?>  
</div>

我可以在输出上看到结果,但没有邮件。 在C:\xampp\sendmail\sendmail.iniC:\xampp\php\php.ini

下配置了一些内容

这些是已完成的更改。

sendmail.ini

smtp_server=localhost

的php.ini

; XAMPP: Comment out this if you want to work with an SMTP Server like Mercury
SMTP = 127.0.0.1
smtp_port = 25

; XAMPP IMPORTANT NOTE (1): If XAMPP is installed in a base directory with spaces (e.g. c:\program filesD:\xampp) fakemail and mailtodisk do not work correctly.
; XAMPP IMPORTANT NOTE (2): In this case please copy the sendmail or mailtodisk folder in your root folder (e.g. C:\sendmail) and use this for sendmail_path.  

; XAMPP: Comment out this if you want to work with fakemail for forwarding to your mailbox (sendmail.exe in the sendmail folder)
sendmail_path = "\"C:\xampp\sendmail\sendmail.exe\" -t"

; XAMPP: Comment out this if you want to work with mailToDisk, It writes all mails in the C:\xampp\mailoutput folder
; sendmail_path = "C:\xampp\mailtodisk\mailtodisk.exe"

1 个答案:

答案 0 :(得分:0)

使用PHPMailer。这很简单,几乎所有的时间都在工作,而且它发送的邮件不会被归类为Gmail或雅虎等垃圾邮件或促销活动。

PHPMailer只是SMTP类,它通过您的邮件服务器发送邮件,该服务器在互联网上运行。这将为您提供应用程序发送的电子邮件记录。

在localhost中,有90%的人遇到过与您相同的问题。一些配置更改因人而异,所以幸运的是,有些人的工作方式与您在此处发布代码方案完全相同。所以最好为我们的情况找到更好的选择。

您关注的实际答案: 为什么不通过更改php.ini文件中的SMTP配置来发送邮件? 当您放置smtp_server = 127.0.0.1时,service will only send mail on the hosted domain

让我更清楚一点,你必须将你的计算机放在同一个网络/域中,才能从localhost使用该工具。如果邮件地址sent-from属于主机文件或托管区域中反映的域,则每个系统(Windows / Linux / Mac)中的SMTP都用于发送邮件。因为它不是协议,所以我们无法逃避它必要的签名或检查通行证。

示例:如果您的PC是域example.com下的节点,并且您将所有配置设置为localhost,则从localhost发送邮件没有问题,因为您明确被授权以域example.com的身份发送邮件。

sendmail_from = info@example.com
smtp_port = 25 or SSL port of your SMTP server (465 mostly)
SMTP = localhost

发送邮件是需要互联网的程序,因此您的计算机必须位于该域名下,而您希望从本地发送邮件。 完成后,您也不会需要来自第三方的任何SMTP.exe。只有PHP才能为您效劳。