Everybody! This is important. In a few days, these forums will be moving over to using the totally sweet Discourse platform. To ensure this migration happens smoothly with no loss of content, these forums are currently in a read-only mode. I do apologize for the inconvenience.

There is never a good time to turn the forums off for an extended period of time, but I promise the new forums will be a billion times better. I'm pretty sure of it.

See you all on the other side in a few days, and if you have any (non-technical) questions, please e-mail me at kirupa@kirupa.com. For technical questions, try to find a tutorial that corresponds to what you are looking for and post in the comments section of that page.

Cheers,
Kirupa

Results 1 to 2 of 2

Thread: Using PHP to Attach File to Email

  1. #1

    Using PHP to Attach File to Email

    I could use help from an experienced programmer - I have PHP code that I'm using with a registration form (that works fine, sending emails). I've added a file input field to the HTML form:

    Code:
    <form action="php/event_reg_attachment.php" enctype="multipart/form-data" method="post" name="EventRegistration" id="EventRegistration">
        <input type="file" name="Userfile" id="Userfile" name="Userfile" size="29" />
    </form>
    (I'm leaving out the extraneous details.)

    And the PHP code that processes things:
    Code:
    <?php
    
    include '../../includes/connect.php'; // Change as appropriate
    
    if(!empty($_POST['First']) || !empty($_POST['Last']) || !empty($_POST['Email']) || !empty($_POST['Street']))
    {
        $from = "webmaster@mydomain.com";
        $to = "admin@mydomain.com";
    
        $subject = "mydomain.com: " . stripslashes($_POST['EventName']) . " Event Registration";
        
        $body = stripslashes($_POST['EventName']) . " (" . stripslashes($_POST['EventDate']) . ") Registration\r\n\r\n";
        $body .= stripslashes($_POST['First']) . " " . $_POST['Last'] . "\r\n";
        $body .= stripslashes($_POST['Organization']) . "\r\n";
        $body .= stripslashes($_POST['Street']) . ", " . stripslashes($_POST['Suite']) . "\r\n";
        $body .= stripslashes($_POST['City']) . ", " . stripslashes($_POST['State']) . "\r\n";
        $body .= stripslashes($_POST['Country']). "\r\n";
        $body .= stripslashes($_POST['Zipcode']). "\r\n";
        $body .= "\r\n---------------------------\r\n\r\n";
        $body .= "Email:\r\n";
        $body .= stripslashes($_POST['Email']). "\r\n";
        $body .= "\r\n---------------------------\r\n\r\n";
        $body .= "Home Telephone:\r\n";
        $body .= stripslashes($_POST['HomePhone']). "\r\n";
        $body .= "Mobile Telephone:\r\n";
        $body .= stripslashes($_POST['MobilePhone']). "\r\n";
        $body .= "Fax:\r\n";
        $body .= stripslashes($_POST['Fax']). "\r\n";
        $body .= "\r\n---------------------------\r\n\r\n";
        $body .= "Level:\r\n";
        $body .= stripslashes($_POST['Level']). "\r\n";
        $body .= "Experience/Years:\r\n";
        $body .= stripslashes($_POST['Experience']). "\r\n";
        $body .= "\r\n---------------------------\r\n\r\n";
        $body .= "Interests:\r\n";
        $body .= stripslashes($_POST['Interests']);
        $body .= "\r\n---------------------------\r\n\r\n";
    
        $header = "From: " . $from . " <" . $from . ">\r\n";
        $header .= "Reply-To: " . $_POST['First'] . " " . $_POST['Last'] . " <" . $_POST['Email'] . ">\r\n";
        $header .= "Content-type: text/plain; charset=us-ascii\r\n";
        $header .= "Content-Transfer-Encoding:7bit\r\n";
        //$header .= "X-Mailer: PHP/" . phpversion() . "\r\n"; //GoDaddy Disallows This Header
        $header .= "X-Priority: 1";
        if(@mail($to, $subject, $body, $header))
        {
            header( 'Location: http://www.mydomain.com/thankyou.html' ) ;
            
        } else {
            print ('<font size=\"2\">Output Error: Please be sure to fill in all form fields.<br>');
            print("<a href=\"javascript:history.go(-1);\">Return to the form</a></font>");
        }
    } else {
            print ('<font size=\"2\">Error! Form variables not received. Please contact the website <a href=\"mailto:admin@mydomain.com\">administrator</a>.<br>');
            print("<a href=\"javascript:history.go(-1);\">Return to the form</a></font>");
    }
    
    ?>
    I need to modify it by adding file attachment capability. I've found some code that relates to the issue but I'm not familiar with the problems in dealing with a multipart message, so I'm not entirely sure how to combine the two scripts...

    Code:
       global $Userfile, $userfile_type, $userfile_name, $userfile_size;
      
       $mail_parts["Userfile"] = $_POST['Userfile'];
       $mail_parts["userfile_type"] = $userfile_type;
       $mail_parts["userfile_name"] = $userfile_name;
       $mail_parts["userfile_size"] = $userfile_size;
       
       if($userfile_size > 0)
       {
          $mail_boundary = md5(uniqid(time()));
          $header .= "MIME-Version: 1.0\r\n";
          $header .= "Content-type: multipart/mixed;
                                          boundary=\"$mail_boundary\"\r\n\r\n";
          $header .= "This is a multi-part message in MIME format.\r\n\r\n";
    
          $fp = fopen($Userfile, "r");
          $file = fread($fp, filesize($Userfile));
          $file = chunk_split(base64_encode($file));
    
          $body = "--$mail_boundary\r\n";
          $body .= "Content-type:text/plain;charset=$mail_charset\r\n";
          $body .= "Content-transfer-encoding:$mail_encoding\r\n\r\n";
          $body .= "$mail_body\r\n";
          $body .= "--$mail_boundary\r\n";
          if(!empty($userfile_type)) $mime_type = $userfile_type;
          else $mime_type = "application/octet-stream";
    
          $body .= "Content-type:$mime_type;name=$userfile_name\r\n";
          $body .= "Content-transfer-encoding:base64\r\n\r\n";
          $body .= $file . "\r\n\r\n";
          $body .= "--$mail_boundary--";
          $mail_body = $body;
       }
    Thanks for any help.
    Last edited by Pherank; July 24th, 2009 at 10:03 PM.

  2. #2

    You can never underestimate the stupidity of the general public.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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

Home About kirupa.com Meet the Moderators Advertise

 Link to Us

 Credits

Copyright 1999 - 2012