Sending attachment with Phpmailer

Asked

Viewed 8,261 times

4

I am using Phpmailer to send attachments via contact form.

Front end of the Form:

<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 does not arrive the attachment, and gives the following message: 2017-08-16 19:11:14 Could not access file: array.

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

  • 1

    I haven’t used this for a long time, but as I recall you need to upload the file, and point the way to ->Addatachment() instead of this $Resume, then just delete the file so it doesn’t take up space for nothing.

2 answers

4


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 function AddAttachment is:

public function AddAttachment($path,
                              $name = '',
                              $encoding = 'base64',
                              $type = 'application/octet-stream')

3

This example is what I always use when I need 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>

Browser other questions tagged

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