SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Member
    Join Date
    Aug 2013
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Php Email Script Help with attachment

    Hi everyone. I am here because I need some help. I have searched and searched for days and read so many articals on this but I admit at 61 I just can not get my head around it. So here I am asking you for your help. My Wife and I run an Internation Homestay and we have an online application form our students must fill out and submit to us for a Homestay Booking The form and the php mail script work fantastic and have so for several years. However now I need to add an attachment part to the form and email This attachment will be the students current photo in jpg, gif or png file format. The pgoto would be attached to the email application form. Here is what I have. This is the input for the attachment on the form:
    HTML Code:
    <tr><td><p>Please send us a current Photo of you: </td><td>
    <input type="file"send us you photo name="attachment" />
    </p>
    </td></tr>
    This part goes into my form.

    Here is my current php mail script:
    PHP Code:
    <?php


    //--------------------------Set these paramaters--------------------------


    $subject 'New Homestay Application';                // Subject of email sent to you.
    $emailadd 'you@yourdomain.com';        // Your email address. This is where the form information will be sent.
    $url 'thankyou.html';               // Where to redirect after form is processed.
    $req '0';                                  // Makes all fields required. If set to '1' no field can not be empty. If set to '0' any or all fields can be empty.

    // --------------------------Do not edit below this line--------------------------
    $text "New Homestay Application:\n\n";       
    $space ' ';
    $line '
    '
    ;
    foreach (
    $_POST as $key => $value)
    {
        if (
    $req == '1')
        {
            if (
    $value == '')
            {echo 
    "$key is empty";die;}
        }
        
    $j strlen($key);
            if (
    $j >= 20)
            
        
    $j 20 $j;
            for (
    $i 1$i <= $j$i++)
            {
    $space .= ' ';}
        
    $value str_replace('\n'"$line"$value);
        
    $conc "{$key}:$space{$value}$line";
        
    $text .= $conc;
        
    $space ' ';
    }
    mail($emailadd$subject$text'From: '.$emailadd.'');
    echo 
    '<META HTTP-EQUIV=Refresh CONTENT="0; URL='.$url.'">';
    ?>
    Please note that the application form is very long and I realy do not re-wright it all over again to accomodate this new part and I am hopping that someone here can help me with the script to make this work.

    Kindest Regards
    and thank you for your help.

  2. #2
    Patience... bronze trophy solidcodes's Avatar
    Join Date
    Jul 2006
    Location
    Philippines
    Posts
    933
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by pagemaker69 View Post
    Hi everyone. I am here because I need some help. I have searched and searched for days and read so many articals on this but I admit at 61 I just can not get my head around it. So here I am asking you for your help. My Wife and I run an Internation Homestay and we have an online application form our students must fill out and submit to us for a Homestay Booking The form and the php mail script work fantastic and have so for several years. However now I need to add an attachment part to the form and email This attachment will be the students current photo in jpg, gif or png file format. The pgoto would be attached to the email application form. Here is what I have. This is the input for the attachment on the form:
    HTML Code:
    <tr><td><p>Please send us a current Photo of you: </td><td>
    <input type="file"send us you photo name="attachment" />
    </p>
    </td></tr>
    This part goes into my form.

    Here is my current php mail script:
    PHP Code:
    <?php


    //--------------------------Set these paramaters--------------------------


    $subject 'New Homestay Application';                // Subject of email sent to you.
    $emailadd 'you@yourdomain.com';        // Your email address. This is where the form information will be sent.
    $url 'thankyou.html';               // Where to redirect after form is processed.
    $req '0';                                  // Makes all fields required. If set to '1' no field can not be empty. If set to '0' any or all fields can be empty.

    // --------------------------Do not edit below this line--------------------------
    $text "New Homestay Application:\n\n";       
    $space ' ';
    $line '
    '
    ;
    foreach (
    $_POST as $key => $value)
    {
        if (
    $req == '1')
        {
            if (
    $value == '')
            {echo 
    "$key is empty";die;}
        }
        
    $j strlen($key);
            if (
    $j >= 20)
            
        
    $j 20 $j;
            for (
    $i 1$i <= $j$i++)
            {
    $space .= ' ';}
        
    $value str_replace('\n'"$line"$value);
        
    $conc "{$key}:$space{$value}$line";
        
    $text .= $conc;
        
    $space ' ';
    }
    mail($emailadd$subject$text'From: '.$emailadd.'');
    echo 
    '<META HTTP-EQUIV=Refresh CONTENT="0; URL='.$url.'">';
    ?>
    Please note that the application form is very long and I realy do not re-wright it all over again to accomodate this new part and I am hopping that someone here can help me with the script to make this work.

    Kindest Regards
    and thank you for your help.
    Try using the PHPMailer class, this way you don't need to re-invent the wheel.
    Just google it.
    Quality codes are optimized and tested...
    Click here for inspiration..

  3. #3
    Community Advisor bronze trophy
    fretburner's Avatar
    Join Date
    Apr 2013
    Location
    Brazil
    Posts
    1,389
    Mentioned
    45 Post(s)
    Tagged
    12 Thread(s)
    Hi pagemaker69,

    You don't need to make too many changes to your actual form to allow uploads
    • Make sure you set the enctype attribute of the <form> tag to multipart/form-data:
      HTML Code:
      <form enctype="multipart/form-data" action="youraction.php" method="POST">
    • Add a hidden field to specify the maximum file size (in bytes - below it's set to 1mb) of the photo that users can attach. Note that this has to come before the file input tag:
      HTML Code:
      <input type="hidden" name="MAX_FILE_SIZE" value="1000000" />

    From PHP, you can access the uploaded file via the $_FILES global variable. You can find details about this in the PHP manual, here: http://us3.php.net/manual/en/feature...ost-method.php

    If you look at example 2 on that page, it shows you how to validate the uploaded file, which you can then attach to the email. Adding images to emails sent with PHP's mail function can be tricky, so solidcodes suggestion about using PHPMailer is a good idea - you can see in this example how to send an email with an image attachment.

    Hope that helps. Feel free to reply if you want more help with any particular step.

  4. #4
    SitePoint Member
    Join Date
    Aug 2013
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I am not re-inventing the wheel Just what to modify the script to send attachment is all. Not interested in installing new php mailer program on my server when what I have works just fine just need some help.

  5. #5
    Community Advisor bronze trophy
    fretburner's Avatar
    Join Date
    Apr 2013
    Location
    Brazil
    Posts
    1,389
    Mentioned
    45 Post(s)
    Tagged
    12 Thread(s)
    Quote Originally Posted by pagemaker69 View Post
    I am not re-inventing the wheel Just what to modify the script to send attachment is all. Not interested in installing new php mailer program on my server when what I have works just fine just need some help.
    What you have works fine with sending the form, but you want to upload and attach an image to the email right? And unfortunately, it's not just a case of changing a line or two of code.

    If you really want to keep using PHP's mail function directly, take a look here, and here to get an idea of what's involved. The nice thing about using a library like PHPMailer is that it hides all that complexity so you don't have to deal with it.

  6. #6
    SitePoint Member
    Join Date
    Aug 2013
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Problem solved Thanks I rewrite the script to accomodate for atachment and made small ajustment to my form.


Bookmarks

Posting Permissions

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