PHPMailer error, SMTP connect () with Gmail

I am trying to create a contact form and Im using PHPMailer. I tried this on localhost with xampp and it works great. But when I upload to my host, I get the SMTP connect () error.

Here is my code:

$m = new PHPMailer;

$m->SMTPAuth = true;

$m->Host = "";
$m->Username = "";
$m->Password = "mypass";
$m->SMTPSecure = "ssl";
$m->Port = "465";


$m->Subject = "Hello world";
$m->Body = "Some content";

$m->FromName = "Contact";

$m->addAddress('', 'Test');


I tried changing the port to 587 and SMTPsecure to tls (and all combinations). But it doesn't work. Any tips for solving this problem?



source to share

2 answers

You may need to specify the address from which the message will be sent, for example:

$mail->From = '';


I would also give isHTML a parameter, true or false:



Another option is to drop the port specification entirely. There are several other parameters that you might find useful. The following example is the code I tested, see if you can adapt it for use:

$mail = new PHPMailer;
$mail->isSMTP();/*Set mailer to use SMTP*/
$mail->Host = '';/*Specify main and backup SMTP servers*/
$mail->Port = 587;
$mail->SMTPAuth = true;/*Enable SMTP authentication*/
$mail->Username = $username;/*SMTP username*/
$mail->Password = $password;/*SMTP password*/
/*$mail->SMTPSecure = 'tls';*//*Enable encryption, 'ssl' also accepted*/
$mail->From = '';
$mail->FromName = $name;
$mail->addAddress($to, 'Recipients Name');/*Add a recipient*/
$mail->addReplyTo($email, $name);
$mail->WordWrap = 70;/*DEFAULT = Set word wrap to 50 characters*/
$mail->addAttachment('../tmp/' . $varfile, $varfile);/*Add attachments*/
/*$mail->addAttachment('/tmp/image.jpg', 'new.jpg');*/
/*$mail->addAttachment('/tmp/image.jpg', 'new.jpg');*/
$mail->isHTML(false);/*Set email format to HTML (default = true)*/
$mail->Subject = $subject;
$mail->Body    = $message;
$mail->AltBody = $message;
if(!$mail->send()) {
    echo 'Message could not be sent.';
    echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
    header("Location: ../docs/confirmSubmit.html");


Hope this helps!



This answer works for me: / ...

I use:

$mail->Host = 'tls://';
$mail->SMTPOptions = array(
   'ssl' => array(
     'verify_peer' => false,
     'verify_peer_name' => false,
     'allow_self_signed' => true




All Articles