0
I want to complete this code to send a file whenever a given page opens.
<?php
// Incluir a classe no teu ficheiro
require_once 'PHPMailerAutoload.php';
require_once 'class.phpmailer.php';
require_once 'class.smtp.php';
// Instanciar a classe para envio de email
$mail = new PHPMailer(true);
// Vamos tentar realizar o envio
try {
// Remetente
$mail->AddReplyTo('[email protected]', 'Meu Nome');
$mail->SetFrom('[email protected]', 'Meu Nome');
// Destinatário
$mail->AddAddress('[email protected]', 'Destinatário');
// Assunto
$mail->Subject = 'Segue ficheiro anexo com XPTO';
// Mensagem para clientes de email sem suporte a HTML
$mail->AltBody = 'Em anexo o ficheiro com XPTO.';
// Mensagem para clientes de email com suporte a HTML
$mail->MsgHTML('<p>Em anexo o ficheiro com XPTO.</p>');
// Adicionar anexo
$caminho = 'C:/xampp/htdocs/envio_de_arquivo_automatico/PHP_ENVIAR_ARQUIVO_POR_EMAIL/PHPMAILER/PHPMailer-master/PHPMailer-master/';
$ficheiro = 'Documento2.docx';
$mail->AddAttachment($caminho.$ficheiro);
// Enviar email
$mail->Send();
echo "Mensagem enviada!";
}
catch (phpmailerException $e) {
// Mensagens de erro do PHPMailer
echo $e->errorMessage();
}
catch (Exception $e) {
// Outras mensagens de erro
echo $e->getMessage();
}
?>
Email is sent and attachment does not appear? or email is not sent? Use this``` ` bar to specify the folders.
– rray
Email does not get sent. And by changing / by error: syntax error, unexpected 'Document2'
– Luciano
I don’t know if it makes a difference but I’m running on LOCALHOST.
– Luciano
Email server is configured? maybe that question help
– rray
I believe so, but I keep checking if I haven’t forgotten anything.
– Luciano
Other than that only the same bar, either you use double quotes or it escapes so
C:\\pasta\\arquivo.txt
– rray
Thanks for the help but it hasn’t worked out yet.
– Luciano
If there is no error message anywhere in PHP (including Apache error logs), it is likely that your SMTP server is misconfigured. I agree with @rray on the
/
: you must replace them with\\
, because it is on the Windows platform.– Vinícius Gobbo A. de Oliveira
Fought people I got, actually I changed the code a little.
– Luciano
$host = 'smtp.live.com'; $username = '[email protected]'; $password = 'PASSWORD'; $port = 587; $Secure = 'tls'; $from = $username; $fromName = 'My Name';
– Luciano
$mail->From = $from; $mail->Fromname = $fromName; $mail->isSMTP(); $mail->Host = $host; $mail->Smtpauth = true; $mail->Username = $username; $mail->Password = $password; $mail->Port = $port; $mail->Smtpsecure = $Secure;
– Luciano
// Add attachment $path = 'C: xampp htdocs envio_de_arquivo_automatico PHP_ENVIAR_ARQUIVO_POR_EMAIL PHPMAILER Phpmailer-master Phpmailer-master ; $file = 'Document2.docx';
– Luciano