Jump to content

mail() not going through


elmas156

Recommended Posts

Can anyone see why this might not be working?  I've stared at it for so long, it looks like something from the matrix now!

 

Here's the code:

<?php

if (isset($_POST['submit'])) { // If the form has been submitted.

	$fname = $_POST['fname'];
	$lname = $_POST['lname'];
	$email = $_POST['email'];
	$position = $_POST['position'];
	$district = $_POST['district'];
	$message = $_POST['message'];

		$sendto = "contact@mysite.com"; //this is actually supposed to go to my REAL email address...
		$emailsubject = "New Contact";		
		$emailmessage = "<html>
		<body>

		<table width='400' border='1' cellspacing='0' cellpadding='0'>
			<tr>
				<td width='150' align='left' valign='top'>
					Name:
				</td>
				<td width='250' align='right' valign='top'>
					$fname $lname
				</td>
			</tr>
			<tr>
				<td width='150' align='left' valign='top'>
					Email:
				</td>
				<td width='250' align='right' valign='top'>
					$email
				</td>
			</tr>
			<tr>
				<td width='150' align='left' valign='top'>
					Position/Title:
				</td>
				<td width='250' align='right' valign='top'>
					$position
				</td>
			</tr>
			<tr>
				<td width='150' align='left' valign='top'>
					District:
				</td>
				<td width='250' align='right' valign='top'>
					$district
				</td>
			</tr>
			<tr>
				<td width='150' align='left' valign='top'>
					Message:
				</td>
				<td width='250' align='right' valign='top'>
					 
				</td>
			</tr>
		</table>
		<table width='400' border='0' cellspacing='0' cellpadding='0'>
			<tr>
				<td width='400' align='left' valign='top'>
					$message
				</td>
			</tr>
		</table>

		</body>
		</html>";

		// To send HTML mail, the Content-type header must be set
		$headers  = 'MIME-Version: 1.0' . "\r\n";
		$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

		// Additional headers
		$headers .= 'From: $fname $lname' . "\r\n";

		// Tells the mail server who the email is from				
		$fromaddress = '-f $email';

		// Mail it
		mail($sendto, $emailsubject, $emailmessage, $headers, $fromaddress);

}

?>

 

Form variables are being passed fine, it's the actual mail() portion that is giving me problems.  Thanks for any help.

Link to comment
Share on other sites

The "From:" email header should contain an email address and you should be using double quotes, not single quotes when the strings contain variables.

<?php
$headers .= "From: $fname $lname <$email>\r\n";
$fromaddress = "-f $email";
?>

 

Ken

Link to comment
Share on other sites

OK, I've tried everything and I still can't get this to work...

 

Here's my new code:

 

<?php

if (isset($_POST['submit'])) { // If the form has been submitted.

	$fname = $_POST['fname'];
	$lname = $_POST['lname'];
	$email = $_POST['email'];
	$position = $_POST['position'];
	$district = $_POST['district'];
	$message = $_POST['message'];

		$sendto = "contact@mysite.com";
		$emailsubject = "New Contact";		
		$emailmessage = "<html>
		<body>

		<table width='400' border='1' cellspacing='0' cellpadding='0'>
			<tr>
				<td width='150' align='left' valign='top'>
					Name:
				</td>
				<td width='250' align='right' valign='top'>
					$fname $lname
				</td>
			</tr>
			<tr>
				<td width='150' align='left' valign='top'>
					Email:
				</td>
				<td width='250' align='right' valign='top'>
					$email
				</td>
			</tr>
			<tr>
				<td width='150' align='left' valign='top'>
					Position/Title:
				</td>
				<td width='250' align='right' valign='top'>
					$position
				</td>
			</tr>
			<tr>
				<td width='150' align='left' valign='top'>
					District:
				</td>
				<td width='250' align='right' valign='top'>
					$district
				</td>
			</tr>
			<tr>
				<td width='150' align='left' valign='top'>
					Message:
				</td>
				<td width='250' align='right' valign='top'>
					 
				</td>
			</tr>
		</table>
		<table width='400' border='0' cellspacing='0' cellpadding='0'>
			<tr>
				<td width='400' align='left' valign='top'>
					$message
				</td>
			</tr>
		</table>

		</body>
		</html>";

		// To send HTML mail, the Content-type header must be set
		$headers  = 'MIME-Version: 1.0' . "\r\n";
		$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

		// Additional headers
		$headers .= "From: $fname $lname <$email>\r\n";

		// Tells the mail server who the email is from				
		$fromaddress = "-f $email";

		// Mail it
		mail($sendto, $emailsubject, $emailmessage, $headers, $fromaddress);

}

?>

 

I even tried removing this line:

$fromaddress = "-f $email";

 

I know that the mail() function is enabled on my server because I've used it with other pages, it just won't work here for some reason.  Any other ideas?

Link to comment
Share on other sites

The From: header should be set to a valid address on your server. The fifth '-f' parameter should also be set to a valid address on your server, and should have no space in it.

That's not necessarily true. On my server that's not the case. It really depends on the server's configuration.

 

Ken 

Link to comment
Share on other sites

That's what I've found with GoDaddy when problems like this crop up. Technically the From: header should be that way anyhow, from my understanding of things, and a Reply-to: header used if that functionality is desired. The fifth parameter isn't always needed, but if required and used, should not have a space in it.

Link to comment
Share on other sites

So using the From: header, would I be able to use a variable ($email) so that it appears as though the email is coming from the user who filled out the form, or should I use a general or catch all email address on my server?

Link to comment
Share on other sites

A) Not many sending mail servers will send an email when the From: address is not a valid mail address hosted at the sending mail server because it does not satisfy the relaying restrictions, assuming that the To: address is also not hosted at the sending mail server (sending it to yourself) as that would satisfy most relaying restrictions in itself.

 

B) Not very many of the major ISP's receiving mail servers will accept an email where the domain in the From: address does not correspond to the sending mail server, either directly as best as can be determined by the dns zone records at the sending mail server or where there is an SPF dns zone record at the domain in the from address that says the sending mail server is authorized to send email for that domain.

 

Are you even sure that the code where your mail() is at is being executed and are there any php errors, with error_reporting set to at lest E_ALL and display_errors set to ON, that would help determine if your sending mail server is even accepting the email to send it?

Link to comment
Share on other sites

Yes, my code is being executed now.  I went back to the basic script that I started with and I am tweaking each section of it to try to get it to do what I want.  When I tweak a section and it stops working, I know that's what was causing my code to fail.  It turns out that the From: header and the fifth parameter was causing the failure when I was trying to use $email within them.  Now I'm just trying to get the emails to display the user's info as the reply to info.

Link to comment
Share on other sites

The 5th parameter is a command line argument that is passed to the mail server when mail() is calling the mail server's binary/executable. If you are actually using SMTP, it doesn't mean anything.

 

If you were using php's error_reporting/display_errors settings, php would probably help you by displaying any errors that the mail() function gets back from your sending mail server.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.