Good afternoon, I always use PHPMailer to send emails, but I do not know why this time I could not configure it on the server. So I am using the code below to send and the question is if not put the headers it sends the messages with the POST data but it will not send the attachment correctly and if I put the headers it only sends the image, I do not know what it's happening wrong, I've tried everything, follow the code, if anyone can guide me, thank you right away!
$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 ) );
}