Hello I'm coming to you because I have a problem with a feature SwiftMailer PHP, indeed the sending function does not work and the email does not look (server OVH), but locally it works perfectly. I tried to find some errors by putting several die (), and it would seem that the problem comes from the line $ result = $ mailer-> send ($ message); I do not understand the difference between the local and the production if not the connection (address that I recovered on OVH and mdp that I defined myself) I would have liked more information on this and help to solve my problem, thank you!
<?php
session_start();
error_reporting(E_ALL);
ini_set('error_reporting', E_ALL);
require_once 'vendor/autoload.php';
if($_SERVER['SERVER_NAME'] == 'localhost'){
$transport = (new Swift_SmtpTransport('smtp.mailtrap.io', 25))
->setUsername('*****')
->setPassword('*****');
}
else{
$transport = (new Swift_SmtpTransport('SSL0.OVH.NET', 587))
->setUsername('[email protected]')
->setPassword('Password');
}
$mailer = new Swift_Mailer($transport);
$errors = array(); // on crée une vérif de champs
if(empty($_POST['Nom'])) {// on verifie l'existence du champ et d'un contenu
$errors ['Nom'] = "Vous n'avez pas renseigné votre nom";
}
if(!array_key_exists('Email', $_POST) || $_POST['Email'] == '' || !filter_var($_POST['Email'], FILTER_VALIDATE_EMAIL)) {// on verifie existence de la clé
$errors ['Email'] = "Vous n'avez pas renseigné votre email";
}
if(empty($_POST['Sujet'])) {// on verifie l'existence du champ et d'un contenu
$errors ['Sujet'] = "Vous n'avez pas renseigné l'objet du message";
}
if(empty($_POST['Message'])) {
$errors ['Message'] = "Vous n'avez pas renseigné votre Message";
}
if(empty($errors) !== true ){ // si erreur on renvoie vers la page précédente
$_SESSION['errors'] = $errors;//on stocke les erreurs
$_SESSION['inputs'] = $_POST;
header('Location: contact.php');
}else{
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=utf-8' . "\r\n";
$message = (new Swift_Message(htmlspecialchars($_POST['Sujet'])))
->setSubject(htmlspecialchars($_POST['Sujet']))
->setFrom([htmlspecialchars($_POST['Email']) => htmlspecialchars($_POST['Nom'])])
->setTo(['[email protected]'])
->setBody(htmlspecialchars($_POST['Message']));
$result = $mailer->send($message);
$_SESSION['success'] = 1;
header('Location: contact.php');
}
?>
A 500 error say´s that your server have problems. But you can try to log php error to and you can compare the local used php version and the php version there you use on the server. Maybe you use an old version on the server.