Send File Attachment from Form Using phpMailer and PHP

后端 未结 8 944
予麋鹿
予麋鹿 2020-11-22 14:09

I have a form on example.com/contact-us.php that looks like this (simplified):

相关标签:
8条回答
  • 2020-11-22 14:41

    This code help me in Attachment sending....

    $mail->AddAttachment($_FILES['file']['tmp_name'], $_FILES['file']['name']);
    

    Replace your AddAttachment(...) Code with above code

    0 讨论(0)
  • 2020-11-22 14:50

    This will work perfectly

        <form method='post' enctype="multipart/form-data">
        <input type='file' name='uploaded_file' id='uploaded_file' multiple='multiple' />
        <input type='submit' name='upload'/> 
        </form>
    
        <?php
               if(isset($_POST['upload']))
            {
                 if (isset($_FILES['uploaded_file']) && $_FILES['uploaded_file']['error'] == UPLOAD_ERR_OK)
                    {
                         if (array_key_exists('uploaded_file', $_FILES))
                            { 
                                $mail->Subject = "My Subject";
                                $mail->Body = 'This is the body';
                                $uploadfile = tempnam(sys_get_temp_dir(), sha1($_FILES['uploaded_file']['name']));
                            if (move_uploaded_file($_FILES['uploaded_file']['tmp_name'], $uploadfile))
                                 $mail->addAttachment($uploadfile,$_FILES['uploaded_file']['name']); 
                                $mail->send();
                                echo 'Message has been sent';
                            }       
                    else
                        echo "The file is not uploaded. please try again.";
                    }
                    else
                        echo "The file is not uploaded. please try again";
            }
        ?>
    
    0 讨论(0)
  • 2020-11-22 14:51

    Try:

    if (isset($_FILES['uploaded_file']) &&
        $_FILES['uploaded_file']['error'] == UPLOAD_ERR_OK) {
        $mail->AddAttachment($_FILES['uploaded_file']['tmp_name'],
                             $_FILES['uploaded_file']['name']);
    }
    

    Basic example can also be found here.

    The function definition for AddAttachment is:

    public function AddAttachment($path,
                                  $name = '',
                                  $encoding = 'base64',
                                  $type = 'application/octet-stream')
    
    0 讨论(0)
  • 2020-11-22 14:54

    File could not be Attached from client PC (upload)

    In the HTML form I have not added following line, so no attachment was going:

    enctype="multipart/form-data"

    After adding above line in form (as below), the attachment went perfect.

    <form id="form1" name="form1" method="post" action="form_phpm_mailer.php"  enctype="multipart/form-data">
    
    0 讨论(0)
  • 2020-11-22 14:55

    You'd use $_FILES['uploaded_file']['tmp_name'], which is the path where PHP stored the uploaded file (it's a temporary file, removed automatically by PHP when the script ends, unless you've moved/copied it elsewhere).

    Assuming your client-side form and server-side upload settings are correct, there's nothing you have to do to "pull in" the upload. It'll just magically be available in that tmp_name path.

    Note that you WILL have to validate that the upload actually succeeded, e.g.

    if ($_FILES['uploaded_file']['error'] === UPLOAD_ERR_OK) {
        ... attach file to email ...
    }
    

    Otherwise you may try to do an attachment with a damaged/partial/non-existent file.

    0 讨论(0)
  • 2020-11-22 15:00

    In my own case, i was using serialize() on the form, Hence the files were not being sent to php. If you are using jquery, use FormData(). For example

    <form id='form'>
    <input type='file' name='file' />
    <input type='submit' />
    </form>
    

    Using jquery,

    $('#form').submit(function (e) {
    e.preventDefault();
    var formData = new FormData(this); // grab all form contents including files
    //you can then use formData and pass to ajax
    
    });
    
    0 讨论(0)
提交回复
热议问题