Solved

PHP Mail - Not Working

Posted on 2011-02-28
23
507 Views
Last Modified: 2012-05-11
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

0
Comment
Question by:VAN_AL
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 10
  • 5
  • 3
  • +3
23 Comments
 
LVL 19

Expert Comment

by:billmercer
ID: 35002234
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
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 35002250
Or there may not be an active mailserver on that machine.  Is it Windows or Linux?
0
 
LVL 1

Author Comment

by:VAN_AL
ID: 35002319
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
Don't Cry: How Liquid Web is Ensuring Security

WannaCry is just the start. Read how Liquid Web is protecting itself and its customers against new threats.

 
LVL 1

Author Comment

by:VAN_AL
ID: 35002342
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
 
LVL 83

Accepted Solution

by:
Dave Baldwin earned 125 total points
ID: 35002393
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
 
LVL 1

Author Comment

by:VAN_AL
ID: 35002500
No he says the he usually uses auth_smtp through a regular mail server. I don't know what that means?
0
 
LVL 1

Author Comment

by:VAN_AL
ID: 35002512
What feature would be disabled would it show up on the php info display and what would that be?
0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 35002671
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
 
LVL 1

Author Comment

by:VAN_AL
ID: 35002748
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
 
LVL 19

Assisted Solution

by:billmercer
billmercer earned 250 total points
ID: 35002909
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
 
LVL 110

Assisted Solution

by:Ray Paseur
Ray Paseur earned 125 total points
ID: 35003215
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
 
LVL 1

Author Comment

by:VAN_AL
ID: 35003259
@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
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 35003276
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
 
LVL 1

Author Comment

by:VAN_AL
ID: 35003312
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
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 35003324
May need to restart the server - some of these things are cached.
0
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 35003326
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
 
LVL 1

Author Comment

by:VAN_AL
ID: 35003389
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
 
LVL 39

Expert Comment

by:Aaron Tomosky
ID: 35003959
Mail requires that sendmail is installed on the server. Check out phpmailer for a super easy solution that uses any smtp server.  
0
 
LVL 7

Expert Comment

by:Vimal DM
ID: 35004294
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
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 35006231
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
 
LVL 1

Author Comment

by:VAN_AL
ID: 35012863
Thanks will give a try.
0
 
LVL 19

Assisted Solution

by:billmercer
billmercer earned 250 total points
ID: 35021484
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
 
LVL 1

Author Comment

by:VAN_AL
ID: 35041476
Thanks for the help all, problem solved.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

691 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question