PHPMailer works only in some versions [closed]

1

I have server with IIS. The PHP that I used is 5.3.28 but I did upgrade and today I use PHP 7.0.0 but PHPMailer does not work for this version, but when I configure ISS to 5.3.28 PHPMailer goes back to work.

How to configure for PHPMailer to work in PHP 7.0.0

    
asked by anonymous 07.12.2017 / 19:18

1 answer

2

You've probably downloaded PHPMailer from an unofficial source or from a very old source, so PHP7 has changed a lot, you have to download PHPMailer from the correct source.

Download this link link the last, which should be 6.0.2

Then in your php script add:

<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

require 'path/to/PHPMailer/src/Exception.php';
require 'path/to/PHPMailer/src/PHPMailer.php';
require 'path/to/PHPMailer/src/SMTP.php';
  

Change path/to to the path where you saved phpmailer

The rest of the script should look something like this:

$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 = 'smtp1.example.com;smtp2.example.com';  // Specify main and backup SMTP servers
    $mail->SMTPAuth = true;                               // Enable SMTP authentication
    $mail->Username = '[email protected]';                 // SMTP username
    $mail->Password = 'secret';                           // SMTP password
    $mail->SMTPSecure = 'tls';                            // Enable TLS encryption, 'ssl' also accepted
    $mail->Port = 587;                                    // TCP port to connect to

    //Recipients
    $mail->setFrom('[email protected]', 'Mailer');
    $mail->addAddress('[email protected]', 'Joe User');     // Add a recipient
    $mail->addAddress('[email protected]');               // Name is optional
    $mail->addReplyTo('[email protected]', 'Information');
    $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 = 'Here is the subject';
    $mail->Body    = 'This is the HTML message body <b>in bold!</b>';
    $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;
}

Email without SSL / TLS

If the account that will send the email via SMTP does not have / support security / encryption then you should add these lines:

$mail->SMTPSecure = false;
$mail->SMTPAutoTLS = false;

And remove this:

$mail->SMTPSecure = 'tls';
    
07.12.2017 / 19:25