e-mail

Asked

Viewed 675 times

1

Hello, my code sends the email to my email box, but my "Alert" does not appear when I click send

Another problem is that in my inbox the email of the sender that appears is not the email of the field that was filled in the form (appears the email that appears is the server).

It is correct the sender’s email that appears in the server’s inbox ?

If not, can you change to receive the e-mail that was filled? And I also could not get my attachment sent.

<?php


$arquivo = isset($_FILES["arquivo"]) ? $_FILES["arquivo"] : FALSE;
$headers = "Content-type: text/plain; charset=utf-8\r\n";
$nome = $_POST['nome'];
$assunto = $_POST['assunto'];
$email_from = $_POST['email'];
$email = "[email protected]";
$mensagem = "<br>  <strong>Mensagem: </strong>".$_POST['texto'];

if(file_exists($arquivo["tmp_name"]) and !empty($arquivo))
{
$fp = fopen($_FILES["arquivo"]["tmp_name"],"rb");
$anexo = fread($fp,filesize($_FILES["arquivo"]["tmp_name"]));
$anexo .= base64_encode($anexo);
fclose($fp);

$anexo .= chunk_split($anexo);
$boundary = "XYZ-" . date("d-m-Y-i:s") . "-ZYX";
$mens = "--$boundary\n";
$mens .= "Content-Transfer-Encoding: 8bits\n";
$mens .= "Content-Type: text/html; charset=\"UTF-8\"\n\n"; //plain
$mens .= "$mensagem\n";
$mens .= "--$boundary\n";
$mens .= "Content-Type: ".$arquivo["type"]."\n";
$mens .= "Content-Disposition: attachment; filename=\"".$arquivo["name"]."\"\n";
$mens .= "Content-Transfer-Encoding: base64\n\n";
$mens .= "$anexo\n";
$mens .= "--$boundary--\r\n";
$headers = "MIME-Version: 1.0\n";
$headers .= "De \"$nome\"\r\n";
$headers .= "E-mail: \"$email_from\" \r\n";
$headers .= "Content-type: multipart/mixed; boundary=\"$boundary\"\r\n";
$headers .= "$boundary\n";

//envio o email com o anexo
mail($email,$assunto,$mens,$headers);
?> 
    <script type="text/javascript"> 
            alert("Email enviado com Sucesso!"); 
    </script> 
<?php
header('location: contato.php');
}
//se não tiver anexo
else
{
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=utf-8\r\n";
$headers .= "De \"$nome\"\r\n ";
$headers .= "E-mail: \"$email_from\" \r\n";
//envia o email sem anexo
mail($email,$assunto,$mensagem, $headers);
?> 
    <script type="text/javascript"> 
            alert("Email enviado com Sucesso!"); 
    </script> 
<?php
header('location: contato.php');
}
?>        

1 answer

2


The alert does not appear because of header('Location: ...')

Use javascript:

$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=utf-8\r\n";
$headers .= "De \"$nome\"\r\n ";
$headers .= "E-mail: \"$email_from\" \r\n";
//envia o email sem anexo
mail($email,$assunto,$mensagem, $headers);
?> 
    <script type="text/javascript"> 
     alert("Email enviado com Sucesso!"); 
     window.location = "contato.php";
    </script> 
<?php
}
?>

The header works with the HTTP response headers, if the browser finds the Location: ... it will process the content, but will not display.

About the problem of email delivery

The headlines De, E-mail there are no:

$headers = "MIME-Version: 1.0\n";
$headers .= "De \"$nome\"\r\n";
$headers .= "E-mail: \"$email_from\" \r\n";
$headers .= "Content-type: multipart/mixed; boundary=\"$boundary\"\r\n";
$headers .= "$boundary\n";

The right thing would be From::

$headers = "MIME-Version: 1.0\n";
$headers .= "From: \"$nome\"<$email_from>\r\n";
$headers .= "Content-type: multipart/mixed; boundary=\"$boundary\"\r\n";
$headers .= "$boundary\n";

And here there should be two line breaks:

$headers .= "Content-type: multipart/mixed; boundary=\"$boundary\"\r\n";
$headers .= "$boundary\n";

Right:

$headers .= "Content-type: multipart/mixed; boundary=\"$boundary\"\r\n\r\n";
$headers .= "$boundary\n";

Alternative

If you still have difficulties with the function mail, you can use the PHPMailer, an example with attachment:

<?php
require 'PHPMailerAutoload.php';

$mail = new PHPMailer;

$mail->isSMTP();                                      // Set mailer to use SMTP
$mail->Host = 'smtp1.example.com;smtp2.example.com';  // Specify main and backup SMTP servers
$mail->SMTPAuth = true;                               // Enable SMTP authentication
$mail->Username = '[email protected]';                 // SMTP username
$mail->Password = 'secret';                           // SMTP password
$mail->SMTPSecure = 'tls';                            // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587;                                    // TCP port to connect to

$mail->From = $email_from;
$mail->FromName = $nome;
$mail->addAddress($email);

$mail->addAttachment($_FILES["arquivo"]["tmp_name"]);
$mail->isHTML(true);

$mail->Subject = $assunto;
$mail->Body    = $mensagem;
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';

if(!$mail->send()) {
    echo 'Erro: ', $mail->ErrorInfo;
} else {
    echo 'Mensagem enviada';
}
  • That’s right, thank you (:

  • @Stable, or use a header( "refresh:5;url=contato.php" ) if you want to avoid failure in case js is disabled.

Browser other questions tagged

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