Many emails often fall into SPAM or do not even reach the recipient due to filters on the servers, understand one thing, when you use the function mail()
or the program sendmail
generally does the sending is the server and not a user authenticated via SMTP, so mail servers for spam security will block things like unauthenticated emails.
It’s not putting half a dozen 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 it is receiving without the headers it is probably because the message arrives in PLAIN format (text) 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 already been much discussed here on the site, it is something that is a lot talking about SPAM, and the practical and most guaranteed solution in PHP without a shadow of doubt is to use authenticated SMTP, there are projects that already solve this for you, as:
Note that authenticated emails have a limit of mails per hour or day, usually a limit of 100 messages per day, precisely 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 don’t get down from https://github.com/PHPMailer/PHPMailer/releases the version compatible with your PHP and add this to the file header:
<?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 they 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;
}
That’s basically it, I’ve been using Phpmailer forever.
– Jorge B.