Sending attachment in email PHPMailer [closed]

0

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

ENVIO.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;
    }
    
asked by anonymous 31.07.2017 / 16:26

1 answer

0

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

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

This is how the attachment goes with the temporary name.

You can fix it in two ways:

  • Move the file to some temporary location with the correct name:

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

  • After sending, you need to remove the file:

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

    // change the call
    $ return = SendEmailCorandini ($ email_assign, $ email_contact, $ email_destination, $ email_reply, $ _FILES ['file'] ['tmp_name'], $ _FILES ['file'] ['name']);

    // change function definition
    function SendEmail ($ subject, $ msg, $ EmailDestino, $ DestinationName, $ FileName, $ FileDateName) {

    // change attachment addition $ mail-> AddAttachment ($ FileName, $ FileName);

  • 31.07.2017 / 19:09