Many emails usually fall into SPAM or even arrive at the recipient due to filters on the servers, understand one thing, when you use the mail()
function or the sendmail
program usually sending it is the server and not an authenticated user via SMTP, so spam mail servers will block things like unauthenticated emails.
It is not to put half a dozen of headers that will solve, like:
$headers = "From: testsite < [email protected] >\n";
$headers .= "Cc: testsite < [email protected] >\n";
$headers .= "X-Sender: testsite < [email protected] >\n";
$headers .= 'X-Mailer: PHP/' . phpversion();
$headers .= "X-Priority: 1\n";
$headers .= "Return-Path: [email protected]\n";
And if you're getting headers it's probably because the message arrives in PLAIN format instead of HTML, and the server that receives the message allows this because PLAIN messages do not have much interaction, which makes them safer than HTML, probably.
This has been discussed a lot in the site, it is something that is very SPAM talking, and the practical and most guaranteed solution in PHP without any doubt is to use authenticated SMTP, there are projects that solve this for you, such as
-
link (download: link - choose the appropriate version for your version of PHP, do this carefully).
Note that authenticated emails have an hourly or daily send limit, usually a limit of 100 messages per day, to prevent people from using emails to make SPAM attacks.
How to install PHPMailer
If you are using composer run the command in the project folder:
composer require phpmailer/phpmailer
And at the beginning of your file add this:
use PHPMailer\PHPMailer\Exception;
require 'vendor/autoload.php';
If you do not download from link the version compatible with your PHP and add this in the header of the file:
<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require 'path/to/PHPMailer/src/Exception.php';
require 'path/to/PHPMailer/src/PHPMailer.php';
require 'path/to/PHPMailer/src/SMTP.php';
And then just below do something like this, of course the settings should follow the same as you would in an email client like Outlook or Thunderbird:
$mail = new PHPMailer(true); // Passing 'true' enables exceptions
try {
//Server settings
$mail->SMTPDebug = 2; // Enable verbose debug output
$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
//Recipients
$mail->setFrom('[email protected]', 'Mailer');
$mail->addAddress('[email protected]', 'Joe User'); // Add a recipient
$mail->addAddress('[email protected]'); // Name is optional
$mail->addReplyTo('[email protected]', 'Information');
$mail->addCC('[email protected]');
$mail->addBCC('[email protected]');
//Attachments
$mail->addAttachment('/var/tmp/file.tar.gz'); // Add attachments
$mail->addAttachment('/tmp/image.jpg', 'new.jpg'); // Optional name
//Content
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'Here is the subject';
$mail->Body = 'This is the HTML message body <b>in bold!</b>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
$mail->send();
echo 'Message has been sent';
} catch (Exception $e) {
echo 'Message could not be sent. Mailer Error: ', $mail->ErrorInfo;
}