Sending attachment in Phpmailer email

Asked

Viewed 713 times

0

I have a problem in this code, because when sending is done sending the information I receive the return = 1, however the attachment is not sent someone can help me?

SENDING.PHP

    $arquivoreal = $_FILES['arquivo'];
    $Nome = $_POST['Nome'];
    $CPF = $_POST['CPF'];

    //Configurações do email, ajustar conforme necessidade
    //==================================================== 
    $email_destinatario ="[email protected]"; // pode ser qualquer email que receberá as mensagens
    $email_reply = "Supervisão"; 
    $email_assunto = "Curriculo"; // Este será o assunto da mensagem
    //====================================================

    //Monta o Corpo da Mensagem
    //====================================================
    $email_conteudo = "<h1>Envio de Curriculo [SITE]</h1>
                        <p>$Nome CPF: $CPF gostaria de fazer parte do nosso quadro de funcionarios, por isso enviou o curriculo pelo site <br> 
                        (Curriculo em anexo)</p>";
    //====================================================

    //Enviando o email 
    //==================================================== 
        // EnviaEmailCorandini(assunto,msg corpo do email,email,Nome Destinatario,Arquivo em anexo);
    $retorno = EnviaEmailCorandini($email_assunto,$email_conteudo,$email_destinatario,$email_reply,$arquivoreal);


    if($retorno == 1){
        header("Location:index.php");
    }else{
            echo "Falha ao enviar o email";
    }
}
}

Phpmailer/index.php

 require 'PHPMailerAutoload.php';

 function EnviaEmail($assunto,$msg,$EmailDestino,$NomeDestino,$NomeArquivo){

    $mail = new PHPMailer();
    $mail->setLanguage('pt');

    $mail->isSMTP();
    $mail->Host         = $host;
    $mail->SMTPAuth     = true;
    $mail->Username     = $user;
    $mail->Password     = $password;
    $mail->Port         = $port;
    $mail->SMTPSecure   = $secure;

    $mail->From     = $from;
    $mail->FromName = $fromName;
    $mail->addReplyTo($from,$fromName);

    $mail->addAddress($EmailDestino,$NomeDestino);
    $mail->isHTML(true);
    $mail->CharSet      = 'utf-8';
    $mail->WordWrap     = 70;
    $mail->Subject      =  $assunto;
    $mail->Body         = $msg;
    $mail->AddAttachment($NomeArquivo);           
    $send = $mail->Send();

    if($send)
        return 1;
    else
        return 0;
    }

  • 1

    It is returning one because in fact the email has been sent. From an echo in this $filename and try to access the file through the browser. Remember that for phpmailer to attach the absolute file path is required.

  • The issue is confusing, you use the Enviaemailcorandini class but you paste the code from the Enviaemail class. There you have the last parameter $Filename but it is sent an Array $arquivoreal = $_FILES['file'], if you want only the name of the file would be $_FILES['file']['tmp_name'], had not searched the manual: http://php.net/manualpt_BR/features.file-uploadpost-method.php

1 answer

0


By code description you need to send the file name by changing the following line:

$arquivoreal = $_FILES['arquivo']['tmp_name'];

Only that so the annex goes with the temporary name.

You can correct in two ways:

  1. Move the file to a temporary location with the correct name:

    $tmp_dir = $path_parts = pathinfo($_FILES['tmp_name']); $arquivoreal = $tmp_dir['dirname'] . '/' . $_FILES['name']; move_uploaded_file( $_FILES['tmp_name'], $arquivoreal);

After sending you need to remove the file:

$retorno = EnviaEmailCorandini(...);
unlink($arquivoreal);
  1. Or Send class, make use of the second parameter of the Addattachment() method as below, but for this you will need to change the function call:

    // change the call
    $return = Enviaemailcorandini($email_subject,$email_content,$email_destinatario,$email_reply, $_FILES['file']['tmp_name'], $_FILES['file']['name']);

    // change the function definition
    Function Enviaemail($subject,$msg,$Emaildestino){

    // amend the inclusion of the Annex $mail->Addattachment($Filename, $Filename);

  • So, I did it this way and it worked, but it goes as file. tmp how could I do to send the file in the same extension and name in which it was "posted"

  • @Matheussouza, I edited the answer to contemplate the change of name of the annex.

Browser other questions tagged

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