0

I gonna send email via 365 using phpmailer lib. This is my config

MAIL_DRIVER=smtp
MAIL_HOST=smtp.office365.com
MAIL_PORT=587
MAIL_USERNAME=support@xxxxx.com
MAIL_PASSWORD=XXXXXX
MAIL_ENCRYPTION=STARTTLS

The following code is function for send email

 public function __construct()
    {
        date_default_timezone_set('America/Virgin');

        $this->mail = new \PHPMailer;

        $this->mail->isSMTP();         


        $this->mail->CharSet = 'UTF-8';
        //Ask for HTML-friendly debug output
        $this->mail->Debugoutput = 'html';
        //Set the hostname of the mail server
        $this->mail->Host = getenv("MAIL_HOST"); //'ssl://smtp.gmail.com';            
        $this->mail->Port = getenv("MAIL_PORT");
        //Set the encryption system to use - ssl (deprecated) or tls
        // $this->mail->SMTPSecure = 'tls';
        //Whether to use SMTP authentication
        $this->mail->SMTPAuth = true;
        $this->mail->Username = getenv("MAIL_USERNAME"); 
        $this->mail->Password = getenv("MAIL_PASSWORD");
        $this->mail->SMTPSecure = getenv("MAIL_ENCRYPTION");     
        $this->setSender(
            $this->sender_email,
            $this->sender_password,
            $this->sender_display_name,
            $this->sender_display_email,
            $this->sender_reply_to_name,
            $this->sender_reply_to_email
        );
    }

public function sendEmail($to_email, $to_name, $subject, $message, $template_name = '')
    {
        //Set who the message is to be sent to
        $this->mail->addAddress($this->trim_input($to_email), $this->trim_input($to_name));
        //Set the subject line
        $this->mail->Subject = $subject;
        //Read an HTML message body from an external file, convert referenced images to embedded,
        //convert HTML into a basic plain-text alternative body
        $this->mail->msgHTML($template_name);
        //Replace the plain text body with one created manually
        $this->mail->AltBody = $message;
        //send the message, check for errors
        if (!$this->mail->send()) {
            return false;
        // echo "Mailer Error: " . $this->mail->ErrorInfo;
        } else {
            return true;
        }
    }

I have tried to send email on my local server and digital ocean server In that case I have correctly sent and received email,

but in Godaddy server there is error

Mailer Error: SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting.

I had tried to changed email driver and another that previously mentioned via this platform Setting up PHPMailer with Office365 SMTP

How to fix that ? Thanks for your advance.

Loveun CG
  • 89
  • 3
  • 12

0 Answers0