0

I tryed this code:

<?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 = "<html><body>";
        $message .= "<H1> DISTRIBUIÇÃO MENSAL DE LUCRO </H1>";
        $message .= "<strong>Total ganho:</strong> " .$total_value. " euros";
        $message .= "</body></html>";


        $mail = new PHPMailer(true);                              // Passing `true` enables exceptions
        try {
            //Server settings
            $mail->SMTPDebug = 2;                                 // Enable verbose debug output
            $mail->isSMTP();                                      // Set mailer to use SMTP
            $mail->Host = 'mail.mydomain.com';                   // Specify main and backup SMTP servers
            $mail->SMTPAuth = true;                               // Enable SMTP authentication
            $mail->Username = 'Myname@mydomain.com';          // SMTP username
            $mail->Password = 'Mypassword';                       // SMTP password
            $mail->SMTPSecure = 'ssl';                            // Enable TLS encryption, `ssl` also accepted
            $mail->Port = 465;                                    // TCP port to connect to 587

            //Recipients
            $mail->setFrom($from, 'MyDomain');
            $mail->addAddress($to, $nickname);     // Add a recipient
            //$mail->addAddress('contact@example.com');               // Name is optional
            $mail->addReplyTo($from, 'MyDomain');
            //$mail->addCC('cc@example.com');
            //$mail->addBCC('bcc@example.com');

            //Attachments
            //$mail->addAttachment('/var/tmp/file.tar.gz');         // Add attachments
            //$mail->addAttachment('/tmp/image.jpg', 'new.jpg');    // Optional name

            //Content
            $mail->isHTML(true);                                  // Set email format to HTML
            $mail->Subject = $subject;
            $mail->Body    = $message;
            //$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';

            $mail->send();
            echo 'Message has been sent';
        } catch (Exception $e) {
            echo 'Message could not be sent.';
            echo 'Mailer Error: ' . $mail->ErrorInfo;
        }

    }

    $x="NunoSousa";
    $email="myemail@gmail.com";
    $total_value = 15;
    send_mail_customer($x, $email, $total_value);

?>

And I receive the error:

2018-09-26 08:06:21 SMTP ERROR: Failed to connect to server: (0) SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting Message could not be sent.Mailer Error: SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting

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.

Nuno Sousa
  • 1
  • 1
  • 2
  • Possible duplicate of [send email using Gmail SMTP server through PHP Mailer](https://stackoverflow.com/questions/16048347/send-email-using-gmail-smtp-server-through-php-mailer) – Tiago Martins Peres Sep 26 '18 at 13:19

3 Answers3

0

Change

 $mail->SMTPSecure = 'ssl';  

To

 $mail->SMTPSecure = 'tls'; 
 $mail->Host = 'smtp.gmail.com'; 
 $mail->Port = 587; 
Rp9
  • 1,955
  • 2
  • 23
  • 31
  • I used TLS in port 587 and it gives me the error: 26 Sep 2018 08:49:02 -0500 220-We do not authorize the use of this system to transport unsolicited, 220 and/or bulk e-mail. – Nuno Sousa Sep 26 '18 at 13:50
0

Maybe your Host is wrong. In my case my host is named smtp.domain.com. Also i'm using tls with port 587, you could try again to connect using correct host.

In your case that would be

 $mail->Host = 'smtp.mydomain.com';
 $mail->SMTPSecure = 'tls';
 $mail->Port = 587;
Syles
  • 135
  • 11
  • I checked in cPanel. The host is mail.mydomain.com. With smtp.mydomain.com I receive the error: Failed to connect to server: php_network_getaddresses: getaddrinfo failed: Name or service not known (0) – Nuno Sousa Sep 26 '18 at 13:47
  • Can you find out the smtp address? Typically the mail provider have an address like `smtp.domain.com`. Or can you find some other information about smtp there? – Syles Sep 26 '18 at 13:49
  • From the cPanel POP/IMAP Server: mail.domain.com SMTP Settings: mail.domain.com – Nuno Sousa Sep 26 '18 at 14:10
  • Then it should working with `$mail->host = 'mail.domain.com'`, `$mail->SMTPSecure = 'tls'` and `$mail->Port = 587`. Maybe you have to enable access from external to this mail account? I remember doing it at the mail provider web.de for accessing ans sending mail through this account by outlook. – Syles Sep 26 '18 at 14:15
0

Your mail host, set to ip address of your server:

$mail->SMTPDebug = 0;  // Enable verbose debug output, default is 2
$mail->isSMTP();   // Set mailer to use SMTP
$mail->Host = ''; 
$mail->SMTPAuth = true;  // Enable SMTP authentication
$mail->Username = '';    // SMTP username
$mail->Password = '';    // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also 
                          //accepted
$mail->Port = 587; 
swatch360
  • 160
  • 2
  • 15