Guides and tutorials

Hundreds of tutorials and step by step guides carefully written by our support team.

PHPMailer

INTRODUCTION

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.

The PHPMailer library

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

enter image description here

enter image description here

Create email account and get connection details

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.

Mail account manual

Outlook configuration manual

PHPMailer installation with composer

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.

Manual installation of PHPMailer

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

Simple configuration example

** 1 .- ** Save the following code in a file example.php

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_SMTP_USER'; // SMTP user $ mail-> Password = 'SET_SMTP_PASSWORD'; // SMTP password $ mail-> SMTPSecure = PHPMailer :: ENCRYPTION_STARTTLS; $ mail-> Port = 587; $ mail-> setFrom ('hello@test.com', 'Your name'); // Mail sender // Recipients $ mail-> addAddress ('test@mydomain.com', 'Recipient's name'); // Email and name of the recipient // Email content $ mail-> isHTML (true); $ mail-> Subject = 'Email subject'; $ mail-> Body = 'Email content in HTML! '; $ mail-> AltBody = 'Plain text mail content for mail clients that do not support HTML'; $ mail-> send (); echo 'The message has been sent'; } catch (Exception $ e) { echo "The message was not 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://yourdomain.com/example.php-lex.europa.eu(http://yourdomain.com/example.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 ** ## Example for Wordpress 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/-lex.europa.eu(https://es.wordpress.org/plugins/wp-mail-smtp/) Specific manual for this case at: [https://www.swhosting.com/es/comunidad/manual/configurar-plugin-para-envio-de-correo-wp-mail-smtp-en-wordpressvascript (https://www.swhosting.com/es/comunidad/manual/ /www.swhosting.com/es/community/manual/configure-plugin-to-send-mail-wp-mail-smtp-en-wordpress)
More than 2000 m² of own facilities and Data Centers in Spain
Your privacy is important for us
We use our own cookies for the proper functioning of the site. In addition, third-party cookies are used for analytical purposes only. This information is not associated with any person so that personal identifying data is not stored, but is only information that is collected to identify the session, with the aim of facilitating the analysis of the website. You can change your preferences at any time by entering this website again. For more information about our cookie policy you can visit our Cookies. You can press the "Accept and close" button to give us your consent or you can access more detailed information and manage cookies.
More than 2000 m² of own facilities and Data Centers in Spain
Your privacy is important for us
We use our own cookies for the proper functioning of the site. In addition, third-party cookies are used for analytical purposes only. This information is not associated with any person so that personal identifying data is not stored, but is only information that is collected to identify the session, with the aim of facilitating the analysis of the website. You can change your preferences at any time by entering this website again. For more information about our cookie policy you can visit our Cookies. You can press the "Accept and close" button to give us your consent or you can access more detailed information and manage cookies.