Title: SMTP Error: Could not connect to SMTP host. Mailer Error: SMTP Error: Could not connect to SMTP host
Tags: php;smtp
Question: Hi all i am new in php and html and facing issue:
SMTP Error: Could not connect to SMTP host. Mailer Error: SMTP Error: Could not connect to SMTP host. but working fine in local xampp and coming error in server.
code below:
<?php
$name = $_POST['name'];
$contact = $_POST['contact'];
$email = $_POST['email'];
$company = $_POST['company'];
$message = $_POST['message'];
$msg= " <strong&gt;Name:</strong&gt; $name <br /&gt; <strong&gt;Contact:</strong&gt; $contact <br /&gt; <strong&gt;Email:</strong&gt; $email <br /&gt;<strong&gt;Company:</strong&gt; $company <br /&gt;<strong&gt;Message:</strong&gt; $message";
require_once('mailer/class.phpmailer.php');
$mail             = new PHPMailer();
$mail-&gt;IsSMTP();
$mail-&gt;Host       = "smtp.gmail.com";
$mail-&gt;SMTPDebug  = 0;
$mail-&gt;SMTPSecure = "ssl";
$mail-&gt;SMTPAuth   = true;
$mail-&gt;Host       = "smtp.gmail.com";
$mail-&gt;Port       = 465;
//$mail-&gt;Username   = "petersonkelly@example.org";
//$mail-&gt;Password   = "gmail pass";
//$mail-&gt;SetFrom('petersonkelly@example.org', 'Pankaj U');
$mail-&gt;SetFrom($email);
$mail-&gt;AddReplyTo($email);
$mail-&gt;Subject    = "Enquiry from Website";
$mail-&gt;MsgHTML($msg);
$address = "petersonkelly@example.org";
$mail-&gt;AddAddress($address, $name);
if(!$mail-&gt;Send()) {
echo "Mailer Error: " . $mail-&gt;ErrorInfo;
} else {
header('Location: thanks.html');
Comment: but working fine in local server?

Comment: you need to check your firewall


Here is another answer: $mail-&gt;SMTPSecure = "tls";
$mail-&gt;Port       = 587;

{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 1, 'CommentCount': 2, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2015:00:10/20/15', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<php><smtp>', 'Title': 'SMTP Error: Could not connect to SMTP host. Mailer Error: SMTP Error: Could not connect to SMTP host', 'ViewCount': 1141, 'id': 33231079, 'user_AboutMe': None, 'user_AccountId': 7152563.0, 'user_DisplayName': 'balajee enterprises', 'user_Reputation': 95.0, 'user_Views': 24.0, 'user_id': 5466086.0}}