Skip to main content

Gestion des pièces jointes avec Q3

 

 

Cet article est obsolète, la gestion des pièces jointes et des compte SMTP est gérable directement via Quilium :
https://docs.quilium.io/settings/fields/#sendmail

 
 
 
 
Configuration YAML 

Dans la configuration de l'élément de contenu ajouté l'action exportData qui va poster le formulaire sur l'URL renseignée.

actions:
      name: Actions
      fields:
        exportData:
          type: http
          config:
            src: "http://mailer.cloos-kraus.lu/index.php"
            method: POST
Récupération et envoie du mail avec pièces jointes

Créer un fichier index.php pour traiter les données du post, récupérer les pièces jointes et envoyer le mail.

<?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\Exception;

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

// Instantiation and passing `true` enables exceptions
$mail = new PHPMailer(true);

$json = file_get_contents('php://input');
$post = json_decode($json, true);

try {
    $mail->isSMTP();                                            // Set mailer to use SMTP
    $mail->Host       = $host;  // Specify main and backup SMTP servers
    $mail->SMTPAuth   = true;                                   // Enable SMTP authentication
    $mail->Username   = $email;                     // SMTP username
    $mail->Password   = $pwd;                               // SMTP password
    $mail->SMTPSecure = 'tls';                                  // Enable TLS encryption, `ssl` also accepted
    $mail->Port       = 587;            
    
    //Recipients
        $mail->setFrom($mail->Username, 'Cloos Kraus Support Kundendienst');

        $sendTo = explode(',',$post['fields']['sendTo']);
        foreach ($sendTo as $key => $value) {
            $mail->addAddress($value);     // Add a recipient $mail->Username
        }

        $mail->addReplyTo($post['fields']['email'] ? $post['fields']['email'] : $mail->Username, 'Information');


        if (isset($post['files'])) {
            foreach ($post['files'] as $files) { // if no multifile
                if (!isset($files['content'])) { // MEANS MULTIFILES
                    foreach ($files as $file) { // if multifile
                        $mail->addStringAttachment(base64_decode($file['content']), 'multi' . $file['name']);
                    }
                } else { // ONLY ONE FILE POSTED
                    $mail->addStringAttachment(base64_decode($files['content']), 'solo' . $files['name']);
                }
            }
        }

        $subject = $post['fields']['objectTitle'];

        // Content
        $mail->isHTML(true);                                  // Set email format to HTML
        $mail->Subject = 'Kontaktanfrage - '.$subject;
        $mail->Body    = '<p>Sie haben eine neue Anfrage erhalten: <br>Name: '
            .$post['fields']['name']
            .'<br>E-mail: '.$post['fields']['email']
            .'<br>Telefon: '.$post['fields']['phone'];
        if(count($post['fields']) > 0){
            $mail->send();
            echo 'Message has been sent';
        }else{
            echo 'No Http request';
        }
    }
} catch (Exception $e) {
    echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}
    

Pour éviter de mettre les informations de connexion du compte mail, on peut créer un fichier config pour les stocker

<?php
$host = 'SMTP';
$email = 'user@mail.lu';
$pwd = '*****';
?>