How Can I Provide the Option for Visitors to Send My Content Via E-mail?

Discussion in 'PHP' started by Masterful, Sep 26, 2008.

  1. #1
    Can anyone explain how, with PHP, I can provide the option for visitors to e-mail my content to friends? For example:

    1. Next to my content, I'll have an e-mail buttom.
    2. When visitors click it, an e-mail window will open, pre-populated with my content (selected from a database).
    3. All the visitor has to do is enter an e-mail address, hit "Submit" and send my content to a friend.
    Anyone know how to do it?
     
    Masterful, Sep 26, 2008 IP
  2. dreamajax

    dreamajax Peon

    Messages:
    13
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    this is invite friend. you can find that in many sites. content is static or dynamic. ?
     
    dreamajax, Sep 27, 2008 IP
  3. Masterful

    Masterful Well-Known Member

    Messages:
    1,653
    Likes Received:
    28
    Best Answers:
    0
    Trophy Points:
    140
    #3
    My content is dynamic. I SELECT it from a MySQL database, and echo it in a WHILE loop. It's a list of jokes, each one with its own ID.

    What I want to do is, have an "E-mail this Joke to a Friend" link under each joke. When clicked, I want an e-mail box to open with the joke inside. I want it so that all the visitor has to do is input the e-mail address and hit "Send".

    I know how to SELECT the jokes, but I don't understand how to do the rest.

    • Will the button be an <INPUT> tag?
    • During the initial WHILE loop which echos the jokes onto my page, do I call the IDs and have them echoed into the NAME attribute of each joke's <INPUT> tag?
    • Do I then $_POST the ID to another page which SELECTs the joke and puts it into an e-mail form?

      ANyone know? :confused:
     
    Masterful, Sep 27, 2008 IP
  4. JAY6390

    JAY6390 Peon

    Messages:
    918
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    0
    #4
    I'd say the best way would be to have an email form on a separate page, and to send the id to that page, so sendform.php is the form

    Then in your jokes output, have a link to sendform.php?id=X where x is the joke id
    Then when the user clicks on that link, it shows the page, with a box for the email address of the recipient and a box for the sender name (optionally their email too). You DO NOT want to allow the joke to be editable since that is a prime example of how spammers can abuse your system. just have a hidden field with the joke id and retrieve the joke before sending the email
     
    JAY6390, Sep 27, 2008 IP
    Masterful likes this.
  5. Masterful

    Masterful Well-Known Member

    Messages:
    1,653
    Likes Received:
    28
    Best Answers:
    0
    Trophy Points:
    140
    #5
    Thanks, Jay! Rep points added! :)

    A quick question . . .

    I echo my jokes onto my page in a while loop. How do I put the while loop results into a variable to send to the next page, which will e-mail the joke?
     
    Masterful, Sep 27, 2008 IP
  6. JAY6390

    JAY6390 Peon

    Messages:
    918
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    0
    #6
    can you show the while loop code? it will help determine how you are doing this
     
    JAY6390, Sep 27, 2008 IP
  7. Masterful

    Masterful Well-Known Member

    Messages:
    1,653
    Likes Received:
    28
    Best Answers:
    0
    Trophy Points:
    140
    #7
    OK. Beneath each joke (in fact, they're coupons!), I have this form:

    <form action=\"email-coupon.php?id=" . $row['id'] ."\" method=\"post\">
    <input type=\"image\" src=\"images/emailcpn.png\" />
    </form>
    Code (markup):
    It sends the ID of the coupon to email-coupons.php.

    Email-coupons.php selects the coupon based on the ID and echos it like this:

    
    while($row = mysql_fetch_assoc($query)) {
    
    echo "<form action=\"email-coupons-manager.php\" method=\"post\">";
    
    echo "<table>";
    echo "<tr>";
    echo "<td>";
    
    echo "Coupon: <a href=\"" . $row['bmurl'] . "\">" . $row['anch'] . "</a><br />";
    echo "Store: " . $row['adv'];
    echo "<br /> Expires: ";
    echo (empty($row['formated_date']) ? $row['expun'] : $row['formated_date']);
    echo "<br /> Instructions: ";
    echo (empty($row['code']) ? $row['instr'] : $row['code']);
    echo "<br />If the link above doesn't work, copy and paste the link below into address bar:<br />";
    echo $row['bmurl'];
    
    echo "</td>";
    echo "</tr>";
    echo "<tr>";
    echo "<td>";
    
    echo "Additional words (optional):<br />";
    echo "<textarea name=\"message\" rows=\"5\" cols=\"35\"></textarea>";
    
    echo "</td>";
    echo "</tr>";
    echo "<tr>";
    echo "<td>";
    
    echo "Send to (e.g., john@aol.com, sarah@yahoo.com):<br />";
    echo "<textarea name=\"to\" rows=\"1\" cols=\"35\"></textarea>";
    
    echo "</td>";
    echo "</tr>";
    echo "<tr>";
    echo "<td>";
    
    echo "From (your e-mail address):<br />";
    echo "<textarea name=\"from\" rows=\"1\" cols=\"35\"></textarea>";
    
    echo "</td>";
    echo "</tr>";
    echo "<tr>";
    echo "<td>";
    
    echo "<br /><input type=\"submit\" name=\"send\" value=\"Send\" />";
    
    echo "</td>";
    echo "</tr>";
    echo "</table>";
    
    echo "</form>";
    
    }
    
    
    PHP:
    As you can see, on email-coupons-manager.php, I can collect the to and from, but not the coupon itself. I need to be able to send the coupon to email-coupons-manager.php in a variable.

    Do you know how I can do this?
     
    Masterful, Sep 27, 2008 IP
  8. JAY6390

    JAY6390 Peon

    Messages:
    918
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    0
    #8
    In that form, have a hidden input with the coupon id
    
    <input type="hidden" name="couponid" id="couponid" value="COUPONIDHERE" />
    HTML:
     
    JAY6390, Sep 27, 2008 IP
  9. Masterful

    Masterful Well-Known Member

    Messages:
    1,653
    Likes Received:
    28
    Best Answers:
    0
    Trophy Points:
    140
    #9
    OK. But then I'll have to SELECT the coupon again on the email-coupons-manager.php page. Although that's fine, I will still have to put the coupon into a variable before I can e-mail it:

    $to = $_REQUEST['to'];
    $from = $_REQUEST['from'];
    $message = $_REQUEST['message'];
    
    mail($to, $from, $message, $coupon);
    PHP:
    How can I put the coupon into a variable so I can send it with the $to and $from?
     
    Masterful, Sep 27, 2008 IP
  10. JAY6390

    JAY6390 Peon

    Messages:
    918
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    0
    #10
    run your query for the id, get the coupon and append/prepend it to your $message contents

    you need to do it this way to make sure your coupon gets sent to the user, otherwise the person can edit out the coupon

    You should really be checking the fields for spam preventing, in case someone has a list of email addresses and just spam. Another thing you should be using is $_POST rather than $_REQUEST
     
    JAY6390, Sep 27, 2008 IP
  11. Masterful

    Masterful Well-Known Member

    Messages:
    1,653
    Likes Received:
    28
    Best Answers:
    0
    Trophy Points:
    140
    #11
    I don't understand what you mean. How can I append it on the $message?
     
    Masterful, Sep 27, 2008 IP
  12. JAY6390

    JAY6390 Peon

    Messages:
    918
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    0
    #12
    Sorry I misread...
    $query = sprintf("SELECT * FROM `coupon_table_name` WHERE `id`='%d'",$_POST['couponid']);
    $res = mysql_query($query);
    if(!$res)
    {
    //ERROR STUFF HERE
    }else{
    $row = mysql_fetch_assoc($res);
    $coupon = $row['coupon_column_name'];
    //EMAIL HERE
    }
    PHP:
     
    JAY6390, Sep 27, 2008 IP
  13. Masterful

    Masterful Well-Known Member

    Messages:
    1,653
    Likes Received:
    28
    Best Answers:
    0
    Trophy Points:
    140
    #13
    Thanks for the help, Jay! :)
     
    Masterful, Sep 27, 2008 IP
  14. JAY6390

    JAY6390 Peon

    Messages:
    918
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    0
    #14
    You're welcome :)
     
    JAY6390, Sep 27, 2008 IP