Send File Attachment from Form Using phpMailer and PHP

后端 未结 8 926
予麋鹿
予麋鹿 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: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.

提交回复
热议问题