I installed PHPMailer to send emails from my PHP application. I'm using Mailtrap to fake the server.
However, when I submit the form to send an email I get this error below:
Fatal error: Uncaught PHPMailer\PHPMailer\Exception: SMTP Error: Could not connect to SMTP host. Failed to connect to server in /home/raso1970/cliquedigitale.com4muz.com/vendor/phpmailer/phpmailer/src/PHPMailer.php:2233 Stack trace: #0 /home/raso1970/cliquedigitale.com4muz.com/vendor/phpmailer/phpmailer/src/PHPMailer.php(2019): PHPMailer\PHPMailer\PHPMailer->smtpConnect(Array) #1 /home/raso1970/cliquedigitale.com4muz.com/vendor/phpmailer/phpmailer/src/PHPMailer.php(1678): PHPMailer\PHPMailer\PHPMailer->smtpSend('Date: Sat, 8 Ap...', 'This is the HTM...') #2 /home/raso1970/cliquedigitale.com4muz.com/vendor/phpmailer/phpmailer/src/PHPMailer.php(1516): PHPMailer\PHPMailer\PHPMailer->postSend() #3 /home/raso1970/cliquedigitale.com4muz.com/email.php(80): PHPMailer\PHPMailer\PHPMailer->send() #4 {main} thrown in /home/raso1970/cliquedigitale.com4muz.com/vendor/phpmailer/phpmailer/src/PHPMailer.php on line 2233
I wrote this code in email.php
:
<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
/////// START PHPMAILER //////
//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\SMTP;
use PHPMailer\PHPMailer\Exception;
?>
<!-- Configuration-->
<?php require_once("./classes/Config.php"); ?>
<?php
//Load Composer's autoloader
require 'vendor/autoload.php';
/**
*
*
* Configure PHPMailer
*
*
*/
//Create an instance; passing `true` enables exceptions
$mail = new PHPMailer(true);
//Server settings
// $mail->SMTPDebug = SMTP::DEBUG_SERVER; //Enable verbose debug output
$mail->isSMTP(); //Send using SMTP
$mail->Host = Config::SMTP_HOST; //Set the SMTP server to send through
$mail->Username = Config::SMTP_USER; //SMTP username
$mail->Password = Config::SMTP_PASSWORD; //SMTP password
$mail->Port = Config::SMTP_PORT; //TCP port to connect to; use 587 if you have set `SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS`
$mail->SMTPAuth = true; //Enable SMTP authentication
$mail->SMTPSecure = 'tls'; //Enable implicit TLS encryption
$mail->isHTML(true);
$mail->CharSet = 'UTF-8';
//Recipients
$mail->setFrom("john@doe.com", "John Doe");
$mail->addAddress("doe@john.com");
$mail->Subject = 'Here is the subject';
$mail->Body = 'This is the HTML message body <b>in bold!</b>';
////// END PHPMAILER /////
///// START SENDING EMAIL //////
if($mail->send()){
echo "IT WAS SENT";
} else {
echo "NOT SENT";
}
///// END SENDING EMAIL /////
?>
Then, I use this file in contact.php
in the action attribute of the form:
<!-- Start contact form section -->
<section class="contact__form--section section--padding pt-0">
<div class="container">
<div class="section__heading text-center mb-40">
<h2 class="section__heading--maintitle mb-10">Une Question ?</h2>
<p class="section__heading--desc">
Merci de nous laisser un message en indiquant le sujet de votre
consultation, nous vous répondrons dans les 24h ouvrées.
</p>
</div>
<div class="main__contact--area">
<div class="row">
<div class="col-xl-5 col-lg-6 col-md-6">
<div class="contact__form--thumbnail">
<img
class="contact__form--thumbnail__img"
src="assets/img/other/contact-thumb2.webp"
alt="contact-thumb"
/>
</div>
</div>
<div class="col-xl-7 col-lg-6 col-md-6">
<div class="contact__form">
<form
class="contact__form--inner"
id="contactFormData"
action="email.php"
method="POST"
>
<div class="row">
<div class="col-lg-6 col-md-6 col-sm-6">
<div class="contact__form--list mb-20">
<label class="contact__form--label" for="input1"
>Prénom
<span class="contact__form--label__star"
>*</span
></label
>
<input
class="contact__form--input"
name="fname"
required="required"
pattern="[0-9A-Za-z\- ]+"
id="input1"
placeholder="Votre Prénom"
type="text"
/>
</div>
</div>
<div class="col-lg-6 col-md-6 col-sm-6">
<div class="contact__form--list mb-20">
<label class="contact__form--label" for="input2"
>Nom
<span class="contact__form--label__star"
>*</span
></label
>
<input
class="contact__form--input"
name="lname"
required="required"
pattern="[0-9A-Za-z\- ]+"
id="input2"
placeholder="Votre Nom"
type="text"
/>
</div>
</div>
<div class="col-lg-6 col-md-6 col-sm-6">
<div class="contact__form--list mb-20">
<label class="contact__form--label" for="input3"
>Numéro de Téléphone
<span class="contact__form--label__star"
>*</span
></label
>
<input
class="contact__form--input"
id="input3"
placeholder="Numéro de Téléphone"
type="number"
name="phone"
required="required"
/>
</div>
</div>
<div class="col-lg-6 col-md-6 col-sm-6">
<div class="contact__form--list mb-20">
<label class="contact__form--label" for="input4"
>Email
<span class="contact__form--label__star"
>*</span
></label
>
<input
class="contact__form--input"
name="email"
id="input4"
placeholder="Email"
type="email"
required="required"
/>
</div>
</div>
<div class="col-12">
<div class="contact__form--list mb-10">
<label class="contact__form--label" for="input5"
>Votre Message
<span class="contact__form--label__star"
>*</span
></label
>
<textarea
class="contact__form--textarea"
name="message"
id="input5"
placeholder="Votre Message"
required="required"
></textarea>
</div>
</div>
</div>
<button
class="contact__form--btn primary__btn"
type="submit"
name="submit"
>
Envoyer
</button>
<p id="my-form-status"></p>
</form>
</div>
</div>
</div>
</div>
</div>
</section>
<!-- End contact section -->
I was expecting to receive an email in my inbox in Mailtrap. However, I get the error above.
Edit: Now, I'm using o2switch SMTP host because I would like to put this project online. Currently, I can connect to SMTP host. However I got an error.
2023-04-08 19:32:24 SERVER -> CLIENT: 220-onglet.o2switch.net ESMTP Exim 4.95 #2 Sat, 08 Apr 2023 21:32:24 +0200 220-We do not authorize the use of this system to transport unsolicited, 220 and/or bulk e-mail. 2023-04-08 19:32:24 CLIENT -> SERVER: EHLO cliquedigitale.com4muz.com 2023-04-08 19:32:24 SERVER -> CLIENT: 250-onglet.o2switch.net Hello cliquedigitale.com4muz.com [109.234.164.157]250-SIZE 52428800250-8BITMIME250-PIPELINING250-PIPE_CONNECT250-AUTH PLAIN LOGIN250 HELP 2023-04-08 19:32:24 CLIENT -> SERVER: AUTH LOGIN 2023-04-08 19:32:24 SERVER -> CLIENT: 334 VXNlcm5hbWU6 2023-04-08 19:32:24 CLIENT -> SERVER: [credentials hidden] 2023-04-08 19:32:24 SERVER -> CLIENT: 334 UGFzc3dvcmQ6 2023-04-08 19:32:24 CLIENT -> SERVER: [credentials hidden] 2023-04-08 19:32:24 SERVER -> CLIENT: 235 Authentication succeeded 2023-04-08 19:32:24 CLIENT -> SERVER: MAIL FROM:soso@gmail.com 2023-04-08 19:32:24 SERVER -> CLIENT: 250 OK 2023-04-08 19:32:24 CLIENT -> SERVER: RCPT TO:soso@gmail.com 2023-04-08 19:32:24 SERVER -> CLIENT: 250 Accepted 2023-04-08 19:32:24 CLIENT -> SERVER: DATA 2023-04-08 19:32:24 SERVER -> CLIENT: 354 Enter message, ending with "." on a line by itself 2023-04-08 19:32:24 CLIENT -> SERVER: Date: Sat, 8 Apr 2023 21:32:24 +0200 2023-04-08 19:32:24 CLIENT -> SERVER: To: soso@gmail.com 2023-04-08 19:32:24 CLIENT -> SERVER: From: Sofiane soso@gmail.com 2023-04-08 19:32:24 CLIENT -> SERVER: Subject: $title 2023-04-08 19:32:24 CLIENT -> SERVER: Message-ID: OradUdYR5sVTJuSvr9I7kFmeGDeJelsezuQ2lvCaE8@cliquedigitale.com4muz.com 2023-04-08 19:32:24 CLIENT -> SERVER: X-Mailer: PHPMailer 6.7.1 (https://github.com/PHPMailer/PHPMailer) 2023-04-08 19:32:24 CLIENT -> SERVER: MIME-Version: 1.0 2023-04-08 19:32:24 CLIENT -> SERVER: Content-Type: text/html; charset=UTF-8 2023-04-08 19:32:24 CLIENT -> SERVER: 2023-04-08 19:32:24 CLIENT -> SERVER: $comment 2023-04-08 19:32:24 CLIENT -> SERVER: 2023-04-08 19:32:24 CLIENT -> SERVER: . 2023-04-08 19:32:24 SERVER -> CLIENT: 250 OK id=1plEIR-0001hK-Mh 2023-04-08 19:32:24 CLIENT -> SERVER: QUIT 2023-04-08 19:32:24 SERVER -> CLIENT: 221 onglet.o2switch.net closing connection SOSO, we received your comment.
I don't know what is the issue now?
Here is my code for email.php
:
<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
/////// START PHPMAILER //////
//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\SMTP;
use PHPMailer\PHPMailer\Exception;
?>
<!-- Configuration-->
<?php require_once("./classes/Config.php"); ?>
<?php
//Load Composer's autoloader
require 'vendor/autoload.php';
// Your email address
$to = "contact@com4muz.com";
// Get first name value
$fname = $_POST['fname'];
// Get Last name value
$lname = $_POST['lname'];
// Get phone number
$phone = $_POST['phone'];
// Get email value
$email = $_POST['email'];
// Get comment text
$comment = $_POST['message'];
// Email tile
// if client name is Robert, title will be "Robert send you an email"
$title = "$fname $lname send you an email";
// Email parameters
// $mailText .= "Name: $fname $lname \n";
// $mailText .= "Phone: $phone \n";
// $mailText .= "Email: $email \n";
// $mailText .= "Message: $comment \n";
// Set "From Header parameter"
// $headers = 'From:' . $email;
// Send email
// $sendMail = mail($to, $title, $mailText, $headers);
/**
*
*
* Configure PHPMailer
*
*
*/
//Create an instance; passing `true` enables exceptions
$mail = new PHPMailer(true);
// try {
//Server settings
$mail->SMTPDebug = SMTP::DEBUG_SERVER;
$mail->isSMTP();
$mail->Host = Config::SMTP_HOST;
$mail->Username = Config::SMTP_USER;
$mail->Password = Config::SMTP_PASSWORD;
$mail->Port = Config::SMTP_PORT;
$mail->SMTPAuth = true;
$mail->SMTPSecure = 'ssl';
$mail->isHTML(true);
$mail->CharSet = 'UTF-8';
//Recipients
$mail->setFrom("$email", "$fname");
$mail->addAddress("$email"); //Add a recipient
// $mail->addAddress('ellen@example.com'); //Name is optional
// $mail->addReplyTo('info@example.com', 'Information');
// $mail->addCC('cc@example.com');
// $mail->addBCC('bcc@example.com');
//Attachments
// $mail->addAttachment('/var/tmp/file.tar.gz'); //Add attachments
// $mail->addAttachment('/tmp/image.jpg', 'new.jpg'); //Optional name
//Content
// $mail->isHTML(true); //Set email format to HTML
$mail->Subject = '$title';
$mail->Body = '$comment';
// $mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
// $mail->send();
// echo 'Message has been sent';
// } catch (Exception $e) {
// echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
// }
////// END PHPMAILER /////
///// START SENDING EMAIL //////
if ($_POST) {
// if all informations are filled
if (($_POST['fname']) && ($_POST['lname']) && ($_POST['phone']) && ($_POST['email']) && ($_POST['message'])) {
// If email is sucessful send
if($mail->send()){
// Write sucessful message
echo "<strong>$lname</strong>, we received your comment.";
exit;
// If email is NOT sucessful send
} else {
// Write message
echo "<strong>Error</strong>, please try again.";
exit;
}
// If we dont have POST method
} else {
// Write message
echo "<strong>Error</strong>, please try again.";
exit;
}
}
///// END SENDING EMAIL /////
?>
Here is my code for contact.php:
<div class="col-xl-7 col-lg-6 col-md-6">
<div class="contact__form">
<form
class="contact__form--inner"
id="contactFormData"
action="email.php"
method="POST"
>
<div class="row">
<div class="col-lg-6 col-md-6 col-sm-6">
<div class="contact__form--list mb-20">
<label class="contact__form--label" for="input1"
>Prénom
<span class="contact__form--label__star"
>*</span
></label
>
<input
class="contact__form--input"
name="fname"
required="required"
pattern="[0-9A-Za-z\- ]+"
id="input1"
placeholder="Votre Prénom"
type="text"
/>
</div>
</div>
<div class="col-lg-6 col-md-6 col-sm-6">
<div class="contact__form--list mb-20">
<label class="contact__form--label" for="input2"
>Nom
<span class="contact__form--label__star"
>*</span
></label
>
<input
class="contact__form--input"
name="lname"
required="required"
pattern="[0-9A-Za-z\- ]+"
id="input2"
placeholder="Votre Nom"
type="text"
/>
</div>
</div>
<div class="col-lg-6 col-md-6 col-sm-6">
<div class="contact__form--list mb-20">
<label class="contact__form--label" for="input3"
>Numéro de Téléphone
<span class="contact__form--label__star"
>*</span
></label
>
<input
class="contact__form--input"
id="input3"
placeholder="Numéro de Téléphone"
type="number"
name="phone"
required="required"
/>
</div>
</div>
<div class="col-lg-6 col-md-6 col-sm-6">
<div class="contact__form--list mb-20">
<label class="contact__form--label" for="input4"
>Email
<span class="contact__form--label__star"
>*</span
></label
>
<input
class="contact__form--input"
name="email"
id="input4"
placeholder="Email"
type="email"
required="required"
/>
</div>
</div>
<div class="col-12">
<div class="contact__form--list mb-10">
<label class="contact__form--label" for="input5"
>Votre Message
<span class="contact__form--label__star"
>*</span
></label
>
<textarea
class="contact__form--textarea"
name="message"
id="input5"
placeholder="Votre Message"
required="required"
></textarea>
</div>
</div>
</div>
<button
class="contact__form--btn primary__btn"
type="submit"
name="submit"
>
Envoyer
</button>
<p id="my-form-status"></p>
</form>
</div>
</div>