PHP Mail - Not Working

Hi All,

I have a request form that is processed by PHP and sent to a specific user, the same code i've used on another site and works perfectly but on a different site (and server) it says mail sent by nothing arrives? I don't know why and need help figuring out why.

Code is attached.
<?php
//If the form is submitted
if(isset($_POST['submit'])) {

	//Check to make sure that the name field is not empty
	if(trim($_POST['first_name']) == '') {
		$hasError = true;
	} else {
		$name = trim($_POST['first_name']);
	}

	//Check to make sure that the subject field is not empty
	if(trim($_POST['last_name']) == '') {
		$hasError = true;
	} else {
		$subject = trim($_POST['last_name']);
	}

	//Check to make sure that the subject field is not empty
	if(trim($_POST['phone']) == '') {
		$hasError = true;
	} else {
		$phone = trim($_POST['phone']);
	}


		//Check to make sure sure that a valid email address is submitted
	if(trim($_POST['email']) == '')  {
		$hasError = true;
	} else if (!eregi("^[A-Z0-9._%-]+@[A-Z0-9._%-]+\.[A-Z]{2,4}$", trim($_POST['email']))) {
		$hasError = true;
	} else {
		$email = trim($_POST['email']);
	}

	//Check to make sure comments were entered
	if(trim($_POST['describe']) == '') {
		$hasError = true;
	} else {
		if(function_exists('stripslashes')) {
			$comments = stripslashes(trim($_POST['describe']));
		} else {
			$comments = trim($_POST['describe']);
		}
	}

	//If there is no error, send the email
	if(!isset($hasError)) {
		$emailTo = 'ashala@nextphasestrategy.com'; 
		$body = "First Name: $name \n\nLast Name: $subject  \n\nPhone: $phone \n\nEmail: $email \n\nComments:\n $comments";
		$subject = "Adroit Resources Inc: Registration for Investors Updates";
		$headers = 'From: Adroit Resources Inc<'.$emailTo.'>' . "\r\n" . 'Reply-To: ' . $email;

		mail($emailTo, $subject, $body, $headers);
		$emailSent = true;
	}
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
	<head>
		<title></title>
		<!-- include jQuery library -->
		<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.1/jquery.min.js"></script>
		<script src="../Scripts/jquery.validate.pack.js" type="text/javascript"></script>

		<script type="text/javascript">
			$(document).ready(function(){
			$("#contactform").validate();
			});
		</script>
	</head>
	
	<body>
			<div>
	<p><h1>Register for Investor Updates</h1></p>
	<?php if(isset($hasError)) { //If errors are found ?>
	<p class="error">Please check if you've filled all the fields with valid information. Thank you.</p>
		
		<?php } ?>
			<?php if(isset($emailSent) && $emailSent == true) { //If email is sent ?>
				<p><strong>Email Successfully Sent!</strong></p>
				<p>Thank you <strong><?php echo $name;?></strong> for registering to receive Adroit Resources Inc Investor Updates! Your email was successfully sent and you will be added to our mailing list.</p>
			<?php } ?>

       <p>
       <form method="post" class="cmxform" action="<?php echo $_SERVER['PHP_SELF']; ?>" id="contactform">
		<fieldset>	
			<label class="field-first">First Name &#42;<input type="text" name="first_name" id="first_name" class="required" value="" /></label>
			<label class="field-last">Last Name &#42;<input type="text" name="last_name" id="last_name" class="required" value="" /></label>
			<label class="field-phone">Phone &#42;<input type="text" name="phone" id="phone" class="required" value="" /></label>
			<label class="field-email">Email &#42;<input type="text" name="email" id="email" class="required email" value="" /></label>
		</fieldset>
		<fieldset>	
			<label class="field-describe">Comments<br /><textarea name="describe" id="describe" cols="40" rows="5"></textarea></label>
		</fieldset>
		<fieldset>
			<input type="submit" name="submit" value="Submit" />			
			<input type="hidden" name="submitted" value="TRUE" />
			</fieldset>
		</form>
	   </p>
	</div>
	
	</body>
</html>

Open in new window

LVL 1
VAN_ALAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
Dave BaldwinConnect With a Mentor Fixer of ProblemsCommented:
I think he told you that you have to use authentication to send email on that server and plain mail() doesn't do that.  If you have PHP-Pear-Mail installed on that server, you can use it.  http://www.authsmtp.com/php-pear-mail/index.html
0
 
billmercerCommented:
If the same code works on a different server, the problem is probably in the settings you're using to access the mail server, or the mail server configuration itself. For example your current server might require SMTP authentication. Can you successfully send an email from the PHP server using a mail client?
0
 
Dave BaldwinFixer of ProblemsCommented:
Or there may not be an active mailserver on that machine.  Is it Windows or Linux?
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
VAN_ALAuthor Commented:
It's a w3 server (i don't know what this is exactly) and i am able to send email with the above code from serveral servers... i tried the code below and it works on all servers except this one, it's on a private server for a client talked to the it tech guys says to use auth_smtp?

<?php
$to = "me@0000.com";
$subject = "Test mail";
$message = "Hello! This is a simple email message.";
$from = "someonelse@example.com";
$headers = "From: $from";
$worked = mail($to,$subject,$message,$headers);

if($worked) {
echo "Mail Sent.";
} else {
echo "Mail Not Sent.";
}

?>
0
 
VAN_ALAuthor Commented:
Oh it's a Linux server, sorry i forgot to run phpinfo on it, which setting in phpinfo would indicate that perhaps the admin might have disabled the mail() function (I'm guessing that's the issue here)
0
 
VAN_ALAuthor Commented:
No he says the he usually uses auth_smtp through a regular mail server. I don't know what that means?
0
 
VAN_ALAuthor Commented:
What feature would be disabled would it show up on the php info display and what would that be?
0
 
Ray PaseurCommented:
I think mail is a built-in feature.  You might look in the PHP core of phpinfo() to see.

Try running this script and let's see what happens. Post back here when you have run it
<?php
error_reporting(E_ALL);
mail('Ray.Paseur@Gmail.com', 'EE Q_26853903', 'Testing 123');

Open in new window

0
 
VAN_ALAuthor Commented:
I get a blank page, i tried removing the mail function and run it with error_reporting on but still got a blank page.
0
 
billmercerConnect With a Mentor Commented:
SMTP authentication was the first thing I suggested you look into. The built-in PHP mail function doesn't support it, so you can't use that if the server requires authentication. Basically your choices are to either use a different method to send mail, or change the mail server configuration.
Since it's not your server, using a different method probably makes more sense.

The pear library PHP-Pear-Mail that DaveBaldwin suggested is probably the most popular solution.

0
 
Ray PaseurConnect With a Mentor Commented:
You would be expecting a blank page, right?  The script produces no browser output.  Just to be clear, you have run the script that I posted and it completed without error?  

(If you have not run it yet, please run it and post here when you have run it).

If you have run it and I have no email, the PHP mail function appears to be disabled, which is rarely the case, but you may be stuck with that situation.  In addition to the Pear thing, this is worth a look:
http://phpmailer.worxware.com/index.php
0
 
VAN_ALAuthor Commented:
@Ray_Paseur I ran you script as you posted and came back with no errors (blank page) and i re-ran your script with my email in the mail function and came up with the same result.

Also one stupid question sorry, what username & password (see code below) goes in here if I am trying to create a form for others fill out and have the email sent to specific email addresse?

// Define SMTP Parameters

$params['host'] = 'mail.authsmtp.com';
$params['port'] = '25';
$params['auth'] = 'PLAIN';
$params['username'] = 'USERNAME'; //CHANGE
$params['password'] = 'PASSWORD'; //CHANGE
0
 
Ray PaseurCommented:
Hmm... No email arrived at all.

I think you would want to create an email account on a mail server (GMail is free) and use the username and password associated with that SMTP server.

If you are letting others use your site to send email to some 3rd party address, you will want to learn about the principles of "Accept Only Known Good Values" to avoid becoming an open relay for spam.
0
 
VAN_ALAuthor Commented:
No emails arrived at all, I don't know what's going on?

Admin says he turned on relaying for our ip address and it should all work now but it doesn't.
0
 
Ray PaseurCommented:
May need to restart the server - some of these things are cached.
0
 
Dave BaldwinFixer of ProblemsCommented:
For what it's worth, several hosting companies require you to send from a valid email address on your account or at least on their servers.  Godaddy recently changed to that policy and one of my accounts stopped receiving email from their forms.  I had to create an email address for that account so that the form on the web site could be emailed to the owner from a valid email address.
0
 
VAN_ALAuthor Commented:
I'm dealing wth an overprotective admin and have to wait until he's go and ready to restart it (if he does), what should have been a simple email form turned into a long thread with possible solutions, also I am not 100% sure but it looks like PEAR isn't installed on the server either so the previously suggested solution from Dave wouldn't work either..
0
 
Aaron TomoskySD-WAN SimplifiedCommented:
Mail requires that sendmail is installed on the server. Check out phpmailer for a super easy solution that uses any smtp server.  
0
 
Vimal DMSenior Software EngineerCommented:
Hai,

"PHPMAILER" is the best one to process the mailing events

1) Can send bulk mails
2) Can have bulk attachment
3) Can avoid spams
4) Any type of mail id's will be accepted to the mailer class

0
 
Ray PaseurCommented:
Go back to the link at ID:35003215 above and install that script.  I've done it before and you have to follow the instructions carefully, but it works and it will get you "over the hump."  Best of luck with it, ~Ray
0
 
VAN_ALAuthor Commented:
Thanks will give a try.
0
 
billmercerConnect With a Mentor Commented:
I went back and reviewed all of the OP's comments, and I'm not sure that a server error can be ruled out.

The script Ray suggested you run didn't provide any actual output of its own. The thing is, returning a blank page could happen for one of two reasons, either because no error occurred, or because the server is ignoring the error_reporting(E_ALL), such as if display_errors is off in the ini file.

If the server doesn't return any errors, then that script would give a blank page regardless of whether there's an error or not.

Try adding an echo to the original script, so you can tell if normal output works.
Also try enabling error messages with ini_set. That way you can at least rule out non-fatal errors.


<?php
ini_set('display_errors', TRUE);
error_reporting(E_ALL);
mail('Ray.Paseur@Gmail.com', 'EE Q_26853903', 'Testing 123');
echo 'THIS IS A TEST');



If there's no error on the server side you should see THIS IS A TEST instead of a blank page. If you still get a blank page, then you know something else is wrong, and the error reporting is not being honored by the server.


0
 
VAN_ALAuthor Commented:
Thanks for the help all, problem solved.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.