Jump to content

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.

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.