Attachment submission with PHPMailer

2

I'm using PHPMailer to send attachments via contact form.

Form front end:

<form action="mail.php" method="POST" enctype="multipart/form-data">
<input type="hidden" name="tipo" value="trabalhe-conosco">
<input class="input_100 require" type="text" name="nome" placeholder="Digite o seu nome">
<input class="input_49_left require email" type="text" name="email" placeholder="Digite o seu e-mail">
<input class="input_49_right require fone" type="text" name="telefone" placeholder="Telefone">
<select class="input_49_left require" name="cargo">
    <option value="">Cargo</option>
    <option value="Comercial">Comercial</option>
    <option value="Administrativo">Administrativo</option>
</select>
<span class="btn btn-default btn-file">
    <input type="file" name="curriculo" class="upload" value="">
</span>
<textarea class="input_100 require" name="mensagem" cols="30" rows="10" placeholder="Mensagem"></textarea>

<button type="submit" formmethod="POST" class="bt-enviar">Enviar</button>

mail.php (form action)

<?php

    $GetPost = filter_input_array(INPUT_POST,FILTER_DEFAULT);

    $Erro      = true;
    $Tipo      = $GetPost['tipo'];
    $Nome      = $GetPost['nome'];
    $Email     = $GetPost['email'];
    $Telefone  = $GetPost['telefone'];
    $Cargo     = $GetPost['cargo'];
    $Curriculo = $_FILES['curriculo'];
    $Mensagem  = $GetPost['mensagem'];

    include_once 'PHPMailer/class.smtp.php';
    include_once 'PHPMailer/class.phpmailer.php';

    $Mailer = new PHPMailer;
    $Mailer->CharSet = "utf8";
    $Mailer->SMTPDebug = 3;

    $Mailer->FromName = "Jogo Digital";
    $Mailer->From = "[email protected]";
    $Mailer->AddAddress("[email protected]");
    $Mailer->IsHTML(true);
    $Mailer->Subject = "Novo currículo \"Trabalhe Conosco\" - {$Nome}";
    $Mailer->AddAttachment($Curriculo);
    $Mailer->Body = "
    Novo currículo enviado por {$Nome}<br />
    Tipo: {$Tipo}<br /><br>
    <b>Dados:</b><br /><br>
    Email: {$Email}<br>
    Telefone: {$Telefone}<br>
    Cargo: {$Cargo}<br>
    Mensagem: {$Mensagem}<br>
    Curriculo: {$Curriculo['name']}
    ";

    if($Mailer->Send()){
        $Erro = false;
    }

    var_dump($Erro);

When sending the attachment does not arrive, and gives the following message: 2017-08-16 19:11:14 Could not access file: Array.

In the body of the email $ Curriculo ['name'] is returning the file name. What should I adapt in the code to work?

    
asked by anonymous 16.08.2017 / 22:51

2 answers

2

Try this:

if (isset($_FILES['uploaded_file']) &&
    $_FILES['uploaded_file']['error'] == UPLOAD_ERR_OK) {
    $mail->AddAttachment($_FILES['uploaded_file']['tmp_name'],
                         $_FILES['uploaded_file']['name']);
}

A basic example can be found here .

The declaration for the AddAttachment function is:

public function AddAttachment($path,
                              $name = '',
                              $encoding = 'base64',
                              $type = 'application/octet-stream')
    
17.08.2017 / 03:14
2

This example is what I always use when I need it and has the sending with functional attachments, I hope it helps you

<!DOCTYPE html>
<html lang="pt-br">
<head>
    <meta charset="UTF-8">
    <title>Enviar e-mail com anexo</title>
</head>
<body>
<form id="form1" name="form1" method="post" action="?acao=enviar" enctype="multipart/form-data">
   <table width="500" border="0" align="center" cellpadding="0" cellspacing="2">
   <tr>
     <td align="right">Nome:</td>
     <td><input type="text" name="nome" id="nome" /></td>
   </tr>
   <tr>
     <td align="right">Assunto:</td>
     <td><input type="text" name="assunto" id="assunto" /></td>
   </tr>
   <tr>
     <td align="right">Mensagem:</td>
     <td><textarea name="mensagem" id="mensagem" cols="45" rows="5"></textarea></td>
   </tr>
   <tr>
     <td align="right">Anexo:</td>
     <td><input type="file" id="arquivo" name="arquivo" /></td>
   </tr>
   <tr>
     <td colspan="2" align="center"><input type="submit" value="Enviar" /></td>
   </tr>
   </table>
</form>

<?php
if($_GET['acao'] == 'enviar'){
 $nome      = $_POST['nome'];
 $assunto   = $_POST['assunto'];
 $mensagem  = $_POST['mensagem'];
 $arquivo   = $_FILES["arquivo"];

 $corpoMSG = "<strong>Nome:</strong> $nome<br> <strong>Mensagem:</strong> $mensagem";
 // chamada da classe       
 require_once('class.phpmailer.php');
 // instanciando a classe
 $mail   = new PHPMailer();
 // email do remetente
 $mail->SetFrom('[email protected]', 'remetente');
 // email do destinatario
 $address = "[email protected]";
 $mail->AddAddress($address, "destinatario");
 // assunto da mensagem
 $mail->Subject = $assunto;
 // corpo da mensagem
 $mail->MsgHTML($corpoMSG);
 // anexar arquivo
 $mail->AddAttachment($arquivo['tmp_name'], $arquivo['name']  );

 if(!$mail->Send()) {
   echo "Erro: " . $mail->ErrorInfo;
  } else {
   echo "Mensagem enviada com sucesso!";
  }
}
?>
</body>
</html>
    
16.08.2017 / 23:18