1
Good afternoon guys, I always use Phpmailer for sending emails, but I don’t know why this time I couldn’t configure on the server. So I’m using the code below to send and the question is that if you do not put the headers it sends the messages with the POST data but will not send the attachment correctly and if I put the headers it sends only the image, I do not know what is wrong, I have tried everything, follows the code, if anyone can guide me , I thank from now!
$arquivo = $_FILES["attachment"];
$boundary = "XYZ-".date("dmYis")."-ZYX";
$fp = fopen($arquivo["tmp_name"], "rb"); // abre o arquivo enviado
$anexo = fread($fp, filesize($arquivo["tmp_name"])); // calcula o tamanho
$anexo = base64_encode($anexo); // codifica o anexo em base 64
fclose($fp); // fecha o arquivo
// cabeçalho do email
$headers = "MIME-Version: 1.0\n";
$headers .= "Content-Type: multipart/mixed; ";
$headers .= "boundary=".$boundary."\r\n";
$headers .= "$boundary\n";
// email
$mensagem = "--$boundary\n";
$mensagem .= "Content-Type: text/html; charset='utf-8'\n";
$mensagem .= "Nome: $name \r\n";
$mensagem .= "E-mail: $email \r\n";
$mensagem .= "Cidade: $city \r\n";
$mensagem .= "Estado: $state \r\n";
$mensagem .= "Mensagem: $message \r\n";
$mensagem .= "--$boundary \n";
if( empty ($arquivo[0] ) ){
$mensagem .= "Content-Type: " .$arquivo["type"]. "; name=\"\" " .$arquivo['name']. " \n";
$mensagem .= "Content-Transfer-Encoding: base64 \n";
$mensagem .= "Content-Disposition: attachment; filename=\"\" " .$arquivo['name']. " \r\n";
$mensagem .= "$anexo \n";
$mensagem .= "--$boundary \n";
}
// enviar o email
$sendMail = mail($sender_email, $default_subject, $mensagem, $headers);
if( !$sendMail ){
echo json_encode( array(
'alert' => 'error',
'message' => $error_message ));
} else {
echo json_encode( array( 'alert' => 'success' , 'message' => $success_message ) );
}
Thank you, but as I said this problem on the server that is not accepting to use phpmailer, but I fix the problem of that, thank you
– Thiago Moreira