0
I need a PHP script that, when running, automatically sends a particular local file attached to an email.
It is not a web form completed by the user. It is automatic sending when the script is executed whenever someone opens the page.
0
I need a PHP script that, when running, automatically sends a particular local file attached to an email.
It is not a web form completed by the user. It is automatic sending when the script is executed whenever someone opens the page.
5
You can use the class Phpmailer to facilitate the sending of the email.
Follow an example of how you can achieve what you want:
// Incluir a classe no teu ficheiro
require_once '/caminho/para/class.phpmailer.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 = '/caminho/completo/para/ficheiro/';
$ficheiro = 'anexo.pdf';
$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();
}
The code above can be stored inside a PHP file, for example enviaMail.php
, file that whenever called or included in another will trigger the sending of the email.
0
PHP has a native function called mail, it is here in the manual http://php.net/manual/en/function.mail.php , a small detail is that both to use the Phpmailer class (already mentioned above) and the native mail function you need to have an email server on your machine, otherwise you will have an error as a response.
Browser other questions tagged php email
You are not signed in. Login or sign up in order to post.
Very good! I’ll test it. I imagined something like this, but without knowledge I had no way, so I ask you if it works only with PDF or with any file?
– Luciano
Any file you want to use.
– Zuul
I’m making the changes to test and I get a question, no reference to the door, that’s right?
– Luciano
For regular sending no, but with authentication already accurate.
– Zuul
Can you vote for your answer?
– Luciano
It has, on the left side of it, up arrow (good, gives reputation), down arrow (bad, take reputation). Additionally, visa = mark as correct answer.
– Zuul
I recommend the use of
require_once 'PHPMailerAutoload.php'
instead ofrequire_once 'class.phpmailer.php'
. :p– Eduardo Silva
@Luciano you said that
$mail->AddReplyTo('[email protected]', 'Meu Nome')
does not run, can explain exactly how "does not run", emits some error, the script hangs at this point, is not being added to the emailReply-to:
? Please be clear– Guilherme Nascimento
I made the suggested change and still nothing.
– Luciano
From $mail->Addreplyto('[email protected]', 'My Name') the page shows as if you have not processed the commands, just showing them as page text.
– Luciano
@Luciano Your file has to be on the first line
<?php
and on the last line?>
. It should also have the extension.php
.– Zuul
<HTML> <HEAD> <TITLE>First example</TITLE> </HEAD> <BODY>
– Luciano
<HTML> <HEAD> <TITLE>First example</TITLE> </HEAD> <BODY> <?php Here is the code ? > </BODY> </HTML>
– Luciano
Warning: require_once(): http://wrapper is disabled in the server Configuration by allow_url_include=0 in C: xampp htdocs envio_de_arquivo_automatico PHP_ENVIAR_ARQUIVO_POR_EMAIL aalgarismo.php on line 5
– Luciano
Warning: require_once(http://localhost/envio_de_arquivo_automatico/PHP SEND FILE BY EMAIL/PHPMAILER/Phpmailer-master/Phpmailer-master/class.phpmailer.php): failed to open stream: no suitable wrapper could be found in C:xampp htdocs envio_de_arquivo_automatico PHP_ENVIAR_ARQUIVO_POR_EMAIL aalgarismo.php on line 5
– Luciano
Fatal error: require_once(): Failed Opening required 'http://localhost/envio_de_arquivo_automatico/PHP SEND FILE BY EMAIL/PHPMAILER/Phpmailer-master/Phpmailer-master/class.phpmailer.php' (include_path='.;C: xampp php PEAR') in C: xampp htdocs envio_de_arquivo_automatico PHP_ENVIAR_ARQUIVO_POR_EMAIL aalgarismo.php on line 5
– Luciano
The script runs and appears sent email but nothing is sent to the registered email.
– Luciano
@Luciano The error is very basic, I will added an included file
class.phpmailer.php
wrong way, he’s pointing to the wrong place, that’s why the messageFailed opening required
, corrects the path used inrequire_once
and probably the script will work.– Guilherme Nascimento