4

I’m trying to make a contact form and I’m using PHPMailer. I tried that on localhost with xampp and it works perfect. But when i upload to my host i get the error SMTP connect() failed.

Here is my code:

$m = new PHPMailer;

$m->isSMTP();
$m->SMTPAuth = true;

$m->Host = "smtp.gmail.com";
$m->Username = "mymail@gmail.com";
$m->Password = "mypass";
$m->SMTPSecure = "ssl";
$m->Port = "465";

$m->isHTML();

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

$m->FromName = "Contact";

$m->addAddress('mymail@gmail.com', 'Test');

I've tried to change the port to 587 and the SMTPsecure to tls (and all the combinations). But doesn’t work. Any advice to solve this?

Thanks

Devilquest
  • 102
  • 3
  • 3
  • 9
  • possible duplicate of [PHPMailer cannot send email](http://stackoverflow.com/questions/25321030/phpmailer-cannot-send-email) – Synchro Sep 19 '14 at 01:45

2 Answers2

4

This answer work form me: https://stackoverflow.com/a/47205296/2171764

I use:

$mail->Host = 'tls://smtp.gmail.com:587';
$mail->SMTPOptions = array(
   'ssl' => array(
     'verify_peer' => false,
     'verify_peer_name' => false,
     'allow_self_signed' => true
    )
);
Jhonattan
  • 372
  • 3
  • 13
  • Don't do this. It's either hiding the fact that your server's TLS configuration is broken (probably an outdated CA cert bundle), or that someone (most likely your hosting provider) is redirecting your SMTP traffic somewhere else (a man-in-the-middle attack), exactly the kind of thing that TLS is designed to prevent. – Synchro Sep 23 '20 at 07:36
3

You may need to specify the address from which the message is going to be sent, like this:

$mail->From = 'user@domain.com';

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

$m->isHTML(true);

Another option is trying to drop the port specification all together. There are several other parameters that you may find useful. The following example is code I've tested, see if you can adapt it for your uses:

$mail = new PHPMailer;
$mail->isSMTP();/*Set mailer to use SMTP*/
$mail->Host = 'mail.domain.com';/*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 = 'user@domain.com';
$mail->FromName = $name;
$mail->addAddress($to, 'Recipients Name');/*Add a recipient*/
$mail->addReplyTo($email, $name);
/*$mail->addCC('cc@example.com');*/
/*$mail->addBCC('bcc@example.com');*/
$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!

CH3M
  • 61
  • 7
  • 1
    I also had to add `$mail->Port = 587;` and it works. – Devilquest Sep 20 '14 at 10:38
  • Interesting, I was able to run it without specifying the port. Could be the GoDaddy server I deployed it on had set the default port automatically, or possibly because the message was generated and sent from the same server? I'm curious now, but I'm glad you got the code to work :) Cheers! – CH3M Sep 21 '14 at 14:39
  • I've added the port specification to provide a more complete answer :) – CH3M Jan 27 '15 at 15:17
  • Finally, the port which you will use is determined by the mail server you are connecting to, so unless you are a system admin for the server, you will likely be forced to use whichever port the server is configured to send/receive on. Just FYI. – CH3M Jan 28 '16 at 16:12
  • Hello @CH3M I am using phpmailer for sending email to my gmail account but i am stuck with this error :: Mailer Error: SMTP connect() failed. i tried your code but, same error coming. – always-a-learner Mar 26 '16 at 09:49
  • @ankitsuthar Did you remember to change the host to Gmail's SMTP address? Also, be sure that $username and $password are properly set before the above code block executes. – CH3M Apr 22 '16 at 21:53