Phpmailer works only on some versions

Asked

Viewed 1,642 times

1

I have an IIS server. The PHP I used is the 5.3.28 however I went to upgrade and today I use PHP 7.0.0 but Phpmailer does not work for this version, but when I set the ISS to 5.3.28 Phpmailer works again.

How to set up Phpmailer to work in PHP 7.0.0

1 answer

3

You 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 from this link https://github.com/PHPMailer/PHPMailer/releases the last, which should be 6.0.2

So 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 the way you saved the 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';

Browser other questions tagged

You are not signed in. Login or sign up in order to post.