Results 1 to 6 of 6

Thread: Sending email with image via PHP

  1. #1

    Thread Starter
    Addicted Member adamlonsdale's Avatar
    Join Date
    Oct 2005
    Posts
    210

    Sending email with image via PHP

    I have a PHP form, that asks for peoples details to apply for a free transport card. Basically I get an email that looks as though it is sent from them, and then have to reply to it asking for a photo. Here is my code:

    On the form page:

    HTML Code:
    <form method="POST" action="[I]hidden[/I]">
            <tr>
              <td class="size">Title</td>
              <td width="326" class="stuff"><input type="text" name="title" size="19" /></td>
            </tr>
            <tr>
              <td class="size">First Name</td>
              <td class="stuff"><input type="text" name="first" size="19" /></td>
            </tr>
            <tr>
              <td class="size">Last Name</td>
              <td class="stuff"><input type="text" name="last" size="19" /></td>
            </tr>
            <tr>
              <td class="size">&nbsp;</td>
              <td class="stuff">&nbsp;</td>
            </tr>
            <tr>
              <td class="size">Date of Birth</td>
              <td class="stuff"><input name="day" type="las" size="2" maxlength="2" />
                /
                <input name="month" type="text" size="2" maxlength="2" />
                /
                <input name="year" type="text" size="4" maxlength="4" /></td>
            </tr>
            <tr>
              <td class="size">&nbsp;</td>
              <td class="stuff">&nbsp;</td>
            </tr>
            <tr>
              <td class="size">Address</td>
              <td class="stuff"><textarea rows="5" name="address" cols="30"></textarea></td>
            </tr>
            <tr>
              <td class="size">Telephone</td>
              <td class="stuff"><input type="text" name="tel" size="19" /></td>
            </tr>
            <tr>
              <td class="size">Email</td>
              <td class="stuff"><input type="text" name="email" size="19" /></td>
            </tr>
            <tr>
              <td class="size">&nbsp;</td>
              <td class="stuff">&nbsp;</td>
            </tr>
            <tr>
              <td class="size">School</td>
              <td class="stuff"><select  size="1"  name="drop_down">
                <option selected="selected"></option>
                <option>Beverley High School</option>
                <option>Beverley Youth Centre</option>
                <option>Bishop Burton College</option>
                <option>Bridlington School Sports College</option>
                <option>Bridlington Youth Centre</option>
                <option>Cottingham High School</option>
                <option>Driffield School</option>
                <option>Driffield Youth Centre</option>
                <option>East Riding College (Beverley Campus)</option>
                <option>East Riding College (Bridlington Campus)</option>
                <option>Goole Youth Centre</option>
                <option>Headlands School and Community Science College</option>
                <option>Hessle High Lower School</option>
                <option>Hessle High Upper School</option>
                <option>Hull College</option>
                <option>Hornsea School and Language College</option>
                <option>Hornsea Youth Centre</option>
                <option>Howden School</option>
                <option>Howden Youth Centre</option>
                <option>John Leggott College</option>
                <option>Longcroft School</option>
                <option>Market Weighton Youth Centre</option>
                <option>Pocklington School</option>
                <option>Pocklington Youth Centre</option>
                <option>Old Goole Youth Centre</option>
                <option>Snaith Youth Centre</option>
                <option>South Holderness Technology College</option>
                <option>South Holderness Youth Centre
                <option>South Hunsley School</option>
                <option>South Hunsley Youth Centre</option>
                <option>The Market Weighton School</option>
                <option>The Snaith School</option>
                <option>Wilberforce College</option>
                <option>Withernsea High School</option>
                <option>Withernsea Youth Centre</option>
                <option>Woldgate College</option>
                <option>Wolfreton Lower School</option>
                <option>Wolfreton Youth Centre</option>
                <option>Wyke College</option>
                <option>Other</option>
              </select></td>
            </tr>
            <tr>
              <td class="size"><p class="size">Other</p></td>
              <td class="stuff"><input type="text" name="other" size="30" /></td>
            </tr>
            <tr>
              <td class="size">&nbsp;</td>
              <td class="stuff">&nbsp;</td>
            </tr>
            <tr>
              <td class="size"><p class="size">Where did you hear about the scheme?</p></td>
              <td class="stuff"><input type="text" name="where" size="30" id="where" /></td>
            </tr>
            <tr>
              <td class="size">Do you have any further comments?</td>
              <td class="stuff"><textarea rows="5" name="comments" cols="30" id="comments"></textarea></td>
            </tr>
            <tr>
              <td class="size">&nbsp;</td>
              <td class="stuff">&nbsp;</td>
            </tr>
            <tr>
              <td class="size"><p class="size">Data Protection</p>
                <p><em class="xxsmall"><b></b>Due to the nature of the pilot, you must agree to allow the East Riding of Yorkshire Council to access your bus card data.</em></p></td>
              <td class="stuff"><input  type="radio"  value="yes"  name="radio" />
                Yes<br />
                <input type="radio" value="no" name="radio" />
                No</td>
            </tr>
            <tr>
              <td colspan="2"><p align="center">
                <input name="submit" type="submit" class="SubmitButton" align="middle" value="Submit" />
              </p></td>
            </tr>
          </table></td></form>
    And then on the processing page:

    PHP Code:
    <?php
    if(isset($_POST['submit'])) {

    $to "[I]my email address[/I]";
    $subject "Pilot Card Application";
    $title $_POST['title'];
    $first $_POST['first'];
    $last $_POST['last'];
    $day $_POST['day'];
    $month $_POST['month'];
    $year $_POST['year'];
    $address $_POST['address'];
    $tel $_POST['tel'];
    $email $_POST['email'];
    $option  =  $_POST['radio'];
    $dropdown  =  $_POST['drop_down'];
    $other  =  $_POST['other'];
    $header "From: $email <$email>";
    $where $_POST['where'];
    $comments $_POST['comments'];

    $body "Transport Pilot Application from $title $first $last\n\n Title: $title\n First Name: $first\n Last Name: $last\n\n Date of Birth: $day / $month / $year\n\n Address:\n$address\n\n Telephone Number: $tel\n Email: $email\n\n School: $dropdown\n Other: $other\n\n Data Protection: $option\n\n $first heard about the scheme from: $where\n\n Other Comments: $comments";
     
    echo
    "<font=verdana><p align=center><b>Thank you for applying for the East Riding Youth Assembly's transport card. <br /> Somebody will get back to you within a few days.</p><br /><p align=center>If you have any further enquiries, please contact [I]my managers email address[/I] or call [I]his phone numbe[/I]</p></b></font>";


    mail($to$subject$body$header);

    } else {

    echo 
    "blarg!";

    }
    ?>
    Basically, how would I modify this code to add:

    • A form where users can upload a picture
    • A part to the mailer form that would send the image as an attachment
    • Facilities to limit the size and file type.


    Adam.

  2. #2
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    Re: Sending email with image via PHP

    • to upload a picture via a form, you can simply add another <input> of type "file" and maybe add the "enctype" attribute to your form. see here for a full example of handling uploads via POST.
    • to attach an image to an email, you can use the PHPMailer class instead of the mail() function
    • when uploading a file to your server with PHP, the file is temporarily stored after the form is submitted. this allows you to validate the image, or even manipulate it (resize, crop, whatever) before actually saving it to your server. you could check the image dimensions, the image size, the file extension, etc.


    hope that helps a little.

  3. #3

    Thread Starter
    Addicted Member adamlonsdale's Avatar
    Join Date
    Oct 2005
    Posts
    210

    Re: Sending email with image via PHP

    Have added the following:

    Upload aspect of form

    HTML Code:
    enctype="multipart/form-data
    HTML Code:
    	<tr>
    	  <td class="size"><p class="size">Please upload a photograph of yourself</p></td>
    	  <td class="stuff"><input type="file" name="uploaded_file"></td>
    	</tr>
    Mailer aspect of form (class) changed

    PHP Code:
    <?php
    if(isset($_POST['submit'])) {

    $to "email";
    $subject "Pilot Card Application";
    $title $_POST['title'];
    $first $_POST['first'];
    $last $_POST['last'];
    $day $_POST['day'];
    $month $_POST['month'];
    $year $_POST['year'];
    $address $_POST['address'];
    $tel $_POST['tel'];
    $email $_POST['email'];
    $option  =  $_POST['radio'];
    $dropdown  =  $_POST['drop_down'];
    $other  =  $_POST['other'];
    $header "From: $email <$email>";
    $where $_POST['where'];
    $comments $_POST['comments'];

    //Get the uploaded file information
    $name_of_uploaded_file =  
        
    basename($_FILES['uploaded_file']['name']);

    //get the file extension of the file
    $type_of_uploaded_file 
        
    substr($name_of_uploaded_file
        
    strrpos($name_of_uploaded_file'.') + 1);

    $size_of_uploaded_file 
        
    $_FILES["uploaded_file"]["size"]/1024;//size in KBs

    //Settings 
    $max_allowed_file_size 5120// size in KB 
    $allowed_extensions = array("jpg""jpeg""gif""bmp");
    $upload_folder './uploads/'//<-- this folder must be writeable by 

    the script

    //Validations
    if($size_of_uploaded_file $max_allowed_file_size 
    {
      
    $errors .= "\n Size of file should be less than 

    $max_allowed_file_size";
    }

    //------ Validate the file extension -----
    $allowed_ext false;
    for(
    $i=0$i<sizeof($allowed_extensions); $i++) 

      if(
    strcasecmp($allowed_extensions[$i],$type_of_uploaded_file) == 0)
      {
        
    $allowed_ext true;    
      }
    }

    if(!
    $allowed_ext)
    {
      
    $errors .= "\n The uploaded file is not supported file type. ".
      
    " Only the following file types are supported: 

    "
    .implode(',',$allowed_extensions);
    }

    //copy the temp. uploaded file to uploads folder
    $path_of_uploaded_file $upload_folder $name_of_uploaded_file;
    $tmp_path $_FILES["uploaded_file"]["tmp_name"];

    if(
    is_uploaded_file($tmp_path))
    {
      if(!
    copy($tmp_path,$path_of_uploaded_file))
      {
        
    $errors .= '\n error while copying the uploaded file';
      }
    }

    include_once(
    'PEAR/Mail.php');
    include_once(
    'PEAR/Mail/mime.php');

    $message = new Mail_mime(); 
    $message->setTXTBody($text); 
    $message->addAttachment($path_of_uploaded_file);

    $body "Transport Pilot Application from $title $first $last\n\n Title: $title\n First Name: $first\n Last Name: $last\n\n Date of Birth: $day / $month / $year\n\n Address:\n$address\n\n Telephone Number: $tel\n Email: $email\n\n School: $dropdown\n Other: $other\n\n Data Protection: $option\n\n $first heard about the scheme from: $where\n\n Other Comments: $comments";

    $extraheaders = array("From"=>$from

    "Subject"=>$subject,"Reply-To"=>$email);
    $headers $message->headers($extraheaders);
    $mail Mail::factory("mail");
    $mail->send($to$headers$body);
     
    echo
    "<font=verdana><p align=center><b>Thank you for applying for the East 
    Riding Youth Assembly's transport card. <br /> Somebody will get back to 
    you within a few days.</p><br /><p align=center>If you have any further 
    enquiries, please contact email or call 
    number</p></b></font>"
    ;

    } else {

    echo 
    "blarg!";

    }
    ?>
    After installing PEAR, I have included PEAR/Mail.php and PEAR/Mail/mime.php. This works well, but when it comes to include the mimePart.php, I believe it is looking in /home/erya/public_html/beta/PEAR/Mail/mime.php/Mail/mimePart.php, instead of /home/erya/public_html/beta/PEAR/Mail/mimePart.php. Is it possible to rewrite this?

    Warning: require_once(Mail/mimePart.php) [function.require-once]: failed to open stream: No such file or directory in /home/erya/public_html/beta/PEAR/Mail/mime.php on line 74

    Fatal error: require_once() [function.require]: Failed opening required 'Mail/mimePart.php' (include_path='.:/usr/lib/php:/usr/local/lib/php') in /home/erya/public_html/beta/PEAR/Mail/mime.php on line 74
    Adam.

  4. #4
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    Re: Sending email with image via PHP

    well, if you're on a shared hosting account with CPanel and whatever, your host should already have PEAR installed (or have a way for you to turn it on, or something). it would be installed in a directory that was accessible from your include_path (from your post, /usr/lib/php and /usr/local/lib/php). you could ask your host if they have PEAR installed, or look through your CPanel for something about it.

    this is, of course, assuming that you didn't do this already! I've never really used PEAR, but if you can't get it to work after doing some of the stuff I listed above, I'll see what I can do.

  5. #5
    New Member
    Join Date
    Dec 2009
    Posts
    1

    Re: Sending email with image via PHP

    This should be a standard operation. When doing this I have been using. In order to retrieve the file.

    file_get_contents('whateverfile.zip')

  6. #6
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    Re: Sending email with image via PHP

    uhhh. what are you even referring to? that doesn't really have anything to do with this thread.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width