Troubleshooting SMTP Error 504 When Sending Email with Attachment

49 views Asked by At

Description: I encountered a 504 error when attempting to send an email with an attachment using SMTP over SSL (port 465). However, when sending emails without attachments or using a localhost setup, no errors occurred. I suspect the issue might be related to either my code or the server configuration. Can someone assist me in resolving this issue?

Background Research:

  1. Checked SMTP server status to ensure it's operational.

  2. Verified SSL/TLS certificate validity and installation.

  3. Reviewed firewall settings to ensure outbound connections on port 465 are allowed.

  4. Confirmed credentials used for SMTP authentication.

  5. Checked attachment size to ensure it doesn't exceed server limits.

  6. Reviewed code configuration for SMTP settings, including hostname, port, encryption method, and authentication.

  7. Enabled debugging/logging in the code to capture detailed SMTP communication information.

  8. Tested sending emails with attachments using different email clients or libraries.

  9. Reviewed SMTP server logs for any error messages or warnings.

  10. Considered using an alternate SMTP server to troubleshoot compatibility issues.

Question: Is the 504 error when sending emails with attachments via SMTP over SSL likely caused by issues with my code configuration or the server setup?

 public function send_email($to, $subject, $message, $from = null, $from_name = null, $attachment = null, $cc = null, $bcc = null)
{
    list($user, $domain) = explode('@', $to);
    if ('tecdiary.com' != $domain || DEMO) {
        $result = false;
        $this->load->library('tec_mail');
        try {
            $result = $this->tec_mail->send_mail($to, $subject, $message, $from, $from_name, $attachment, $cc, $bcc);
        } catch (\Exception $e) {
            $this->session->set_flashdata('error', 'Mail Error: ' . $e->getMessage());
            throw new \Exception($e->getMessage()); 
        }
        return $result;
    }
    return false;
}

tec_mail

class Tec_mail
{
public function __construct()
{
}

public function __get($var)
{
    return get_instance()->$var;
}

public function send_mail($to, $subject, $body, $from = null, $from_name = null, $attachment = null, $cc = null, $bcc = null)
{
    // $mail = new PHPMailer;
    $mail          = new PHPMailer(true);
    $mail->CharSet = 'UTF-8';
    try {
        
        $mail->isSMTP();
        $mail->Host     = 'smtp.gmail.com';
        $mail->SMTPAuth = true;
        $mail->Port     = 465;
        $mail->Username = '';
        $mail->Password = '';
            
        if (DEMO) {
            
            $mail->isSMTP();
            $mail->Host     = 'smtp.gmail.com';
            $mail->SMTPAuth = true;
            $mail->Port     = 465;
            $mail->Username = '';
            $mail->Password = '';

        // $mail->SMTPDebug = 2;
        } elseif ($this->Settings->protocol == 'mail') {
            $mail->isMail();
        } elseif ($this->Settings->protocol == 'sendmail') {
            $mail->isSendmail();
        } elseif ($this->Settings->protocol == 'smtp') {
            $mail->isSMTP();
            $mail->Host       = $this->Settings->smtp_host;
            $mail->SMTPAuth   = true;
            $mail->SMTPSecure = !empty($this->Settings->smtp_crypto) ? $this->Settings->smtp_crypto : false;
            $mail->Port       = $this->Settings->smtp_port;
            if (isset($this->Settings->smtp_oauth2)) {
                $email        = $this->Settings->smtp_user;
                $clientId     = $this->config->item('client_id');
                $clientSecret = $this->config->item('client_secret');
                $refreshToken = $this->config->item('refresh_token');

                $this->mail->AuthType = 'XOAUTH2';

                $provider = new Google(['clientId' => $clientId, 'clientSecret' => $clientSecret]);

                $this->mail->setOAuth(new OAuth([
                    'provider'     => $provider,
                    'clientId'     => $clientId,
                    'clientSecret' => $clientSecret,
                    'refreshToken' => $refreshToken,
                    'userName'     => $email,
                ]));
            } else {
                $mail->Username = $this->Settings->smtp_user;
                $mail->Password = $this->Settings->smtp_pass;
            }
        } else {
            $mail->isMail();
        }

        if ($from && $from_name) {
            $mail->setFrom($from, $from_name);
            $mail->addReplyTo($from, $from_name);
        } elseif ($from) {
            $mail->setFrom($from, $this->Settings->site_name);
            $mail->addReplyTo($from, $this->Settings->site_name);
        } else {
            $mail->setFrom($this->Settings->default_email, $this->Settings->site_name);
            $mail->addReplyTo($this->Settings->default_email, $this->Settings->site_name);
        }

        $mail->addAddress($to);
        if ($cc) {
            try {
                if (is_array($cc)) {
                    foreach ($cc as $cc_email) {
                        $mail->addCC($cc_email);
                    }
                } else {
                    $mail->addCC($cc);
                }
            } catch (\Exception $e) {
                log_message('info', 'PHPMailer Error: ' . $e->getMessage());
            }
        }
        if ($bcc) {
            try {
                if (is_array($bcc)) {
                    foreach ($bcc as $bcc_email) { 
                        $mail->addBCC($bcc_email);
                    }
                } else {
                    $mail->addBCC($bcc);
                }
            } catch (\Exception $e) {
                log_message('info', 'PHPMailer Error: ' . $e->getMessage());
            }
        }
        $mail->Subject = $subject;
        $mail->isHTML(true);
        $mail->Body    = $body;
        $mail->AltBody = strip_tags($mail->Body);
        
        if ($attachment) { 
            if (is_array($attachment)) {
                foreach ($attachment as $attach) {
                    $mail->addAttachment($attach);
                }
            } else {
                $mail->addAttachment($attachment);
            }
        }

        if (!$mail->send()) {
            log_message('error', 'Mail Error: ' . $mail->ErrorInfo);
            throw new Exception($mail->ErrorInfo);
        }
        return true;
    } catch (MailException $e) {
        log_message('error', 'Mail Error: ' . $e->getMessage());
        throw new \Exception($e->errorMessage());
    } catch (\Exception $e) {
        log_message('error', 'Mail Error: ' . $e->getMessage());
        throw new \Exception($e->getMessage());
    }
}

}

0

There are 0 answers