Server-side Starter Tutorial:Tell-a-Friend - Part 2 of 6

OK, what next?

The next bit is pretty simple:

  1. We determine what data were sent (posted) from the html form
  2. We construct the four essential elements of an e-mail: From, To, Subject, and Body
  3. We send the message

What was 'posted'?

There's a simple little syntax for determining what data was 'posted' from a form where the METHOD used was 'post':

$some_variable = $_POST['something_posted_in_form'];

Or, for our case where the name of the html form field for the friend's e-mail address is friend_email. Since we want to mail to that address, let's recover the posted e-mail information as a variable named $mail_to with this line of code:

$mail_to = $_POST['friend_email]';

So now we know the e-mail address of their 'friend'.

... and what else?

Since the e-mail we're sending is coming from 'us', the From shouldn't present any problems. Likewise, the Subject shouldn't be any problem since we can define both by simply declaring two variables in our little php script:

$mail_from = "From:webmaster@www.yourdomain.tld";
$mail_subject = "A friend has recommended www.yourdomain.tld";

We can also define the 'body' of our tell-a-friend message:

$mail_body = "A friend has recommended you visit http://www.yourdomain.tld";

Now we have all we need - the To: and From: a Subject and a Message (body of e-mail). Time to send the mail ...

«  previous  |  next  »  

Site Links

 

Starter Tutorials