0

Trying to send mail from localhost using mail(), no mail is received. I am using XAMPP. It's showing that the mail is send but i cannot see any mail. I am new to 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>

I can see result on the output but no mail. Configured few of the things under C:\xampp\sendmail\sendmail.ini and C:\xampp\php\php.ini

These are the changes done.

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"
Al Foиce ѫ
  • 4,195
  • 12
  • 39
  • 49

1 Answers1

1

Use PHPMailer. That is pretty easy and almost all time working and mail sent by it will not be categorized under Spam or Promotions in Gmail or Yahoo etc.

PHPMailer is just SMTP class which sends mail by your mail server which is live on internet. This will give you record of emails sent by your application.

In localhost, 90% of people having same issues like you're having. Some config change is vary between person to person so luckily some people having this working exactly same as you posted the code scenario here. So better to find a better alternative for our situation.

The actual Answer for your concern: Why mail is not sent by changing SMTP config in php.ini file? When you put smtp_server = 127.0.0.1, the service will only send mail on the hosted domain.

Let me be more clear about this, you have to put your computer in the same network/domain to use that facility from localhost. SMTP in every system (Windows/Linux/Mac) is designed to send mails if mail address sent-from belong to the domain reflected in host file or hosted zone. Because it is Protocol not application so we cannot escape it's necessary signatures or check-passes.

Example: If your PC is a node under the domain example.com and you set all config to localhost no problem sending mail from localhost because you are explicitly authorized to send mail as the domain example.com.

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

Sending mail is the procedure requires internet so your computer has to be under that domain on which's behalf you wanna send mail from local. After done that you will no longet require any SMTP.exe from 3rd party also. Only PHP will work for you great.