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 = "[email protected]";

$subject = "Check it out!";

$message = "test message";

$from = "[email protected]";

$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 = "[email protected]";

$subject = "Check it out!";

$message = "test message";

$from = "[email protected]";

$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="[email protected]";

 

$to = "[email protected]";

$subject = "Check it out!";

$message = "test message";

$from = "[email protected]";

$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
https://forums.phpfreaks.com/topic/82142-from-header-in-small-email-script/
Share on other sites

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

 

PhREEEk

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

 

	<?php
	$myemail="[email protected]";

	$to = "[email protected]";
	$subject = "Check it out!";
	$message = "test message";
	$from = "[email protected]";
	$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? 

<?php		
	$to = "[email protected]";
	$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

Yep, I understand that Flame.

 

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

 

<?php
                $myemail="[email protected]";
                
                $to = "[email protected]";
                $subject = "Check it out!";
                $message = "test message";
                $from = "[email protected]";
                $headers = "From: " . "$myemail";
                mail($to,$subject,$message,$headers);
                ?>

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

 

	<?php
	$to = "[email protected]";
	$subject = "Check it out!";
	$message = "test message";
	$from = "[email protected]";
	$headers = "From: $from";
	mail($to,$subject,$message,$headers);		
	?>

 

and an email gets sent.

 

But with this code, no email is sent:

 

	<?php
	$myemail="[email protected]";

	$to = "[email protected]";
	$subject = "Check it out!";
	$message = "test message";
	$from = "[email protected]";
	$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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.