<?php
/**
 * @link      http://github.com/zendframework/ZendSkeletonApplication for the canonical source repository
 * @copyright Copyright (c) 2005-2016 Zend Technologies USA Inc. (http://www.zend.com)
 * @license   http://framework.zend.com/license/new-bsd New BSD License
 */

namespace Application\Controller;

use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\JsonModel;
use Zend\View\Model\ViewModel;
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

class IndexController extends AbstractActionController
{
    public function indexAction()
    {   $view = new ViewModel();
        return $view;
    }

    public function contactAction()
    {
        $json = new JsonModel();

        $retMessage = "Le server n'a pas pu envoyer le message. Veuillez réessayer plus tard.";
        $isEnvoye = FALSE;
        ini_set('SMTP', 'smtp.free.fr');

        if(isset($_POST['name']) && isset($_POST['email']) && isset($_POST['subject']) && isset($_POST['message'])){
            $name = $_POST['name'];
            $email = $_POST['email'];
            $subject = $_POST['subject'];
            $message = nl2br($_POST['message']);
            $to = "nicolas.riault@gmail.com";
            $from = $email;
            $message_format = '<b>Nom:</b> '.$name.' <br><b>Email:</b> '.$email.' <p>'.$message.'</p>';
            $headers = "From: $from\n";
            $headers .= "MIME-Version: 1.0\n";
            $headers .= "content-type: text/html; charset=iso-8859-1\n";
            if(mail($to, $subject, $message_format, $headers)){
                $retMessage = "Le mail est envoyé.";
                $isEnvoye = TRUE;
            }
        }

       /* if(isset($_POST['name']) && isset($_POST['email']) && isset($_POST['subject']) && isset($_POST['message'])){
            $name = $_POST['name'];
            $email = $_POST['email'];
            $subject = $_POST['subject'];
            $message = nl2br($_POST['message']);
            $to = "nicolas.riault@gmail.com";
            $from = $email;
            $message_format = '<b>Nom:</b> '.$name.' <br><b>Email:</b> '.$email.' <p>'.$message.'</p>';

            $mail = new PHPMailer();                              // Passing `true` enables exceptions
            try {
                //Server settings                            // Enable verbose debug output
                $mail->isSMTP();                                      // Set mailer to use SMTP
                $mail->Host = 'smtp.gmail.com';  // Specify main and backup SMTP servers
                $mail->SMTPAuth = true;                               // Enable SMTP authentication
                $mail->Username = $to;                 // SMTP username
                $mail->Password = 'Dpz9jou99';                           // SMTP password
                $mail->SMTPSecure = 'tls';                            // Enable TLS encryption, `ssl` also accepted
                $mail->Port = 587;                                    // TCP port to connect to

                //Recipients
                $mail->setFrom($from, 'Mailer');
                $mail->addReplyTo($to, 'Nicolas Riault');

                //Content
                $mail->isHTML(true);                                  // Set email format to HTML
                $mail->Subject = $subject;
                $mail->Body    = $message_format;

                $mail->send();
                $retMessage = "Le mail est envoyé.";
                $isEnvoye = TRUE;
            } catch (Exception $e) {

                $retMessage = "Le server n'a pas pu envoyer le message." . $mail->ErrorInfo;
                $isEnvoye = FALSE;
            }
        }
*/
        $result = array(
            'result' => array(
                'content' => array(
                    'msg' => $retMessage,
                    'statut' => $isEnvoye,
                ),
            ),
            'success' => $isEnvoye,
        );

        $json->setVariables($result);
        return $json;
    }
}
