PHP mail() function has a problem with sending a message


#1

Hi, I’m new so please forgive me if I have done anything that is not accepted. My form is giving me the custom error message when I try to send a message, but from what I can see all code is correct. It seems the specific error is the PHP mail() function has a problem with sending a message. I have another form elsewhere on my website and also conducted a test on my server and it works fine. It’s just this form. I apologize if it’s a stupid error or oversight, but I’ve been at it for days now and can’t seem to fix the problem.

The problem is with the first file- Contact form PHP. I have just included the others for reference.

Contact form PHP
[php][code]<?php

// Get email address
require_once ‘config.php’;

// Ensures no one loads page and does simple spam check
if(isset($_POST[‘name’]) && empty($_POST[‘spam-check’])) {

// Declare our $errors variable we will be using later to store any errors
$error = '';

// Setup our basic variables
$input_name = strip_tags($_POST['name']);
$input_email = strip_tags($_POST['email']);
$input_subject = strip_tags($_POST['subject']);
$input_message = strip_tags($_POST['message']);

// We'll check and see if any of the required fields are empty
if(strlen($input_name) < 2) $error['name'] = 'Please enter your name.';
if(strlen($input_message) < 5) $error['message'] = 'Please leave a message.';

// Make sure the email is valid
if(!filter_var($input_email, FILTER_VALIDATE_EMAIL) ) $error['email'] = 'Please enter a valid email address.';

// Set a subject & check if custom subject exist
$subject = "Message from $input_name";
if($input_subject ) $subject .= ": $input_subject";

$message = "$input_message\n";
$message .= "\n---\nThis email was sent by contact form.";

// Now check to see if there are any errors 
if(!$error) {

	// No errors, send mail using conditional to ensure it was sent
	if(mail($your_email_address, $subject, $message, "From: $input_email")) {
		echo '<p class="success">Your email has been sent!</p>';
	} else {
		echo '<p class="error">There was a problem sending your email!</p>';
	}
	
} else {
	
	// Errors were found, output all errors to the user
	$response = (isset($error['name'])) ? $error['name'] . "<br /> \n" : null;
	$response .= (isset($error['email'])) ? $error['email'] . "<br /> \n" : null;
	$response .= (isset($error['message'])) ? $error['message'] . "<br /> \n" : null;

	echo "<p class='warning'>$response</p>";
	
}

} else {

die('Direct access to this page is not allowed.');

}
?>[/code][/php]

The PHP from the Config file noted in above form
[php][code]<?php

/* Contact Form
---------------------------------------------------------------------- */

// Enter your email address
$your_email_address = ‘williamsalexandria@gmail.com’;

/* Instagram Feed
---------------------------------------------------------------------- */

// Twitter account name
$user_id = ‘mvrkmvjor’;

// Twitter cache time (default is 1 hour ( 60 minutes ) )
$instagram_cachetime = 60;
?>[/code][/php]

HTML of form

[code]



Do not fill out this field:
[/code]

#2

To clarify, it is returning this, “There was a problem sending your email!”?

From the Docs:

Returns TRUE if the mail was successfully accepted for delivery, FALSE otherwise.

Is the mail server set up where you are trying to send it from?

Better alternatives are libraries like SwiftMail and PHPMailer; or services like MailGun.


#3

Sure, don’t use your Php mail() function anymore. My suggest : Google ( Gmail - it’s so easy ), Mailgun … Use these mail service will help delivery your email to inbox instead of Junk box ;D