Envoyer du mail en PHP depuis PLMshift

Les images des conteneurs PHP de PLMshift ne proposent pas la commande système mail ou sendmail. Pour envoyer du mail il faut utiliser le module PHPmailer, avec sa documentation.

A la racine du dépôt GIT utilisé pour fabriquer l’image via le BuildConfig, créez un fichier composer.json (ou s’il existe déjà, ajoutez le module phpmailer) avec cette ligne:

"phpmailer/phpmailer":"^6.5"

ce qui donnerait à minima:

{

    "name": "php/mailer",
    "type": "project",
    "license": "private",
    "require": {
        "phpmailer/phpmailer": "^6.5"
    }
}

Ensuite, vous pouvez envoyer du mail de la façon suivante:

<?php
//Import PHPMailer classes into the global namespace
//These must be at the top of your script, not inside a function
use  PHPMailer\PHPMailer\PHPMailer;
use  PHPMailer\PHPMailer\SMTP;
use  PHPMailer\PHPMailer\Exception;

//Load Composer's autoloader
require  'vendor/autoload.php';

//Create an instance; passing `true` enables exceptions
$mail  =new  PHPMailer(true);

try  {
     //Server settings
     $mail->SMTPDebug  = SMTP::DEBUG_SERVER;//Enable verbose debug output
     $mail->isSMTP();//Send using SMTP
     $mail->Host = 'super.bordeaux';//Set the SMTP server to send through
     $mail->Port = 25;
     $mail->SMTPAuth = false;//Enable SMTP authentication
     $mail->SMTPAutoTLS = false;


     //Recipients
     $mail->setFrom('from@example.com','Mailer');
     $mail->addAddress('joe@example.net','Joe User');//Add a recipient

     //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}";
}