I need to send multiple attachments via PHPmailer and I'm using jQuery to get the value of input file
, now I need to get the file path to be able to send it by email, however when I get the path it comes with C:\fakepath\image.jpg
or only image.jpg
, I've been searching and this is related to browser security, so how do you send attachments? the schematic I'm doing and the following:
HTML
<div class="divAnexos">
<label for="anexos" class="label">Anexos:</label>
<input type="file" id="pegarAnexo" multiple>
<textarea id="anexos"></textarea>
<button id="addAnexos" class="ui-state-default"> Anexar </button>
</div>
JS
document.getElementById('addAnexos').onclick = function () {
document.getElementById('pegarAnexo').click();
};
$('#pegarAnexo').change(function (event) {
tmppath = URL.createObjectURL(event.target.files[0]);
console.log(tmppath);
$('#anexos').html($(this).val());
});
PHP
function enviarEmail($aUser, $aPass, $aPort, $aDestinatario, $aHost, $aAssunto, $aCorpo, $aArquivos = '', $aCopia = '') {
$mail = new PHPMailer;
$mail->isSMTP();
$mail->CharSet = 'UTF-8';
$mail->Host = $aHost;
$mail->Port = $aPort;
$mail->SMTPSecure = 'tls';
$mail->SMTPAuth = true;
$mail->Username = $aUser;
$mail->Password = $aPass;
$mail->setFrom($aUser);
$mail->addAddress($aDestinatario);
$mail->addAddress($aCopia);
$mail->Subject = $aAssunto;
$mail->Body = $aCorpo;
$mail->addAttachment($aArquivos);
if (!$mail->send()) {
return false;
} else {
return true;
}
}
The algorithm does the following, when you click the addAnexos
button it will open input file
to get the file and put the path information in textarea
, tmppath
comes from an idea that I took here , which takes the temporary path of the file, but this path only works in Google Chrome and Mozilla.