Send email using SMTP in PHP with PHPMailer

3k Views Asked by At

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 = "[email protected]";
        $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 = '[email protected]';          // 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('[email protected]');               // Name is optional
            $mail->addReplyTo($from, 'MyDomain');
            //$mail->addCC('[email protected]');
            //$mail->addBCC('[email protected]');

            //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="[email protected]";
    $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.

3

There are 3 best solutions below

3
On

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; 
1
On

Change

 $mail->SMTPSecure = 'ssl';  

To

 $mail->SMTPSecure = 'tls'; 
 $mail->Host = 'smtp.gmail.com'; 
 $mail->Port = 587; 
4
On

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;