Send File Attachment from Form Using phpMailer and PHP
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">
Try:
if (isset($_FILES['uploaded_file'])
&& $_FILES['uploaded_file']['error'] == UPLOAD_ERR_OK
) {
$mail->addAttachment($_FILES['uploaded_file']['tmp_name'],
$_FILES['uploaded_file']['name']);
}
A basic example attaching multiple file uploads can be found here.
The function definition for addAttachment
is:
/**
* Add an attachment from a path on the filesystem.
* Never use a user-supplied path to a file!
* Returns false if the file could not be found or read.
* Explicitly *does not* support passing URLs; PHPMailer is not an HTTP client.
* If you need to do that, fetch the resource yourself and pass it in via a local file or string.
*
* @param string $path Path to the attachment
* @param string $name Overrides the attachment name
* @param string $encoding File encoding (see $Encoding)
* @param string $type MIME type, e.g. `image/jpeg`; determined automatically from $path if not specified
* @param string $disposition Disposition to use
*
* @throws Exception
*
* @return bool
*/
public function addAttachment(
$path,
$name = '',
$encoding = self::ENCODING_BASE64,
$type = '',
$disposition = 'attachment'
)