Duplicate question. I found some answers but no one works.
I want to send email using Php Mailer or wp_mail with attachment PDF file in WordPress. In both techniques PHP Mailer or wp_mail, Email goes with attachment but every time in spam.
Code using PHP Mailer 1
$mail->From = "from email";
$mail->FromName = "name";
$mail->AddAddress("to email");
//$mail->AddReplyTo( 'from email', 'name' ); //Line add or not doesn't effect.
$mail->AddAttachment("filename.pdf");
$mail->Subject = "Email Subject";
$mail->Body = "Email Body";
if(!$mail->Send())
{
echo "Message could not be sent. <p>";
echo "Mailer Error: " . $mail->ErrorInfo;
}
else
{
echo "Message sent";
}
Code using PHP Mailer2
$mail->isSMTP();
$mail->Host = 'smtp.gmail.com';
$mail->SMTPAuth = true;
$mail->Username = 'email user name';
$mail->Password = 'password';
$mail->Port = 587; //587 or 25
$mail->From = "from email";
$mail->FromName = "name";
$mail->AddAddress("to email");
//$mail->AddReplyTo( 'from email', 'name' ); //Line add or not doesn't effect.
$mail->AddAttachment("filename.pdf");
$mail->Subject = "Email Subject";
$mail->Body = "Email Body";
if(!$mail->Send())
{
echo "Message could not be sent. <p>";
echo "Mailer Error: " . $mail->ErrorInfo;
}
else
{
echo "Message sent";
}
Code using wp_mail
global $wpdb;
$to = 'to email';
$from = 'from email';
$name = 'name';
$headers = 'From: name <from email>' . "\r\n";
$subject = 'SUBJECT';
$msg = 'CONTENT';
$mail_attachment = array('filename.pdf');
wp_mail($to, $subject, $msg, $headers, $mail_attachment);