#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2013
    Posts
    16
    Rep Power
    0

    How to send a resume to mail in php as attachment?


    i need to send a resume as attachment to a particular mail id...

    its my form.

    Code:
    <form method="POST" name="email" action="sendmail.php" enctype="multipart/form-data" onsubmit="return validateForm()";>
    
        <table border=0 cellPadding=5 cellSpacing=10 width=100%>
       
            <tr>
                <td><label for='name'>Name:</label></td>
                <td><input  type="text" name="name" maxlength="50" size="30"></td>
            </tr>
             <tr>
                <td><label for='familyname'>Family Name:</label></td>
                <td><input  type="text" name="fname" maxlength="50" size="30"></td>
            </tr>
             <tr>
                <td><label for='age'>Age:</label></td>
                <td><input  type="text" name="age" maxlength="50" size="30"></td>
            </tr>
            <tr>
                <td><label for='experience'>Years of Experience:</label></td>
                <td><input  type="text" name="exp" maxlength="50" size="30"></td>
            </tr>
            <tr>
                <td><label for='university'>University Attened:</label></td>
                <td><input  type="text" name="university" maxlength="50" size="30"></td>
            </tr>
             <tr>
                <td><label for='phone'>Phone Number:</label></td>
                <td><input  type="text" name="phone" maxlength="50" size="30"></td>
            </tr>
            <tr>
                <td><label for='resume'>Resume:</label></td>
                <td><input type="file" name="resume"></td>
            </tr>
            <tr>
                <td colSpan=2 align=center><input class=subbtn type="submit" value="Submit" name='submit' /></td>
            </tr>
            
        </table>
    </form>
    its my sendmail.php (without file attachment part)

    Code:
    <?php
    $name=$_POST['name'];
    $fname=$_POST['fname'];
    $age=$_POST['age'];
    $exp=$_POST['exp'];
    $university=$_POST['university'];
    $phone=$_POST['phone'];
    
    $email_to='rajletsgetlost@gmail.com';
    
    $emailsubject=ucfirst($name).' Resume';
    $body.="Hi,\n";
    $body.="Please find Mr.".$name." details below:\n\n";
    $body.="Name: ".$name."\n";
    $body.="Family Name: ".$fname." \n";
    $body.="Age: ".$age." \n";
    $body.="Experience: ".$exp." \n";
    $body.="University: ".$university." \n";
    $body.="Phone: ".$phone." \n";
    
    mail("rajletsgetlost@gmail.com", $emailsubject,
      $body, "From:" . $name);
      header("location: thanks.php");
    it works fine without attachment...

    sendmail.php (file attachment part)

    Code:
    $fileatt =$_FILES['resume']['tmp_name']; // Path to the file
    $fileatt_type = "application/octet-stream"; // File Type
    $fileatt_name = $_FILES['resume']['name']; // Filename that will be used for the file as the attachment
    
    
    $file = fopen($fileatt,'rb');
    $data = fread($file,filesize($fileatt));
    fclose($file);
    
    
    $semi_rand = md5(time());
    $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
    
    $headers .= "\nMIME-Version: 1.0\n" .
    "Content-Type: multipart/mixed;\n" .
    " boundary=\"{$mime_boundary}\"";
    
    $body.= "This is a multi-part message in MIME format.\n\n" .
    "--{$mime_boundary}\n" .
    "Content-Type:text/html; charset=\"iso-8859-1\"\n" .
    "Content-Transfer-Encoding: 7bit\n\n" .
    $body. "\n\n";
    
    $data = chunk_split(base64_encode($data));
    
    $body.= "--{$mime_boundary}\n" .
    "Content-Type: {$fileatt_type};\n" .
    " name=\"{$fileatt_name}\"\n" .
    "Content-Disposition: attachment;\n" .
    " filename=\"{$fileatt_name}\"\n" .
    "Content-Transfer-Encoding: base64\n\n" .
    $data . "\n\n" .
    "--{$mime_boundary}--\n";
    in the file attachment i am getting error here in these two lines,

    Code:
    $data = fread($file,filesize($fileatt));
    fclose($file);
    fread(), fclose() not a valid arugument ...
    please help me to fix it...
  2. #2
  3. Devshed Beginner (1000 - 1499 posts)

    Join Date
    Jan 2004
    Location
    New Springfield, OH
    Posts
    1,236
    Rep Power
    1469
    Have you checked the file path to the document being uploaded? You php doesn't use any error checking/handling so it's going to be difficult to pinpoint without more information.

    What is the exact error? Is this being displayed in the browser or coming from the error log?

    You should really take the time to implement some error checking any time that you perform an operation that can fail. This will allow your site to fail gracefully.
    Don't like me? Click it.

    Scripting problems? Windows questions? Ask the Windows Guru!

    Stay up to date with all of my latest content. Follow me on Twitter!

    Help us help you! Post your exact error message with these easy tips!
  4. #3
  5. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,908
    Rep Power
    6351
    Niplo is right. The best way for this code to be debugged is for the original author to add error checking and handling into it.

    You could also trash your sendmail.php and use PHPMailer, which does file attachments more easily
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2013
    Posts
    16
    Rep Power
    0
    Originally Posted by Nilpo
    Have you checked the file path to the document being uploaded? You php doesn't use any error checking/handling so it's going to be difficult to pinpoint without more information.

    What is the exact error? Is this being displayed in the browser or coming from the error log?

    You should really take the time to implement some error checking any time that you perform an operation that can fail. This will allow your site to fail gracefully.
    i didn't get the attachment in the mail.. in the body of the mail i can get the form details only.. an also i am getting this..

    Code:
    "--{$mime_boundary}\n" .
    "Content-Type: {$fileatt_type};\n" .
    " name=\"{$fileatt_name}\"\n" .
    "Content-Disposition: attachment;\n" .
    " filename=\"{$fileatt_name}\"\n" .
    "Content-Transfer-Encoding: base64\n\n" .
    $data . "\n\n" .
    "--{$mime_boundary}--\n";
    the resume attachment not found......
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2013
    Posts
    16
    Rep Power
    0
    Originally Posted by ManiacDan
    Niplo is right. The best way for this code to be debugged is for the original author to add error checking and handling into it.

    You could also trash your sendmail.php and use PHPMailer, which does file attachments more easily
    i downloaded the script phpmailer.. but i dont how to proceed with it?

    Code:
    <html>
    <head>
    <title>PHPMailer - Sendmail advanced test</title>
    </head>
    <body>
    
    <?php
    
    require_once('../class.phpmailer.php');
    
    $mail = new PHPMailer(true); // the true param means it will throw exceptions on errors, which we need to catch
    $mail->IsSendmail(); // telling the class to use SendMail transport
    
    try {
      $mail->AddAddress('whoto@otherdomain.com', 'John Doe');
      $mail->SetFrom('name@yourdomain.com', 'First Last');
      $mail->AddReplyTo('name@yourdomain.com', 'First Last');
      $mail->Subject = 'PHPMailer Test Subject via mail(), advanced';
      $mail->AltBody = 'To view the message, please use an HTML compatible email viewer!'; // optional - MsgHTML will create an alternate automatically
      $mail->MsgHTML(file_get_contents('contents.html'));
      $mail->AddAttachment('images/phpmailer.gif');      // attachment
      $mail->AddAttachment('images/phpmailer_mini.gif'); // attachment
      $mail->Send();
      echo "Message Sent OK</p>\n";
    } catch (phpmailerException $e) {
      echo $e->errorMessage(); //Pretty error messages from PHPMailer
    } catch (Exception $e) {
      echo $e->getMessage(); //Boring error messages from anything else!
    }
    ?>
    
    </body>
    </html>
    in this code

    Code:
    $mail->AddAttachment('images/phpmailer.gif');      // attachment
      $mail->AddAttachment('images/phpmailer_mini.gif'); // attachment
    i don't understand this.. i have to attach a file in the form... that file should be added here... how to post the attachment file here???

    could you help me in connecting the form with this php mailer?????
  10. #6
  11. Devshed Beginner (1000 - 1499 posts)

    Join Date
    Jan 2004
    Location
    New Springfield, OH
    Posts
    1,236
    Rep Power
    1469
    Originally Posted by RomanticRaj
    i don't understand this.. i have to attach a file in the form... that file should be added here... how to post the attachment file here???
    If what you have is correct, you need this.
    PHP Code:
    $mail->AddAttachment($_FILES['resume']['tmp_name']); 
    Don't like me? Click it.

    Scripting problems? Windows questions? Ask the Windows Guru!

    Stay up to date with all of my latest content. Follow me on Twitter!

    Help us help you! Post your exact error message with these easy tips!
  12. #7
  13. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,908
    Rep Power
    6351
    Nilpo's right again. Are you sure your mailer is sendmail?
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.
  14. #8
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2013
    Posts
    16
    Rep Power
    0
    i fixed the problem.. its working now.. thank you for your help....
  16. #9
  17. Devshed Beginner (1000 - 1499 posts)

    Join Date
    Jan 2004
    Location
    New Springfield, OH
    Posts
    1,236
    Rep Power
    1469
    For the benefit of anyone else who comes across this thread, what did you do to make it work?
    Don't like me? Click it.

    Scripting problems? Windows questions? Ask the Windows Guru!

    Stay up to date with all of my latest content. Follow me on Twitter!

    Help us help you! Post your exact error message with these easy tips!
  18. #10
  19. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,908
    Rep Power
    6351
    Originally Posted by Nilpo
    For the benefit of anyone else who comes across this thread, what did you do to make it work?
    Any bets on the odds of RomanticRaj coming back to explain his mystery problem on his script which contained no error handling or anything to indicate he knew what was going on?
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.

IMN logo majestic logo threadwatch logo seochat tools logo