Sending an image to email via PHP form?

Discussion in 'PHP' started by Egnited, Aug 14, 2008.

  1. #1
    I am developing a [mobile] website that needs to allow visitors to send both an image (which is on my server) and a custom message to a friend's email using a PHP form. I have to keep it mobile-compatible, so I can't use java, etc.

    Here is my form so far... but the form can be totally scratched if necessary. I'm not very knowledgeable at PHP.

    
    <!doctype html public "-//w3c//dtd html 3.2//en">
    
    <html>
    
    <head>
    <title>Demo</title>
    </head>
    
    <body>
    
    <form method=post action=tellck.php>
    <table  border="0" cellpadding="0" cellspacing="0" align=center>
    <tr bgcolor='#f1f1f1'><td colspan=2 align=center><font face='Verdana' size='2' ><b>Send Image to Friend</b></font></td></tr>
    
    <tr><td width=100><font face='Verdana' size='2' >Your Name</font></td><td width=200><input type=text name=y_name></td></tr>
    <tr bgcolor='#f1f1f1'><td><font face='Verdana' size='2' >Your Email</font></td><td><input type=text name=y_email></td></tr>
    <tr ><td><font face='Verdana' size='2' >Your Friend's Name</font></td><td><input type=text name=f_name></td></tr>
    <tr bgcolor='#f1f1f1'><td><font face='Verdana' size='2' >Friend's Email</font></td><td><input type=text name=f_email></td></tr>
    <tr ><td><font face='Verdana' size='2' >Your Message</font></td><td><textarea name=y_msg rows=5 cols=20></textarea></td></tr>
    
    <tr bgcolor='#f1f1f1'><td colspan=2 align=center><font face='Verdana' size='2' ><input type=submit value='Send'></font></td></tr>
    </table>
    </form>
    
    </body>
    
    </html>
    
    
    PHP:
    And the next page:

    
    <!doctype html public "-//w3c//dtd html 3.2//en">
    
    <html>
    
    <head>
    <title>Demo</title>
    <meta name="GENERATOR" content="Arachnophilia 4.0">
    <meta name="FORMATTER" content="Arachnophilia 4.0">
    </head>
    
    <body bgcolor="#ffffff" text="#000000" link="#0000ff" vlink="#800080" alink="#ff0000">
    
    <?
    $status = "OK";
    $msg="";
    $y_email=$_POST['y_email'];
    $y_name=$_POST['y_name'];
    $f_email=$_POST['f_email'];
    $f_name=$_POST['f_name'];
    $y_msg=$_POST['y_msg'];
    
    
    if(substr_count($y_email,"@") > 1  or substr_count($f_email,"@") > 1){
    $msg .="Use only one email address<BR>"; 
    $status= "NOTOK";
    }
    if (!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $y_email)){ // checking your email 
    $msg .="Your email address is not correct<BR>"; 
    $status= "NOTOK";}
    
    if (strlen($y_name) <2 ) { // checking your name
    $msg .="Please enter your name<BR>"; 
    $status= "NOTOK";}
    
    if (!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $f_email)) { // checking friends email 
    $msg .="Your Friends address is not correct<BR>"; 
    $status= "NOTOK";}
    
    if (strlen($f_name) <2 ) { // checking freinds name
    $msg .="Please enter your friend's name<BR>"; 
    $status= "NOTOK";}
    
    if (strlen($y_msg) <2 ) { // checking Message details
    $msg .="Please enter your message details<BR>"; 
    $status= "NOTOK";}
    
    if($status=="OK"){ // all validation passed
    /////////// Sending the message starts here //////////////
    $ip=$_SERVER['REMOTE_ADDR']; // Collect the IP address of visitor
    
    $ref=@$HTTP_REFERER; 
    /////Message at the top of the page showing the url////
    $header_message = "Hey $f_name!  Your friend $y_name thought you'd enjoy this picture!";
    /// Body message prepared with the message entered by the user ////
    $body_message =$header_message."\n".$y_msg."\n";
    $body_message .="\n This message was sent to you using http://www.website.mobi";
    
    //// Mail posting part starts here /////////
    
    $headers="";
    //$headers = "Content-Type: text/html; charset=iso-8859-1\n".$headers; 
     // Un comment the above line to send mail in html format
    $headers4=$y_email;         // Change this to change from address
    $headers.="Reply-to: $headers4\n";
    $headers .= "From: $headers4\n"; 
    $headers .= "Errors-to: $headers4\n"; 
    
    $subject="Request to visit URL";
    
    mail($f_email,$subject,$body_message,$headers);
    
    
    
    ////// Mail posting ends here ///////////
    echo "<center><font face='Verdana' size='2' color=green>Thank You, Your message posted to $f_name</font></center>";
    
    //////////// Sending the message ends here /////////////
    }else{// display the error message
    echo "<center><font face='Verdana' size='2' color=red>$msg</font></center>";
    }
    ?>
    
    </body>
    
    </html>
    
    PHP:

    This produces:

    http://www.egnited.net/demo/tell.php


    So, how can I make the form send an image (from a URL) to the email?

    I also need the have the form send to the user from MY email, not the user's...


    I would really appreciate any help on this!

    Thanks!

    Tom
     

    Attached Files:

    Egnited, Aug 14, 2008 IP
  2. BMR777

    BMR777 Well-Known Member

    Messages:
    145
    Likes Received:
    8
    Best Answers:
    1
    Trophy Points:
    140
    #2
    Take a look at this article which describes how to send an .html email using PHP. Assuming you only have a few images on your server and you want the user to specify the image to send, you could simply take in their choice in the form and when it is posted do an IF statement to determine which image to send. If you want the same image all the time, it's even easier.

    Just format your email the special way as described in the article, then you can send a .html email and use the html img tag to specify the URL to display your image off of the server. This should work fine for most modern webmail and PC based email clients that can understand .html emails.

    Hope it helps,
    BMR777
     
    BMR777, Aug 15, 2008 IP
  3. XenFen

    XenFen Banned

    Messages:
    48
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Yep, read that article, it should help :)
     
    XenFen, Aug 15, 2008 IP