2016-09-16 52 views
0

尝试使用邮件()从本地主机发送邮件,则不会收到邮件。我正在使用XAMPP。这表明邮件已发送,但我看不到任何邮件。我是PHP新手。无法使用php中的mail()从本地主机发送邮件

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 = '[email protected]'; 
    $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" 
+0

你看过这里吗? http://stackoverflow.com/questions/27273153/php-xampp-mail-function-not-working –

+0

请使用邮件类从发送电子邮件本地主机看到此链接:https://github.com/PHPMailer/PHPMailer –

+0

是我这样做,好像它的Gmail @迈克尔hanslo –

回答

0

使用PHPMailer。这很容易,几乎所有的时间工作和它发送的邮件将不会被归类为垃圾邮件或促销在Gmail或雅虎等

PHPMailer只是SMTP类,它通过您的邮件服务器发送邮件,这是互联网上的生活。这会给你的应用程序发送的电子邮件记录。

在本地主机,90%的有像你遇到同样问题的人。某些配置更改因人而异,所以幸运的是,有些人的工作原理与您在此处发布代码场景完全相同。所以最好为我们的情况找到一个更好的选择。

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

让我这个更清晰,你必须把你的电脑在同一个网络/域使用该设施从本地主机。如果邮件地址sent-from属于反映在主机文件或托管区域中的域,则每个系统(Windows/Linux/Mac)中的SMTP均用于发送邮件。因为它是协议而不是应用程序,所以我们不能逃避它的必要签名或检查通行证。

例子:如果你的电脑是域example.com下一个节点并设置所有配置为localhost没有问题从本地主机发送邮件,因为你明确授权发送邮件的域example.com。

sendmail_from = [email protected] 
smtp_port = 25 or SSL port of your SMTP server (465 mostly) 
SMTP = localhost 

发送邮件的程序是需要互联网这样你的计算机必须是域上是代表你想从本地发送邮件下。 完成之后,您也不需要第三方的任何SMTP.exe。只有PHP会对你有用。

+0

感谢兄弟它真的帮助我很多.. –

相关问题