On some servers the PHP mail() function is not available. This function is more and more often disabled on servers as a security measure. It is usually disabled for protection against SPAM, mail spoofing and proxy mail server.
In the event that any of the hosted websites are breached by a malicious user, the aforementioned risks are avoided.
Do you need to host the website and email for your domain? SWHosting can help you with its servers managed with SWPanel.
cta:cloud_app_swpanel_smart_d5
The PHPMailer library allows us, among other functionalities, to establish an SMTP connection with a mail server. This SMTP server will be the one who will actually send our email.
The official website of the project on GitHub contains the documentation with the installation and configuration options. Link to Github: https://github.com/PHPMailer/PHPMailer
We will need to have an email account created and the connection data. For this example we will need: The outgoing mail server (SMTP server), username and password.
This is the easiest and fastest option for users who already use composer in their project.
Add to the composer.json file:
"phpmailer/phpmailer":"~6.1"
.
Or directly:
composer require phpmailer/phpmailer
.
There is a complete configuration example on the Github page.
1.- Download the library from https://github.com/PHPMailer/PHPMailer, click the green "Clone or download" button and "Download ZIP".
Unzip the downloaded file "PHPMailer-master.zip" and rename the PHPMailer-master directory to PHPMailer.
Copy the PHPMailer directory inside our project.
To verify that the paths are correct, it should exist from the root of our project: /PHPMailer/src/PHPMailer.php
1.- Save the following code in an example.php file
<?php
// Show PHP errors (Disable in production)
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
// Include library PHPMailer
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
use PHPMailer\PHPMailer\SMTP;
require 'PHPMailer/src/Exception.php';
require 'PHPMailer/src/PHPMailer.php';
require 'PHPMailer/src/SMTP.php';
// Start
$mail = new PHPMailer(true);
try {
// Configuration SMTP
$mail->SMTPDebug = SMTP::DEBUG_SERVER; // Show output (Disable in production)
$mail->isSMTP(); // Activate SMTP sending
$mail->Host = 'CONFIGURE_SMTP_SERVER'; // SMTP Server
$mail->SMTPAuth = true; // SMTP Identification
$mail->Username = 'CONFIGURE_USER_SMTP'; // SMTP User
$mail->Password = 'CONFIGURE_SMTP_PASSWORD'; // SMTP Password
$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
$mail->Port = 587;
$mail->setFrom('[email protected]', 'Your name'); // Mail sender
// Recipients
$mail->addAddress('[email protected]', 'Recipient's name'); // Email and recipient's name
// Mail content
$mail->isHTML(true);
$mail->Subject = 'Subject of the mail';
$mail->Body = 'Mail content <b>in HTML!</b>';
$mail->AltBody = 'Mail content in plain text for mail clients that do not support HTML';
$mail->send();
echo 'The message has been sent';
} catch (Exception $e) {
echo "Message has not been sent. Mailer Error: {$mail->ErrorInfo}";
}
2 .- Modify the connection parameters, the e-mail of the sender and the recipient where we will send our test mail.
3 .- Upload by FTP the directory PHPMailer and the file example.php to the root of the public directory. For our case: / data / web
4 .- Run the example from http://tudominio.com/exemple.php It will show us the process debug and if everything has gone well we will have received the email. If it shows an error, be it PHP or SMTP connection, we must debug the source of the problem.
In production remember to disable debugging of PHP and SMTP errors
For those who have a website with Wordpress it will be easier to use the WP Mail SMTP plugin https://es.wordpress.org/plugins/wp-mail-smtp/
And remember that, if you need to host the website and email for your domain, SWHosting can help you with its servers managed with SWPanel.
cta:cloud_app_swpanel_smart_d5