4
I need to send several attachments via Phpmailer and am using jQuery to get the value of input file
, now I need to pick up the file path to be able to send by email, but when I take the path it comes with C:\fakepath\image.jpg
or only image.jpg
, I’ve been doing research 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 button addAnexos
he will open the input file
to pick up the file and put the path information in the textarea
, the tmppath
comes from an idea I picked up here, which takes the temporary file path, but this path only works on Google Chrome and Mozilla.
Why do you need to know path to send the file? The intention is to send the file and this HTML does for you. If you want to use JS to automate, ok, but the path is not necessary.
– Maniero
biggown, how to send attachments without the need only with html ? I have this doubt, because I pass in the function sendEmail the parameter File, which contains a string with the path.
– Gabriel Rodrigues
What a function
enviarEmail
? I don’t see any.– Maniero
You have to take a look at the file field, after understanding how the file upload works, you will get the physical path of the file to send it as an attachment
– Dexxtz