Example #1
0
 public function getServiceConfig()
 {
     return array('factories' => array("Mail" => function ($sm) {
         $mail = new Mail();
         $mail->setRenderer($sm->get("ViewRenderer"));
         /**
          * @var $sm \Zend\ServiceManager\ServiceLocatorInterface
          */
         $config = $sm->get("config");
         $env = $config["env"]["type"];
         $transporterConfig = $config["WasabiMail"]["transporter"];
         $transporter = null;
         //Production
         if ($env == "production") {
             $transporter = new Sendmail();
         } elseif ($env == "develop") {
             $mailConfig = $transporterConfig["develop"];
             $mail->setTo($mailConfig["to"]);
             $transporter = new \Zend\Mail\Transport\Smtp();
             $options = new \Zend\Mail\Transport\SmtpOptions();
             $options->setName($mailConfig["name"]);
             $options->setHost($mailConfig["host"]);
             $options->setPort($mailConfig["port"]);
             if (isset($mailConfig['connection_class'])) {
                 $options->setConnectionClass($mailConfig['connection_class']);
             }
             if (isset($mailConfig['connection_config'])) {
                 $options->{$mailConfig}['connection_config'];
             }
             $transporter->setOptions($options);
         } elseif ($env == "local") {
             $fileConfig = $transporterConfig["local"];
             $options = new \Zend\Mail\Transport\FileOptions();
             $options->setPath($fileConfig["base"] . $fileConfig["target"]);
             $options->setCallback(function (\Zend\Mail\Transport\File $transport) {
                 return "Message_" . microtime(true) . "-" . mt_rand(0, 100) . ".txt";
             });
             $transporter = new \Zend\Mail\Transport\File($options);
         }
         $mail->setTransporter($transporter);
         return $mail;
     }));
 }
<?php

require_once 'vendor/autoload.php';
if (isset($_POST['sujet'], $_POST['message'])) {
    $sujet = $_POST['sujet'];
    $message = $_POST['message'];
    $dest = 'tmp/';
    $mail = new \Zend\Mail\Message();
    $mail->setSubject($sujet)->addTo('*****@*****.**')->setFrom('*****@*****.**')->setBody($message);
    $options = new \Zend\Mail\Transport\FileOptions();
    $options->setPath($dest);
    $transport = new \Zend\Mail\Transport\File($options);
    $transport->send($mail);
}
?>
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>Formulaire Contact</title>
</head>
<body>
<form method="post">
    <p>
        Sujet :
        <input name="sujet">
    </p>

    <p>
        Message :
        <textarea name="message"></textarea>