Send e-mail with attachment in PHP

Asked

Viewed 85 times

0

Hello, I’m using the following code:

 <!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 ($_POST['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>

And I’m afraid the following mistake:

( ! ) Notice: Undefined index: ? acao in C: wamp www EMAILL email.php on line 33 Call Stack

Time Memory Function Location

1 0.0050 390248 {main}( ) ... email.php:0

I really don’t understand why, I already gave a study in the code but I can not understand the error. Someone help me, please!

1 answer

0

The problem is simpler than I imagined, the moment the page is first loaded the request variables $_POST, $_GET and $_REQUEST are present but have no defined values. Hence a direct check to $_POST['acao'] == 'enviar' will generate error for this reason should be done before checking isset($_POST["acao"]).

Also remove the query string, ?acao=enviar attribute action of <form> and send the query as input type='hidden':

 <!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="" enctype="multipart/form-data">
   <input type="hidden" name="acao" value="enviar" />
   <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 (isset($_POST["acao"]) && $_POST['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>
  • Good morning! I tried to make the change but got no results, it keeps returning the same error messages.

  • Only one, no <form id="Form1" name="Form1" method="post" action="? acao=enviar" enctype="Multipart/form-data"

  • This piece I posted is all my code.

  • The same mistake :/

  • Keeps returning the same error: Notice: Undefined index: acao in C: wamp www EMAILL attachment.php on line 33

  • All code in php?

  • the error does not persist

  • Can you recommend some code that has the same function? I really need it for a school project.

  • Yes, exactly

  • Excuse me, the fault is mine! I should have realized this mistake just by hitting the eye. I deleted the comments because it lists a long discussion that no longer makes sense.

Show 5 more comments

Browser other questions tagged

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