Knowledge base
1000 FAQs, 500 tutorials and instructional videos. Here, there are only solutions!
Using PHPMailer on Infomaniak
This guide explains how to use PHPMailer with Infomaniak Web Hosting to send emails from a website.
⚠ WARNING ⚠
Infomaniak guarantees that its services comply with standard protocols (IMAP, S3, etc.), but does not provide additional support for external services or software, as their configuration may change depending on the provider or publisherThis guide is therefore provided for informational purposes only, and its implementation remains your responsibility (see: Support Policy / Art. 11.9 of the Terms of Service). If needed, a qualified professional can assist you.
Introduction
- PHPMailer is a PHP library that allows you to create and send emails from a website, particularly via SMTP.
- It allows you to send messages in HTML format, add an alternative text version, manage attachments, and use SMTP authentication.
- For authenticated sending via SMTP, the PHP environment used by the website must be compatible with a recent encrypted connection, specifically TLS 1.2 or higher.
- An outdated version of PHP or the OpenSSL library used by PHP may cause a connection or authentication error, even if the email address and password are correct.
- Do not use PHP 5.3/5.4 for this type of sending; use a recent and supported version of PHP.
- With authenticated SMTP sending, the address used as the sender must match the email address used for SMTP authentication.
Install PHPMailer
To use PHPMailer, install the library in your website's files.
- Download PHPMailer from its official repository or install it with Composer if your project uses it.
- Copy the PHPMailer files into a directory on your website via FTP.
Upload the necessary files to your PHP script, adapting the path according to the chosen location:
use PHPMailer\PHPMailer\PHPMailer; use PHPMailer\PHPMailer\Exception; require **DIR** . '/PHPMailer/src/Exception.php'; require **DIR** . '/PHPMailer/src/PHPMailer.php'; require **DIR** . '/PHPMailer/src/SMTP.php';
Configure SMTP sending with Infomaniak
Use the following authenticated SMTP settings:
- SMTP server:
mail.infomaniak.com - Port:
465 - Encryption:
SMTPS/ SSL-TLS - Authentication: required
- Username: the complete email address
- Password: the password for the email address used
Example of a complete configuration:
<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require **DIR** . '/PHPMailer/src/Exception.php';
require **DIR** . '/PHPMailer/src/PHPMailer.php';
require **DIR** . '/PHPMailer/src/SMTP.php';
$mail = new PHPMailer(true);
try {
// Configuration SMTP
$mail->isSMTP();
$mail->Host = 'mail.infomaniak.com';
$mail->SMTPAuth = true;
$mail->Username = '[sender@domain.xyz](mailto:sender@domain.xyz)';
$mail->Password = 'mot_de_passe_de_l_adresse_mail';
$mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS;
$mail->Port = 465;
$mail->CharSet = 'UTF-8';
```
// Expéditeur
// L'adresse doit correspondre à celle utilisée pour l'authentification SMTP.
$mail->setFrom('sender@domain.xyz', 'Nom du site');
// Destinataire
$mail->addAddress('recipient@example.com');
// Adresse de réponse facultative
$mail->addReplyTo('sender@domain.xyz', 'Nom du site');
// Contenu du message
$mail->isHTML(true);
$mail->Subject = 'Message envoyé depuis le site';
$mail->Body = '<p>Contenu HTML du message.</p>';
$mail->AltBody = 'Contenu texte du message.';
$mail->send();
echo 'Message envoyé';
```
} catch (Exception $e) {
echo 'Le message n'a pas pu être envoyé. Erreur: ' . htmlspecialchars($mail->ErrorInfo);
}If your application requires the use of port 587, use STARTTLS:
$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
$mail->Port = 587;
Check the PHP version and TLS compatibility
Authenticated SMTP sending requires a recent encrypted connection. If the site uses an outdated version of PHP, for example PHP 5.3, or an outdated OpenSSL library, PHPMailer may fail before or during authentication. The errors may then be misleading, for example:
SMTP connect() failedCould not authenticatestream_socket_enable_crypto()- an authentication error even though the password is correct
In this case, check the following:
- the PHP version used by the website;
- the OpenSSL version used by PHP;
- the version of PHPMailer used by your project;
- the correspondence between the SMTP port and the chosen encryption method.
For reliable configuration, use a recent version of PHP that is compatible with TLS 1.2 or higher, as well as a current version of PHPMailer. If necessary, refer to these guides:
- Change the PHP version of a website
- Troubleshoot a PHP issue related to an outdated version
- Display the website's technical information
Resolving a Sender Mismatch Error
The error Sender mismatch SMTP code: 550 Additional SMTP info: 5.7.1 can occur when the address used as the sender does not match the email address used for SMTP authentication. Here is an example to avoid:
$mail->Username = 'sender@domain.xyz';
$mail->setFrom('another-address@domain.xyz', 'Nom du site');In this example, the script authenticates with sender@domain.xyz, but attempts to send the message using a different sender address.
Use the same email address for SMTP authentication and for the sender:
$mail->Username = 'sender@domain.xyz';
$mail->setFrom('sender@domain.xyz', 'Nom du site');The sender name can be customized, but the email address must remain consistent with the SMTP account used.
In the case of a contact form
If a visitor enters their email address in a form, do not use it as the sender's address for the message. Use the authenticated email address as the sender, and then add the visitor's address as the reply-to address.
Example:
$mail->Username = 'sender@domain.xyz';
$mail->setFrom('sender@domain.xyz', 'Formulaire du site');
$mail->addReplyTo($email_visiteur);This configuration allows you to respond to visitors from your email software without sending the message with an unauthorized sender address.
Temporarily enable debug mode
In case of an error, temporarily enable PHPMailer's debug mode to obtain more details about the SMTP connection:
$mail->SMTPDebug = 2;
$mail->Debugoutput = 'html';Disable this mode after your tests to avoid displaying technical information to website visitors.
Learn more
Link to this FAQ: https://faq.infomaniak.com/576
Has this FAQ been helpful?