Link to home
Start Free TrialLog in
Avatar of phillystyle123
phillystyle123Flag for United States of America

asked on

How to allow site visitor to email a webpage from my site via a form

I need an option for users to be able to click on an "email this page to a friend" link, fill out a quick form  and send the page as a link embedded in the resulting email (sorry for the long-winded explanation). i'm not terribly great at php so...it's a bit of a challenge for me
Avatar of CDirenzi
CDirenzi
Flag of United States of America image

PHP has a function that sends emails from your script:
mail(address,subject,message,headers);
Make the email to a friend a form that gets the email address and the friend's name, as well as possibly a message that the user can send to his friend.  Build a string for the message and send it via the mail command.  

$message='Hello'.$friendName.', '.$userMessage.'<p> <a href=\"url\">Click here!</a></p>';

$headers="From: member@email.com\r\nbcc: mom@email.com";
the header allows you to set who it is from, plus you can send cc and bcc copies.  the \r\n is how you do a carriage return in a string.

mail($friendEmail, 'Email Subject Here', $message,$headers);

Avatar of phillystyle123

ASKER

ok - so i'm passing all of these variables over to a form - what would the variable for the actual link i'm emailing to a friend look like?
Well the link is in the message string, in the message string:
$message='Hello'.$friendName.', '.$userMessage.'<p> <a href=\"url\">Click here!</a></p>';

This part is the hyperlink:
<a href=\"http://url.com\">Click here!</a>
this is what i've got so far - something's a tad funky - email not going thru -can you take a look:

<?php
if (isset($_REQUEST['to']))
//if "from" is filled out, send email
  {
  //send email
  $to = $_REQUEST['to'] ;
  $from = $_REQUEST['from'] ;
  $subject = $_REQUEST['subject'] ;
  $sender = $_REQUEST['sender'] ;
  $message = "here is the message" ;
  $headers = "From: $from";
mail($to,$sender,$subject,$message,$headers);
  echo "Thank you for using our mail form";
  }
else
//if "from" is not filled out, display the form
  {
  echo "<form method='post' action='email_form.php'>
  E-mail to:<br /> <input name='to' type='text' class=\"inputbox\" size=\"25\" /><br />
  Sender:<br /> <input name='sender' type='text' class=\"inputbox\" size=\"25\" /><br />
  Your E-mail:<br /> <input name='from' type='text' class=\"inputbox\" size=\"25\" /><br />
  Subject:<br /> <input name='subject' type='text'  class=\"inputbox\" size=\"25\" /><br />
  <input type='submit' />
  </form>";
  }
?>
First of all, you have the action on the form taking you to another page, but the mailing function is on the same page, so your action should be
action="sendemail.php"
also, you have the variables in the wrong order.
The order should be:

who it is mailed to
the subject line
the message
who it is from with the headers
this is very close to working for me. i've got the email being sent. all variables working well. now i need to pass along the URL variable and i can't seem to get it to send in the email - meaning the email still goes out but there's just a blank space where the URL variable needs to be. here's my code:

<?php
if (isset($_REQUEST['to']))
//if "from" is filled out, send email
  {
  //send email
  $to = $_REQUEST['to'] ;
  $subject = $_REQUEST['subject'] ;
  $message = "This is an e-mail from Levy Angtreich Finney Baldante Rubenstein & Coren sent by $sender ($from). You may find the following Link interesting: http://url.com?id=".$_GET['id']."" ;
  $from = $_REQUEST['from'] ;
  $sender = $_REQUEST['sender'] ;
  $headers = "From: $from";
mail($to,$subject,$message,$headers);
  echo "Thank you for using our mail form";
  }
else
//if "from" is not filled out, display the form
  {
  echo "<form method='post' action='email_form.php'>
  E-mail to:<br /> <input name='to' type='text' class=\"inputbox\" size=\"25\" /><br />
  Subject:<br /> <input name='subject' type='text'  class=\"inputbox\" size=\"25\" /><br />
  Your E-mail:<br /> <input name='from' type='text' class=\"inputbox\" size=\"25\" /><br />
  Sender's Name:<br /> <input name='sender' type='text' class=\"inputbox\" size=\"25\" /><br />

 
  <input type='submit' />
  </form>";
  }
?>
You can't use two double quotes in a row, I don't think. And you don't need them anyway.
Try this:

 $message = "This is an e-mail from Levy Angtreich Finney Baldante Rubenstein & Coren sent by $sender ($from). You may find the following Link interesting: http://url.com?id=".$_GET['id'] ;

I am assuming  the url of the page you are emailing this from has the correct GET string in it.
thanks for hanging with this nanharbison:

email's being sent but i'm still not getting that variable in the url. here's my code:

here's what the email looks like:

This is an e-mail from Levy Angtreich Finney Baldante Rubenstein &
 Coren sent by craiy (craig@lfbanks.com). You may find the following Link
 interesting: http://url.com?id=

i'm successfully printing out the variable at the bottom of the code (<?php echo $_GET['id'];?>) so it's definitely getting passed
<?php
if (isset($_REQUEST['to']))
//if "from" is filled out, send email
  {
  //send email
  $to = $_REQUEST['to'] ; 
  $subject = $_REQUEST['subject'] ;
 $message = "This is an e-mail from Levy Angtreich Finney Baldante Rubenstein & Coren sent by $sender ($from). You may find the following Link interesting: http://url.com?id=".$_GET['id'] ;
  $from = $_REQUEST['from'] ;
  $sender = $_REQUEST['sender'] ;
  $headers = "From: $from";
mail($to,$subject,$message,$headers);
  echo "Thank you for using our mail form";
  }
else
//if "from" is not filled out, display the form
  {
  echo "<form method='post' action='email_form.php'>
  E-mail to:<br /> <input name='to' type='text' class=\"inputbox\" size=\"25\" /><br />
  Subject:<br /> <input name='subject' type='text'  class=\"inputbox\" size=\"25\" /><br />
  Your E-mail:<br /> <input name='from' type='text' class=\"inputbox\" size=\"25\" /><br />
  Sender's Name:<br /> <input name='sender' type='text' class=\"inputbox\" size=\"25\" /><br />
 
  
  <input type='submit' />  
  </form>";
  }
?>
<?php echo $_GET['id'];?>

Open in new window

Sometimes I have trouble using POST and GET variables as is. Try assigning a simpler variable the value of the GET ID.
$someid= $_GET['id'];

and then use:

$message = "This is an e-mail from Levy Angtreich Finney Baldante Rubenstein & Coren sent by $sender ($from). You may find the following Link interesting: http://url.com?id=$someid";
hmm -still the same result
<html>
<link href="styles/main.css" rel="stylesheet" type="text/css">
 
<body>
<div class="componentheading">
		E-mail this Link to a Friend	</div>
 
<?php
if (isset($_REQUEST['to']))
//if "from" is filled out, send email
  {
  //send email
  $someid= $_GET['id'];
  $to = $_REQUEST['to'] ; 
  $subject = $_REQUEST['subject'] ;
 $message = "This is an e-mail from Levy Angtreich Finney Baldante Rubenstein & Coren sent by $sender ($from). You may find the following Link interesting: http://url.com?id=$someid";
  $from = $_REQUEST['from'] ;
  $sender = $_REQUEST['sender'] ;
  $headers = "From: $from";
mail($to,$subject,$message,$headers);
  echo "Thank you for using our mail form";
  }
else
//if "from" is not filled out, display the form
  {
  echo "<form method='post' action='email_form.php'>
  E-mail to:<br /> <input name='to' type='text' class=\"inputbox\" size=\"25\" /><br />
  Subject:<br /> <input name='subject' type='text'  class=\"inputbox\" size=\"25\" /><br />
  Your E-mail:<br /> <input name='from' type='text' class=\"inputbox\" size=\"25\" /><br />
  Sender's Name:<br /> <input name='sender' type='text' class=\"inputbox\" size=\"25\" /><br />
 
  
  <input type='submit' />  
  </form>";
  }
?>
<?php echo $_GET['id'];?>
 
</body>
</html>

Open in new window

Where is the GET id being assigned?
<img src="images/emailButton.png" alt="Email Article" onclick="MM_openBrWindow('email_form.php?id=<?php echo $row_rsArticles['id']; ?>','Email','width=400,height=500')" />

the page the form is being generated from:

http://lfbanks.com/levyweb2/news.php
can I fill out the email this link to a friend to see what is happening? I can just send it to myself.
yes-it totally works
OOOHHH! This opens in a new window, which does NOT have the URL address bar. It is not getting the variable because as soon as you hit submit the browser is not longer holding on to the GET variable a second time. You need to have a hidden input in the form that is holding on to the GET ID.
<input type="hidden" name="id" value = "'.$_GET['id'].'">

and change the url

http://url.com?id=".$_POST['id'];
<input type="hidden" name="id" value = "'.$_GET['id'].'">

getting this error on the form page now:

Parse error: syntax error, unexpected T_STRING, expecting ',' or ';' in /home/lfbanks/public_html/levyweb2/email_form.php on line 32

<html>
<link href="styles/main.css" rel="stylesheet" type="text/css">
 
<body>
<div class="componentheading">
		E-mail this Link to a Friend	</div>
 
<?php
if (isset($_REQUEST['to']))
//if "from" is filled out, send email
  {
  //send email
  $someid= $_GET['id'];
  $to = $_REQUEST['to'] ; 
  $subject = $_REQUEST['subject'] ;
 $message = "This is an e-mail from Levy Angtreich Finney Baldante Rubenstein & Coren sent by $sender ($from). You may find the following Link interesting: http://url.com?id=".$_POST['id'];
  $from = $_REQUEST['from'] ;
  $sender = $_REQUEST['sender'] ;
  $headers = "From: $from";
mail($to,$subject,$message,$headers);
  echo "Thank you for using our mail form";
  }
else
//if "from" is not filled out, display the form
  {
  echo "<form method='post' action='email_form.php'>
  E-mail to:<br /> <input name='to' type='text' class=\"inputbox\" size=\"25\" /><br />
  Subject:<br /> <input name='subject' type='text'  class=\"inputbox\" size=\"25\" /><br />
  Your E-mail:<br /> <input name='from' type='text' class=\"inputbox\" size=\"25\" /><br />
  Sender's Name:<br /> <input name='sender' type='text' class=\"inputbox\" size=\"25\" /><br />
 
  <input type="hidden" name="id" value = "'.$_GET['id'].'">
  <input type='submit' />  
  </form>";
  }
?>
<?php echo $_GET['id'];?>
 
</body>
</html>

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of nanharbison
nanharbison
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
awesome!!! thank you so much for sticking with this - your solution worked perfectly