send php email

1

Hello, my code sends the email to my email box, however my "alert" does not appear when I click on send

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

Correct the sender's email that appears in the server's inbox ?

If not, can you change the email that has been filled in? And I also have not been able to 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');
}
?>        
    
asked by anonymous 16.04.2015 / 23:04

1 answer

2

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
}
?>

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

About the issue of email delivery

Headers De , E-mail do not exist:

$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 correct 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";

The correct one:

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

Alternative

If you are still experiencing difficulties with the mail function, you can use PHPMailer , an example with an attachment: / p>

<?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';
}
    
16.04.2015 / 23:11