简体   繁体   中英

PHP Mail not sending on website using Ras Pi LAMP server

I run my bootstrap website on a Raspberry Pi LAMP server and I'm getting 'Error!' when sending a message using the contact form, the form uses two files using PHP Mail and the contact form is on the home page, here's the code:

Contact form on index.html:

<div class="col-sm-12">
<form class="form-horizontal" action="assets/php/contactForm.php" method="post" role="form" id="contactForm">
                        <div class="form-group">
                            <div class="col-sm-6">
                                <input type="text" name="name" class="form-control" placeholder="Name"> 
                            </div>
                            <div class="col-sm-6">
                                <input type="text" name="contactEmail" class="form-control" placeholder="Email">
                            </div>
                        </div>
                        <div class="form-group">
                            <div class="col-sm-12">
                                <textarea name="message" class="form-control" rows="8" placeholder="Message"></textarea>
                            </div>
                        </div>
                        <div class="form-group">
                            <div class="col-sm-12">
                                <button type="submit" class="btn btn-theme ladda-button" data-style="expand-left">
                                <span class="ladda-label">Submit</span>
                                </button>
                            </div>
                        </div>
                    </form>

contactForm.php

<?php
include("include/settings.php");

if(isset($_POST['name']) && isset($_POST['contactEmail']) && isset($_POST['message'])){
$name = $_POST['name'];
$from = $_POST['contactEmail'];
$message = $_POST['message'];

$subject = "Message from " . $name;

if (mail ($to, $subject, $message, $from)) { 
    $response = array('sent' => 1);
    echo json_encode($response);
} else { 
    $response = array('sent' => 0);
    echo json_encode($response);
} 
}
?>

settings.php

<?php

// Contact
$to = 'xxxxxxx@hotmail.co.uk';
$subject = 'Contact Form from website';

?>

(email address removed and replaced by xxxxxxx just for this post for privacy reasons)

Any idea why i'm getting 'Error!' when using the form?

From the command line on your Pi, can you test the following

mail -s "Test Email" xxxxxxx@hotmail.co.uk < /dev/null

to check the Pi is ready to send out mail. If not, you may need to install some sort of mail server (most guides point to postfix, but there are others). If you do want to install postfix, use the following

sudo apt-get install postfix

Also, in your /etc/php5/apache2/php.ini file, check what the sendmail_path option is set as well.

You haven't shared any specific configuration details, though above code should let the email to be sent.

You must have ensured the email address@hotmail.co.uk exists, so try using any other provider's email address and check. Code seems fine.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM