Send email attaching a local file

Asked

Viewed 1,436 times

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.

2 answers

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.

  • 1

    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?

  • Any file you want to use.

  • 1

    I’m making the changes to test and I get a question, no reference to the door, that’s right?

  • 1

    For regular sending no, but with authentication already accurate.

  • 1

    Can you vote for your answer?

  • 1

    It has, on the left side of it, up arrow (good, gives reputation), down arrow (bad, take reputation). Additionally, visa = mark as correct answer.

  • I recommend the use of require_once 'PHPMailerAutoload.php' instead of require_once 'class.phpmailer.php'. :p

  • @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 email Reply-to:? Please be clear

  • I made the suggested change and still nothing.

  • 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 Your file has to be on the first line <?php and on the last line ?>. It should also have the extension .php.

  • <HTML> <HEAD> <TITLE>First example</TITLE> </HEAD> <BODY>

  • <HTML> <HEAD> <TITLE>First example</TITLE> </HEAD> <BODY> <?php Here is the code ? > </BODY> </HTML>

  • 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

  • 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

  • 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

  • The script runs and appears sent email but nothing is sent to the registered email.

  • @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 message Failed opening required, corrects the path used in require_once and probably the script will work.

Show 13 more comments

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

You are not signed in. Login or sign up in order to post.