I'm using 000webhost server and I got an error while sending the email using PHPMailer, knowing that it works in localhost with tls
and Port 587
Here is my code
<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require $_SERVER['DOCUMENT_ROOT'] . '/Portfolio/mail/Exception.php';
require $_SERVER['DOCUMENT_ROOT'] . '/Portfolio/mail/PHPMailer.php';
require $_SERVER['DOCUMENT_ROOT'] . '/Portfolio/mail/SMTP.php';
if(isset($_POST['hdn-insert'])) {
$data=array();
$variables = array();
$name = $_POST['name'];
$email_from = $_POST['email'];
$subject = $_POST['subject'];
$message = $_POST['message'];
$query = @unserialize (file_get_contents('http://ip-api.com/php/'));
$location = $query['city'] . ', ' . $query['country'] . '.';
$year = date('Y');
$variables['name'] = $_POST['name'];
$variables['email'] = $_POST['email'];
$variables['message'] = $_POST['message'];
$variables['location'] = $location;
$variables['year'] = $year;
$template = file_get_contents('MailUI.html');
foreach($variables as $key => $value)
{
$template = str_replace('{{ '.$key.' }}', $value, $template);
}
$mail = new PHPMailer;
$mail->isSMTP();
$mail->SMTPDebug = 0; // 0 = off (for production use) - 1 = client messages - 2 = client and server messages
$mail->Host = "smtp.gmail.com"; // use $mail->Host = gethostbyname('smtp.gmail.com'); // if your network does not support SMTP over IPv6
$mail->Port = 587; // TLS only
$mail->SMTPSecure = 'tls'; // ssl is deprecated
$mail->SMTPAuth = true;
$mail->Username = 'hazemcrunchy@gmail.com'; // email
$mail->Password = '**********'; // password
$mail->setFrom($email_from, $name); // From email and name
$mail->addAddress('hazemsamiir196@gmail.com', 'Hazem El-behairy'); // to email and name
$mail->Subject = $subject;
$mail->msgHTML($template); //$mail->msgHTML(file_get_contents('contents.html'), __DIR__); //Read an HTML message body from an external file, convert referenced images to embedded,
$mail->AltBody = 'HTML messaging not supported'; // If html emails is not supported by the receiver, show this body
// $mail->addAttachment('images/phpmailer_mini.png'); //Attach an image file
$mail->SMTPOptions = array(
'ssl' => array(
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true
)
);
if(!empty($name) && !empty($email_from) && !empty($subject) && !empty($message) && strlen($message) >= '8'){
if(!ctype_space($name) && !ctype_space($email_from) && !ctype_space($subject) && !ctype_space($message)){
if ($mail->send()){
$data['status'] = 1;
} else {
$data['status'] = 0;
}
echo json_encode($data);
} else {
$data['status'] = 0;
}
}
else{
$data['status'] = 0;
}
}
?>
The error message is:
SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
I hope you guys get me well.