2016-09-16 45 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

感謝兄弟它真的幫助我很多.. –

相關問題