-1

Trying to set up PHPMailer for a site I have hosted on Bluehost and after a full day of researching and troubleshooting I just can not get it working.

I'm a beginner so apologies in advance for the newbie question, but I've been reading everything I can find (including this and this, as well as the PHPMailer docs) to solve this but can't seem to get mine set up correctly. Any guidance, thoughts on what I'm doing wrong or ways to debug this are much appreciated.

This is what I've found regarding Bluehost SMTP.

Secure SSL/TLS Settings (Recommended)

Username               Your email address: john@example.com
Password                The password for that email account.
Incoming Server      mail.example.com*
Incoming Port          993 (IMAP) or 995 (POP3)
Outgoing Server      mail.example.com*
Outgoing Port           465 (SMTP)
Authentication          Password

*Replace example.com with your domain name.

Below is what I'm using in my file (personal info removed).

<?php

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

//Load Composer's autoloader
require 'vendor/autoload.php';

$mail = new PHPMailer(true);     
try {
    //Server settings
    $mail->SMTPDebug = 2;             
    $mail->isSMTP();                  
    $mail->Host = 'mail.MYDOMAIN.com';  
    $mail->SMTPAuth = true;            
    $mail->Username = 'MYEMAIL@MYDOMAIN.com';   
    $mail->Password = 'MYEMAILPASSWORD';        
    $mail->SMTPSecure = 'tls';                  
    $mail->Port = 465;                          

    //Recipients
    $mail->setFrom('MYEMAIL@MYDOMAIN.com');
    $mail->addAddress('MYEMAIL@MYDOMAIN.com'); 

    //Content
    $mail->isHTML(true);            
    $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. Mailer Error: ', $mail->ErrorInfo;
}

I've installed PHPMailer using Composer so the autoload.php file (and the phpmailer folder) are in a 'vendor' folder located in the same directory as the file that contains the code shown above.

After uploading to my Bluehost server, when I try to display the webpage that has this code in the browser, I get this HTTP ERROR 500 screenshot and of course no email sent.

Error screen shot

Synchro
  • 35,538
  • 15
  • 81
  • 104
Nick
  • 3
  • 3
  • The first thing you always do on a 500, is check the server’s error logs! – misorude Nov 27 '18 at 08:23
  • The error logs revealed `PHP Parse error: syntax error, unexpected T_STRING, expecting T_CONSTANT_ENCAPSED_STRING or '(' in FILENAME on line 4` After seeing the answer to [this question](https://stackoverflow.com/questions/13960277/error-parse-error-syntax-error-unexpected-t-string-expecting-t-constant-encap), I contacted Bluehost and realized that my my site was running PHP5.2 so I updated to PHP7 and now it is working! :) – Nick Nov 27 '18 at 11:59

1 Answers1

1

Something that's covered in many of the examples and the docs is what combinations of encryption and port settings will work.

You have Port = 465 and SMTPSecure = 'tls'; that won't work. Either change Port to 587, or SMTPSecure to 'ssl' (but not both!). As it stands, you're trying to open a connection to a port that expects implicit TLS while using a protocol that expects to need to make it explicit with STARTTLS, and that's not going to work.

Synchro
  • 35,538
  • 15
  • 81
  • 104
  • This was very helpful, as well as reviewing the server error logs as @misorude suggested above. – Nick Nov 27 '18 at 12:00
  • If you're happy with this answer, please select it as your chosen answer using the checkmark next to it. – Synchro Nov 27 '18 at 17:00