Jump to content

From Header in small email script


dlcmpls

Recommended Posts

Hello all.

 

I have no hair left!

 

I'm working with a very simple email script, but I'm having trouble getting the script to work with a "from" email address supplied via a form (or even hard coded).

 

Here's the code that I know works (with no references to the external form)

 

<?

$to = "bill@bob.com";

$subject = "Check it out!";

$message = "test message";

$from = "someone@somewhere.com";

$headers = "From: $from";

mail($to,$subject,$message,$headers);

?>

 

Works a-ok.

 

But, now I want the "From" email address to be the email supplied by the user of the form.  So I have an input field called "Email" in my form.  If I try to modify the above code like so:

 

<?

    $to = "bill@bob.com";

$subject = "Check it out!";

$message = "test message";

$from = "someone@somewhere.com";

$headers = "From: ($_POST)";

mail($to,$subject,$message,$headers);

?>

No email gets sent.

 

In fact, I can't even set a hard code my own variable in the above code like so:

 

<?php

$myemail="myemail@someplace.com";

 

$to = "bill@bob.com";

$subject = "Check it out!";

$message = "test message";

$from = "someone@somewhere.com";

$headers = "From: $myemail";

mail($to,$subject,$message,$headers);

?>

 

That doesn't work.  No email gets sent which confuses me because I'm simply replacing one variable ($from) with another ($myemail) in the $headers line.

 

What am I missing? 

 

Any help would be greatly appreciated.

 

dlc

Link to comment
Share on other sites

Thanks phreeek.  but that didn't work.  I tried this code:

 

	<?php
	$myemail="jane@jane.com";

	$to = "bill@bob.com";
	$subject = "Check it out!";
	$message = "test message";
	$from = "someone@somewhere.com";
	$headers = "From: " . "$myemail";
	mail($to,$subject,$message,$headers);
	?>

 

And no luck.

 

One thing I don't understand at all...why would replacing one variable ($from) with another ($myemail) cause a problem to begin with? 

Link to comment
Share on other sites

<?php		
	$to = "bill@bob.com";
	$subject = "Check it out!";
	$message = "test message";
	$from = $_POST['email'];
	$headers = "From: $from";
	mail($to,$subject,$message,$headers);
?>

 

what is the name of the input field for the email?

if it is not email, then replace $_POST['email'] with $_POST['NameHere']

obviously putting the name of the input field where it says

NameHere

Link to comment
Share on other sites

Yep, I understand that Flame.

 

Let's drop the idea of a form for now.  Why doesn't this script work:

 

<?php
                $myemail="jane@jane.com";
                
                $to = "bill@bob.com";
                $subject = "Check it out!";
                $message = "test message";
                $from = "someone@somewhere.com";
                $headers = "From: " . "$myemail";
                mail($to,$subject,$message,$headers);
                ?>

Link to comment
Share on other sites

Yes, php is enabled.  To recap, this code works fine:

 

	<?php
	$to = "bill@bob.com";
	$subject = "Check it out!";
	$message = "test message";
	$from = "someone@somewhere.com";
	$headers = "From: $from";
	mail($to,$subject,$message,$headers);		
	?>

 

and an email gets sent.

 

But with this code, no email is sent:

 

	<?php
	$myemail="jane@jane.com";

	$to = "bill@bob.com";
	$subject = "Check it out!";
	$message = "test message";
	$from = "someone@somewhere.com";
	$headers = "From: $myemail";
	mail($to,$subject,$message,$headers);		
	?>

 

I don't understand why simply changing the name of the variable in $headers line would disable the script.

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.