title: Send email using SMTP in PHP with PHPMailer
body: <p>I tryed this code:</p>
  
  <pre><code>&lt;?php
  
  // 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\Exception;
  
  //Load composer's autoloader
  require '/home/username/public_html/vendor/autoload.php';
  
  
  
  /**
       * @param type $array
       * @param type $int
       * @return type
       */
      function send_mail_customer($nickname, $email, $total_value)
      {
          $to      = $email;
          $subject = 'DISTRIBUIÇÃO DE LUCRO: ' . $nickname ;
  
          $from = "myname@mydomain.com";
          $name = $nickname;
  
          $message = "&lt;html&gt;&lt;body&gt;";
          $message .= "&lt;H1&gt; DISTRIBUIÇÃO MENSAL DE LUCRO &lt;/H1&gt;";
          $message .= "&lt;strong&gt;Total ganho:&lt;/strong&gt; " .$total_value. " euros";
          $message .= "&lt;/body&gt;&lt;/html&gt;";
  
  
          $mail = new PHPMailer(true);                              // Passing `true` enables exceptions
          try {
              //Server settings
              $mail-&gt;SMTPDebug = 2;                                 // Enable verbose debug output
              $mail-&gt;isSMTP();                                      // Set mailer to use SMTP
              $mail-&gt;Host = 'mail.mydomain.com';                   // Specify main and backup SMTP servers
              $mail-&gt;SMTPAuth = true;                               // Enable SMTP authentication
              $mail-&gt;Username = 'Myname@mydomain.com';          // SMTP username
              $mail-&gt;Password = 'Mypassword';                       // SMTP password
              $mail-&gt;SMTPSecure = 'ssl';                            // Enable TLS encryption, `ssl` also accepted
              $mail-&gt;Port = 465;                                    // TCP port to connect to 587
  
              //Recipients
              $mail-&gt;setFrom($from, 'MyDomain');
              $mail-&gt;addAddress($to, $nickname);     // Add a recipient
              //$mail-&gt;addAddress('contact@example.com');               // Name is optional
              $mail-&gt;addReplyTo($from, 'MyDomain');
              //$mail-&gt;addCC('cc@example.com');
              //$mail-&gt;addBCC('bcc@example.com');
  
              //Attachments
              //$mail-&gt;addAttachment('/var/tmp/file.tar.gz');         // Add attachments
              //$mail-&gt;addAttachment('/tmp/image.jpg', 'new.jpg');    // Optional name
  
              //Content
              $mail-&gt;isHTML(true);                                  // Set email format to HTML
              $mail-&gt;Subject = $subject;
              $mail-&gt;Body    = $message;
              //$mail-&gt;AltBody = 'This is the body in plain text for non-HTML mail clients';
  
              $mail-&gt;send();
              echo 'Message has been sent';
          } catch (Exception $e) {
              echo 'Message could not be sent.';
              echo 'Mailer Error: ' . $mail-&gt;ErrorInfo;
          }
  
      }
  
      $x="NunoSousa";
      $email="myemail@gmail.com";
      $total_value = 15;
      send_mail_customer($x, $email, $total_value);
  
  ?&gt;
  </code></pre>
  
  <p>And I receive the error:</p>
  
  <blockquote>
    <p>2018-09-26 08:06:21 SMTP ERROR: Failed to connect to server: (0) SMTP
    connect() failed.
    <a href="https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting" rel="nofollow noreferrer">https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting</a> Message
    could not be sent.Mailer Error: SMTP connect() failed.
    <a href="https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting" rel="nofollow noreferrer">https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting</a></p>
  </blockquote>
  
  <p>I read the wiki but I just can't see what's wrong. I tried also TLS encryption with the port 587 and the issue still the same.</p>