Link to home
Start Free TrialLog in
Avatar of mattimeikalainen
mattimeikalainen

asked on

Tell to friend php form

I want make little tell to friend form and there is one field where all friends email address goes separate dot, commas or point. But I don't figure out how it works :)

Here is my form without action (send page) because I haven't do it yet.


<table border="0" cellpadding="10" cellspacing="0">
<tr>
<td width="397"> *Your Friend's email address:</td>
<td width="301">&nbsp;</td>
</tr>
<tr>
  <td><input size="30" name="friends_address" maxlength="45"></td>
  <td>&nbsp;</td>
</tr>
<tr>
  <td>*Sender emailtd>
  <td>&nbsp;</td>
</tr>
<tr>
  <td><input size="30" name="sender_email" maxlength="45"></td>
  <td>&nbsp;</td>
</tr>
<tr>
<td> *Nimesi:</td>
<td rowspan="5" align="right">&nbsp;</td>
</tr>
<tr>
<td><input size="30" name="name" maxlength="45"></td>
</tr>

<tr>
<td>&nbsp;</td>
</tr>
<tr>
<td>Viestisi</td>
</tr>
<tr>
<td><label>
  <textarea name="message" id="viestisi" cols="60" rows="5"></textarea>
</label></td>
</tr>
<tr>
<td>
<p align="left"> Voi l&auml;hett&auml;&auml; suosituksen my&ouml;s usealle vastaanottajalle. Erota osoitteet pilkulla ja v&auml;lily&ouml;nill&auml; toisistaan. <br>Sähköpostiosoitteesi ja nimesi näytetään vastaanottajalle.<br>
<br>
*Tähdellä merkityt ovat pakollisia
</td>
<td align="right"><input onClick="validate();" type="button" value="Suosittele"></td>
</tr>
</table>
</center>
</div>
</form>
</td>
</tr> 
<tr valign="top"> 
<td valign="middle" align="center">&nbsp; 

</td>
</tr> 
</table>

Open in new window

Avatar of Loganathan Natarajan
Loganathan Natarajan
Flag of India image

You can split it with explode,

ref. http://php.net/manual/en/function.explode.php
ASKER CERTIFIED SOLUTION
Avatar of Tiller79188231
Tiller79188231

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
Avatar of kumaranmca
kumaranmca

there two functions are available in php

1. explode
2. split

to split the string to array and make it an array. To get the user entered email ids. Please refer the below url.

http://www.plus2net.com/php_tutorial/tell_friend.php
When I want to add a "send this page to a friend" link to a web page, I let the browser handle it.  Almost every PHP installation populates these fields, and almost every browser obeys the "mailto" link.  Pretty easy.  Plus, you do not have the issue of client reluctance since they are not exposing their email address or their friends' email address(es) to the risk of getting captured in a spam machine.


<?php // RAY_email_page_to_friend.php
error_reporting(E_ALL);

// DEMONSTRATE HOW TO ADD A "SEND THIS PAGE" LINK

$y = $_SERVER["HTTP_HOST"] ;
$z = urlencode($_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"]);

echo "<a href=\"mailto:?subject=$y&body=$z\">send this page to a friend</a>";

Open in new window

Avatar of mattimeikalainen

ASKER

I try next send code, but I don't figure out how this split or explode works.  Tiller79188231 and kumaranmca your looks right to me but I don't understand how it has to be in my code :-(
<?php


if(count($_POST)) {

foreach(array('friendmail1','friendmail2','friendmail3','email','name') as $key) $_POST[$key] = strip_tags($_POST[$key]);
if(!is_secure($_POST)) { die("Asta la vista Hacker");}

$emailto = "mail@mail.com"; 
$esubject = "Viesti on lähetetty"; 
$emailtext = "
$_POST[name] Kerro kaveria lomaketta on käytetty. Käyttäjän sähköpostiosoite on $_POST[email]
Sivua suositeltiin seuraaville henkilöille:

$_POST[friendmail1]
$_POST[friendmail2]
$_POST[friendmail3]

";

@mail("$emailto", $esubject, $emailtext, "From: $_POST[email]");

$thankyoupage = "kiitos.php"; 
$tsubject = "Suositus ystävältäsi $_POST[name]";
$ttext = "
Hei,
$_POST[name] On lähettänyt tämän viestin sinulle ja hän arvelee sinun pitävän alla olevasta sivusta.

http://www.xxx.com/

";

@mail("$_POST[friendmail1],$_POST[friendmail2],$_POST[friendmail3]", $tsubject, $ttext, "FROM: $_POST[email]");

header("Location: $thankyoupage");
exit;

}



function is_secure($ar) {
$reg = "/(Content-Type|Bcc|MIME-Version|Content-Transfer-Encoding)/i";
if(!is_array($ar)) { return preg_match($reg,$ar);}
$incoming = array_values_recursive($ar);
foreach($incoming as $k=>$v) if(preg_match($reg,$v)) return false;
return true;
}

function array_values_recursive($array) {
$arrayValues = array();
foreach ($array as $key=>$value) {
if (is_scalar($value) || is_resource($value)) {
$arrayValues[] = $value;
$arrayValues[] = $key;
}
elseif (is_array($value)) {
$arrayValues[] = $key;
$arrayValues = array_merge($arrayValues, array_values_recursive($value));
}
}
return $arrayValues;
}
?>

Open in new window

From that code snippet, it looks like you need a learning resource to get some foundation in PHP.  Consider getting this book:
http://www.sitepoint.com/books/phpmysql4/

The Sitepoint book will not make you a pro, but if you give yourself a month to read it and work through the examples, you will be way ahead.

Here are the links that tell how the PHP functions split() and explode() work.  Note the large red warning box on split().
http://us2.php.net/manual/en/function.split.php
http://us2.php.net/manual/en/function.explode.php

The standard "form-to-email" script is pretty well understood in the industry today.  Here is a simple teaching example.  Looks like you would want to use information from the POST form to populate the $to array.  To avoid becoming an open relay for spam, you need to make sure you have clean strings.  Note that there will be no commas or semi-colons permitted in the external data.

HTH, ~Ray
<?php // RAY_form_to_email.php
error_reporting(E_ALL);

// SEND MAIL FROM A FORM

// A FUNCTION TO CLEAN UP THE DATA - AVOID BECOMING AN OPEN-RELAY FOR SPAM
function clean_string($str)
{
    $str = stripslashes($str);
    $str = trim(preg_replace("/ +/", " ", $str));
    $str = preg_replace('/^ a-zA-Z0-9&+:?_\.\-/', '', $str);
    return $str;
}

// REQUIRED VALUES ARE PREPOPULATED - CHANGE THESE FOR YOUR WORK
$from  = "NoReply@Your.org";
$subj  = "Contact Form";

// THIS IS AN ARRAY OF RECIPIENTS
$to[]  = "You@Your.org";
$to[]  = "Her@Your.org";
$to[]  = "Him@Your.org";

// IF THE DATA HAS BEEN POSTED
if (!empty($_POST['email']))
{
    // CLEAN UP THE POTENTIALLY BAD AND DANGEROUS DATA
    $email      = clean_string($_POST["email"]);
    $name       = clean_string($_POST["name"]);
    $telephone  = clean_string($_POST["telephone"]);

    // CONSTRUCT THE MESSAGE
    $content    = '';
    $content   .= "You have a New Query From $name \n\n";
    $content   .= "Tel No: $telephone\n";
    $content   .= "Email: $email\n";

    // SEND MAIL TO EACH RECIPIENT
    foreach ($to as $recipient)
    {
        if (!mail( $recipient, $subj, $content, "From: $from\r\n"))
        {
            echo "MAIL FAILED FOR $recipient";
        }
        else
        {
            echo "MAIL WORKED FOR $recipient";
        }
    }
} // END OF PHP - PUT UP THE FORM
?>
<form method="post">
<br/>Email: <input name="email" />
<br/>Phone: <input name="telephone" />
<br/>Name:  <input name="name" />
<br/><input type="submit" />
</form>

Open in new window

Yes I am not so familiar with php, but can you help me. I can't figure out this :( That eplode system is  the answer but how i get those from post method with the form field friends_address,
something like:  
$to[]  = "explode(",", $friends_address);";

??
foreach(array('friendmail1','friendmail2','friendmail3','email','name') as $key)

why is the a foreach??.. the form can only be submitted once...

HOW are the email addresses your wanting to send mail to inputted into the form?.. same text box?.. different text box??
First I have three different email filelds friendmail1, friendmail2 and friendmail3. But is it possible to have one email field where can write all email addresses separated with , ? mail1@hotmail.com, mail2@hotmail.com, mail3@hotmail.com and so on one input field...
Don't look at mine code, that is very wrong. I thinks that $emails = explode(",", $stringOfEmails); is right but I don't figure out how it works...

My form  are only next fields: sender address field, friends address field (where goes all email addresses separated with ,)  and message field. The goes also somekind of message "your friends thinks that you like this site" or something...
So you would get the information from the text field

$emails = explode(",", $_POST['Field_with_emails_seperated_by_comma'];

foreach($emails as $sendToEmail){


//code to send email here


}
Hello Tiller79188231 and thank you of your answer :-)

Now I get an error Parse error: syntax error, unexpected ';' in /var/www/customers/mlifefi/public_html/kerrokaverille/2.php on line 14

Here is action page code: I find another tell a friend code which i test. I just figure out that form have to work also only one email address. One or more email address.

<?php
$fromName=$_REQUEST["fromName"];
$fromEmail=$_REQUEST["fromEmail"];
$toName=$_REQUEST["toName"];
$emails = explode(",", $_POST['toEmail'];
foreach($emails as $sendToEmail){
if ($fromEmail) { $ref=getenv('HTTP_REFERER');
if ($fromName=='')
$subject= "Your friend has referred you this link from www.sitename.com";
else $subject= "$fromName has referred you this link from www.sitename.com";
$message = " Dear $toName, Your friend $fromName has referred this link to you. Please visit this link by clicking $ref ";
$from = "From: $fromEmail\r\n"; mail($toEmail, $subject, $message, $from); header ("location:".$ref); }
?>
I would try and layout your code a little easier to read for yourself...

$emails = explode(",", $_POST['toEmail']; should be $emails = explode(",", $_POST['toEmail']);
and you are missing a closing bracket at the end to close out your foreach statement

here is the fixed code

<?php
$fromName=$_REQUEST["fromName"];
$fromEmail=$_REQUEST["fromEmail"];
$toName=$_REQUEST["toName"];

$emails = explode(",", $_POST['toEmail']);


foreach($emails as $sendToEmail){
      if ($fromEmail) {
            $ref=getenv('HTTP_REFERER');
            if ($fromName=='')
                  $subject= "Your friend has referred you this link from www.sitename.com";
            else
                  $subject= "$fromName has referred you this link from www.sitename.com";
                  
            $message = " Dear $toName, Your friend $fromName has referred this link to you. Please visit this link by clicking $ref ";
            $from = "From: $fromEmail\r\n";
            mail($toEmail, $subject, $message, $from);
            header ("location:".$ref);
      }
}
?>
Oh and also, your foreach statement will only run for one email.. because the last line.. "header("Location:".$ref);".. will send the browser to another page and stop the script from running