PHPMailer



Identifiants SMTP

Gmail
gmail:
  host: smtp.gmail.com
  port: 465
  username: proxidata1@gmail.com
  password: 456835216
  smtpsecure: ssl
  setfrommail: proxidata1@gmail.com
  setfromname: Proxidata
  addreplytomail: proxidata1@gmail.com
  addreplytoname: Proxidata
OVH MX PLAN
ovh:
  host: SSL0.OVH.NET
  port: 465
  username: no-reply@proxidata.fr
  password: KcGzr0C2qVJ1
  smtpsecure: ssl
  setfrommail: no-reply@proxidata.fr
  setfromname: Proxidata
  addreplytomail: no-reply@proxidata.fr
  addreplytoname: Proxidata
Constructeur

namespace Model;

use Model\Mail;
use Silex\Application;
use \PHPMailer;

/**
* Sending Emails using PHPMailer
*/
class Mail
{
  private $mail;
    private $host;
    private $port;
    private $username;
    private $password;
    private $smtpsecure;
    private $setfrommail;
    private $setfromname;
    private $addreplytomail;
    private $addreplytoname;

    private $path;

  public function __construct(Application $app)
  {
    $this->mail = new PHPMailer;

    $sender = $app['config']['sender']['use'];

    $this->host           = $app['config'][$sender]['host'];
    $this->port           = $app['config'][$sender]['port'];
    $this->username       = $app['config'][$sender]['username'];
    $this->password       = $app['config'][$sender]['password'];
    $this->smtpsecure     = $app['config'][$sender]['smtpsecure'];
    $this->setfrommail    = $app['config'][$sender]['setfrommail'];
    $this->setfromname    = $app['config'][$sender]['setfromname'];
    $this->addreplytomail = $app['config'][$sender]['addreplytomail'];
    $this->addreplytoname = $app['config'][$sender]['addreplytoname'];

    $this->mail->isSMTP();              // Set mailer to use SMTP
    $this->mail->CharSet    = 'UTF-8';
    $this->mail->Host       = $this->host;      // Specify main and backup SMTP servers
    $this->mail->SMTPAuth   = true;         // Enable SMTP authentication
    $this->mail->Username   = $this->username;    // SMTP username
    $this->mail->Password   = $this->password;    // SMTP password
    $this->mail->SMTPSecure = $this->smtpsecure;  // Enable TLS encryption, `ssl` also accepted
    $this->mail->Port       = $this->port;      // TCP port to connect to
    $this->mail->isHTML(true);            // Set email format to HTML

    $this->mail->SMTPOptions = [
      'ssl' => [
        'verify_peer'       => false,
        'verify_peer_name'  => false,
        'allow_self_signed' => true
      ]
    ];
    //$this->mail->SMTPDebug = 1;

    $this->path = 'http://localhost/www/Proxidata/web/index_dev.php';
  }

  ...
}
Envoyer un email simple

public function sendNewFormContact($html)
  {
    $this->mail->setFrom($this->setfrommail, $this->setfromname);
    $this->mail->addAddress('bernie.anglade@laposte.net', 'Bernie Anglade');      // Add a recipient
    $this->mail->addReplyTo($this->addreplytomail, $this->addreplytoname);
    $this->mail->addBCC('bernie.anglade@laposte.net');

    $this->mail->Subject = '[Proxidata] Nouveau message du formulaire de contact';
    $this->mail->Body    = $html;
    $this->mail->AltBody = 'Nouveau message du formulaire de contact';

    if(!$this->mail->send()) {
      return false;
    } else {
      return true;
    }
  }
Envoyer un email avec pièce-jointe

public function sendDevis($email, $who, $pj, $date, $html)
  {
    $this->mail->setFrom($this->setfrommail, $this->setfromname);
    $this->mail->addAddress($email, $who);      // Add a recipient
    $this->mail->addReplyTo($this->addreplytomail, $this->addreplytoname);
    $this->mail->addBCC('bernie.anglade@laposte.net');

    $this->mail->addAttachment($pj);
    //$this->mail->addAttachment('/tmp/image.jpg', 'new.jpg'); // Optional name

    $this->mail->Subject = "[Proxidata] Votre devis du $date";
    $this->mail->Body    = $html;
    $this->mail->AltBody = '';

    if(!$this->mail->send()) {
      return false;
    } else {
      return true;
    }
  }
Templates HTML/Twig

Layout
<form>
    <select class="uk-select">
        <option></option>
        <option></option>
    </select>
    <textarea class="uk-textarea"></textarea>
    <input class="uk-radio" type="radio">
    <input class="uk-checkbox" type="checkbox">
</form>
{% set size = '12' %}

<table border="0 none" cellspacing="0" width="600" style="margin: 5px auto; border: 2px solid #39a8d9; border-radius: 3px; font-family: calibri,arial,sans-serif;">
    <thead>
        <tr style="width: 100%;">
            <td colspan="2" style="background-color: #39a8d9;">
                <div>
                    <center style="margin: 5px 0 10px">
                        <a href="https://www.proxidata.fr/">
                            <img src="http://151.80.46.179/ressources/proxidata.png" alt="Proxidata" />
                        </a>
                    </center>
                </div>
            </td>
        </tr>
        <tr><td colspan="2" style="height: 20px;"></td></tr>
    </thead>

    <tbody style="padding: 5px;">
        {% block content %}{% endblock %}

        <tr style="width: 100%;">
            <td colspan="2" style="font-size:12pt; width: 100%; padding: 0 10px;">
                <br />
                <span>Cordialement,</span>
                <h2 style="font-size: 12pt;">L'équipe de Proxidata</h2>
            </td>
        </tr>
    </tbody>

    <tfoot>
        <tr style="width: 100%;">
            <td colspan="2" style="background-color: #39a8d9; font-size: 8pt; padding: 8px 10px; color: white;">
                <span>PROXIDATA / MARKET YOUR TARGET SAS - www.proxidata.fr - contact@proxidata.fr</span><br />
                <span>37 rue Marbeuf, Espace DDM 75008 PARIS France - tél +33 (0)1 82 83 62 26</span><br />
                <span>RCS Paris n° 2015B14787 - Déclaration CNIL N° 1983814 SIREN 812534147 - APE 4791A - TVA Intra FR19812534147</span>
            </td>
        </tr>
    </tfoot>
</table>
{% block content %}
{% extends "email/layout.twig" %}

{% set facture = facture_path|split('/') %}
{% set path = url('accueil') ~ 'fichier/factures/' ~ facture[0] ~ '/' ~ facture[1] %}

{% block content %}

        <tr style="width: 100%;">
            <td colspan="2" style="font-size:{{ size }}pt; width: 100%; padding: 0 10px;">
                <h1 style="font-size: 14pt;">{{ nom }},</h1>
            </td>
        </tr>
        
        <tr style="width: 100%;">
            <td colspan="2" style="font-size:{{ size }}pt; width: 100%; padding: 0 10px;">
                <span>Nous venons de recevoir votre règlement pour votre commande de {{ duree }} mois d'abonnement<br />d'un montant de {{ total }} €, et nous vous en remercions.<br /><br />
                    Votre abonnement vient d'être crédité sur votre compte.<br /><br />
                    Vous trouverez votre facture en pièce-jointe.<br /><br />
                    Vous pouvez également la télécharger sur votre espace client.</span>
            </td>
        </tr>

        <tr style="width: 100%;">
            <td colspan="2" style="width: 100%;">
                <a href="{{ path }}" title="Me rendre sur Proxidata pour télécharger ma facture" style="font-size:12pt; background-color: #39a8d9; border-radius: 3px; color: white; display: block; font-weight: bold; margin: 40px auto 30px; padding: 15px; text-align: center; text-decoration: none; text-transform: uppercase; width: 250px;">Télécharger ma facture</a>
            </td>
        </tr>

{% endblock %}