Solved

Tell to friend php form

Posted on 2010-09-03
13
1,081 Views
Last Modified: 2012-05-10
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

0
Comment
Question by:mattimeikalainen
  • 5
  • 4
  • 2
  • +2
13 Comments
 
LVL 36

Expert Comment

by:Loganathan Natarajan
Comment Utility
You can split it with explode,

ref. http://php.net/manual/en/function.explode.php
0
 
LVL 2

Accepted Solution

by:
Tiller79188231 earned 450 total points
Comment Utility
you have no opening form tag.
are you wanting to split the emails in php?
if so it would be done like this

$emails = explode(",", $stringOfEmails);
$emails would now be an array of the emails entered
0
 
LVL 8

Expert Comment

by:kumaranmca
Comment Utility
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
0
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
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

0
 

Author Comment

by:mattimeikalainen
Comment Utility
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

0
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
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

0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 

Author Comment

by:mattimeikalainen
Comment Utility
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);";

??
0
 
LVL 2

Expert Comment

by:Tiller79188231
Comment Utility
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??
0
 

Author Comment

by:mattimeikalainen
Comment Utility
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...
0
 
LVL 2

Expert Comment

by:Tiller79188231
Comment Utility
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


}
0
 

Author Comment

by:mattimeikalainen
Comment Utility
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); }
?>
0
 
LVL 2

Expert Comment

by:Tiller79188231
Comment Utility
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);
      }
}
?>
0
 
LVL 2

Expert Comment

by:Tiller79188231
Comment Utility
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
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL (http://www.experts-exchange.com/articles/201/Handling-Date-and-Time-in-PHP-and-MySQL.html) several years ago, it seemed like now was a good time to updat…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
The viewer will learn how to dynamically set the form action using jQuery.

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now