Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New Coder
    Join Date
    Nov 2007
    Posts
    41
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Question Sending HTML Form w/ Attachments

    Hey there!

    I have a form that I want to have an attachment box, much like an email. However, the form is in HTML and despite my efforts, I can't get the HTML and attachment to send at the same time.

    All right, when I wrote this before, I said I didn't think PHPMailer would do the trick. But I managed to get an HTML email to send in it, with an attachment, but the attachment is always just a couple of bytes and not the real thing. The attachment should be loaded from a field where a user browses to a file on their computer. (Like C:\Blah.jpg)

    It gets the name of the file correctly, but that's about it.

    Do these files have to be uploaded somewhere? Or does it have to do with the encoding? I have it set to "application/octet-stream"

    Any advice? I'm an intermediate beginner to PHP.

    Thanks

    My code:
    PHP Code:
    $mail = new PHPMailer();
    $mail->SMTPAuth false;
    $mail->AddAddress("thisismy@emailaddress.com");
    $mail->From $qq_emailv//variable of user-inputed email
    $mail->FromName $from//variable of user's name
    $mail->Subject "Welcome!";
    $mail->IsHTML(true);
    $mail->Body "<b>This is a</b> <i>test</i> <small>of the PHP</small><h1>Mailer</h1> {$attach}";
    $attach stripslashes($attach); // $attach is declared earlier from the POST array
    $mail->AddStringAttachment($attach,$attach,"base64","application/octet-stream"); 
    Last edited by sakka; 12-21-2007 at 01:00 AM.

  • #2
    New Coder
    Join Date
    Nov 2007
    Posts
    41
    Thanks
    6
    Thanked 0 Times in 0 Posts
    Bump...I added more info! Any thoughts? Thanks guys...!

  • #3
    Regular Coder
    Join Date
    Jan 2003
    Posts
    867
    Thanks
    4
    Thanked 8 Times in 8 Posts
    With PHPMailer you need to allow them to upload the files to the webserver and then you can attach it to the email before sending. They can't send directly from their hard drive.

  • #4
    New Coder
    Join Date
    Nov 2007
    Posts
    41
    Thanks
    6
    Thanked 0 Times in 0 Posts
    I'm such a dork I got an attachment to send WITH an HTML. Thank you for the tip, arnyinc.

    Is it possible to send more than 2 attachments, however, in PHP Mailer?
    Last edited by sakka; 12-21-2007 at 07:23 PM.

  • #5
    New Coder
    Join Date
    Nov 2007
    Posts
    41
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Question

    K, ignore that last post - for some reason, the attachments decided not to send again! And I didn't even fiddle with the script!

    Here is my script. I apologize if it seems very unsecure. I am planning to add much more security (limiting file sizes and types) once I have the attachment basics down.

    If anyone could look over this and tell me what's going on... my attachments again aren't sending. They worked for a little bit, but that's it. I can see the file is uploading though into the correct directory.

    PHP Code:
    <?
    require("class.phpmailer.php");

    if (isset(
    $_POST['submit']) && $_POST['submit'] == "Submit"
    {
        
    $qq_name false;
        
    $qq_emailv false;

    //Check If Empty Form
        //Firstname
        
    if ( empty( $_POST['qq_name'] ) )            {
            echo 
    '<br><font color="red">ERROR: Please enter a First Name</font>'; } 
            else {
            
    $qq_name true;    }
        
          
    //Email
        
    if ( empty( $_POST['qq_email'] ) )        {
              echo 
    '<br><font color="red">ERROR: Please enter a Email</font>';        }
            else        {
                
    //Valid Email
                
    if(!preg_match('/^[a-z0-9&\'\.\-_\+]+@[a-z0-9\-]+\.([a-z0-9\-]+\.)*+[a-z]{2}/is'$_POST['qq_email']))            {
                echo 
    '<br><font color="red">ERROR: Please enter a valid Email</font>';            }
                else            {
                
    $qq_emailv $_POST['qq_email'];            }
            }
            

    // If - 2
    if ($qq_name && $qq_emailv) {

    $from $_POST['qq_name'];

    ///Uploading Attachments Script///
    $target_path "./uploads/";

    /* Add original filename to our target path.
    Result is 'uploads/filename.extension' */
    $target_path $target_path basename$_FILES['uploadedfile']['name']);

    ////////////////Send Mail//////////////////

    $mail = new PHPMailer();
    $mail->SMTPAuth false;
    $mail->AddAddress("sara@sungrafx.net");
    $mail->From $qq_emailv;
    $mail->FromName $from;
    $mail->Subject "MLI, INC - QUICK QUOTE FORM";
    $mail->IsHTML(true);
    $mail->Body ' (The HTML form is here and submits correctly. I'm removing it now because it's GIGANTIC!) ';
    $attach $_FILES['uploadedfile']['name'];
    $mail->AddAttachment($target_path);


    // Send to Mail
    if(!$mail->Send()) {
      echo 
    'Failed to send mail. Please contact the website administrator.';
    } else {
        if(
    move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
        echo 
    'Mail sent with attachment:' $target_path '.';
        
    $mail->ClearAttachments();    
    }      

    }
    }
    }
    ?>

  • #6
    Regular Coder
    Join Date
    May 2005
    Posts
    563
    Thanks
    0
    Thanked 3 Times in 3 Posts
    It looks like you are moving the file to the temp location on your server after you send the mail.

    That might be why you can see the file after but it doesnt send.

    move the line

    PHP Code:
    move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path); 
    out of the if statement and place it above this:

    PHP Code:
    $attach $_FILES['uploadedfile']['name']; 
    $mail->AddAttachment($target_path); 
    You might also want to place checks to see if there is a file uploaded, at the moment it will try to send attachments when there are none.

  • Users who have thanked SeeIT Solutions for this post:

    sakka (01-07-2008)

  • #7
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,647
    Thanks
    2
    Thanked 406 Times in 398 Posts
    Are you saving the attachments for a reason? You shouldn't need to even move them unless you intend to save them.
    PHP Code:
    $mail->AddAttachment($_FILES['uploadedfile']['tmp_name']); 


  •  

    Posting Permissions

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