问题
I have recently been learning HTML, js, CSS, and PHP through creating web pages and posting them on my apache2 server hosted on my raspberry pi with a no-ip.com address. My first real project for learning PHP has been creating a simple form to then send an email to me, but I keep running into the issue of the mail() function executing without any errors, but not actually sending emails.
Here is the form:
<form class="" action="/FormTesting/sendMail.php" method="post">
Name:<br><input type="text" name="name" value="" required><br>
Email:<br><input type="text" name="email" value="" required><br>
Message:<br><textarea name="message" rows="8" cols="80" required></textarea><br>
Subject:
<input type="radio" name="subject" value="Hello!">Hello!
<input type="radio" name="subject" value="Whats up">Whats Up!<br>
<input type="submit" name="submit" value="Submit">
</form>
Here is the PHP file reference in the form's 'action' attribute:
<?php
if($_SERVER["REQUEST_METHOD"] == "POST") {
$to = "myExampleEmail@gmail.com";
$name = $_POST["name"];
$email = $_POST["email"];
$message = $_POST["message"];
$subject = $_POST["subject"];
echo $name;
echo $email;
echo $message;
echo $subject;
$headers = array("From: " . $email,
"Reply-To: " . $to,
"X-Mailer: PHP/" . PHP_VERSION);
mail($to, $subject, $message, $header);
} else {
echo $_SERVER["REQUEST_METHOD"];
}
?>
I understand that this question has been asked before, and I have tried most of the methods set out by other answers, but I still can't seem to get the PHP mail() function to work. The best information I have is that I have to do some kind of install or formatting on the server itself to allow for emails to be sent, but I have yet to find it well documented and/or explained. (i.e phpMailer? - but still... how?)
EDIT:
I checked the return value of the mail function . . .
if(mail("myExampleEmail@gmail.com", "subject", "message", $headers)) {
echo "TRUE";
}else {
echo "FALSE";
}
. . . and it turns out to be returning false. I assume that means it is an error in my code, then?
EDIT:
Apparently I wasn't clear when I said I did research before posting this question. I tried everything that was given as an answer to the question that mine was a closed as a "duplicate" of, hence me asking, the question anyway.
回答1:
I tried the same script and got the email. But, it went to spam folder. Header should have more details. You can check the mail function return code to make sure that the function succeeded. Following is the best practice.
$boundary = "==MP_Bound_xyccr948x==";
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: multipart/alternative; boundary=\"$boundary\"\r\n";
$headers .= "From: OrganizationName <webmaster@mycompany.com>\r\n";
$headers .= "Return-path: webmaster@mycompany.com\r\n";
$message = "This is a Multipart Message in MIME format\n";
$message .= "--$boundary\n";
$message .= "Content-Type: text/plain; charset=\"utf-8\"\n";
$message .= "Content-Transfer-Encoding: 7bit\n\n";
$message .= $user_entered_message . "\n";
$message .= "--$boundary--";
$mailsent = mail($to, $subject, $message, $headers, "-fbounces@mycompany.com");
if ($mailsent)
{
confirmation_msg();
}
else
{
email_error_msg();
}
来源:https://stackoverflow.com/questions/44866673/not-receiving-emails-from-php-mail-function