Sending email Problem

Website URL

https://www.rstrader.net, its a family investing website.

Error Message

The site loads for a while and then gives an 502 Gateway nginx error

Other Information

I have using Amazon for a while with this files and always have worked pretty great, but when I intergrated the website files here, is giving me this problem.

This problem always occours when the website needs to send an email.

The website uses PHPMailer and as SMTP uses Yandex SMTP.

I know its not an DNS problem because database, website respondes as we can see at https://rstrader.net/dns.php.

I dont know if its an script problem because it was running perfectly on Amazon AWS. And I have looking at it and I didnt notice any programming problem, and looks like neither VS Code does.

So I dont know what to do more, i got run out of ideas, so I need suggestions.

Ps: Im a brand new Web Developer haha’ go smooth with the answers.

Have you tried turning on PHPMailer SMTP Debug?

If that doesn’t help, can you share the code you have (making sure not to show any passwords) ?

1 Like

The debug system kind of it didnt happen. sounding like the error came before it but here is the code anyway.

its set to grab the information from database

    <?php
// Import PHPMailer classes into the global namespace
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;

// Load Composer's autoloader

require 'vendor/autoload.php';

class Mailer
{
    private $smtp_host = '';
    private $smtp_user = '';
    private $smtp_pass = '';
    private $smtp_port = '';
    private $name = '';
    private $from = '';
    private $to = '';
    private $replyto = '';
    private $subject = '';
    private $msg = '';

    public function __construct($config = false) {

        if (isset($config['smtp_host']))  $this->smtp_host = $config['smtp_host']; 
        if (isset($config['smtp_user']))  $this->smtp_user = $config['smtp_user']; 
        if (isset($config['smtp_pass']))  $this->smtp_pass = $config['smtp_pass']; 
        if (isset($config['smtp_port']))  $this->smtp_port = $config['smtp_port']; 
        if (isset($config['name']))  $this->name = $config['name']; 
        if (isset($config['from']))  $this->from = $config['from']; 
        if (isset($config['to']))  $this->to = $config['to']; 
        if (isset($config['replyto']))  $this->replyto = $config['replyto']; 
        if (isset($config['subject']))  $this->subject = $config['subject']; 
        if (isset($config['msg']))  $this->msg = $config['msg']; 
    }

    public function send() {
        // Instantiation and passing `true` enables exceptions
        $mail = new PHPMailer(true);

        try {
            //Server settings
            $mail->SMTPDebug = SMTP::DEBUG_SERVER;                      // Enable verbose debug output
            $mail->isSMTP();                                              // Send using SMTP
            $mail->Host       = $this->smtp_host;                         // Set the SMTP server to send through
            $mail->SMTPAuth   = true;                                     // Enable SMTP authentication
            $mail->Username   = $this->smtp_user;                         // SMTP username
            $mail->Password   = $this->smtp_pass;                         // SMTP password
            $mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;           // Enable TLS encryption; `PHPMailer::ENCRYPTION_SMTPS` encouraged
            $mail->Port       = $this->smtp_port;                         // TCP port to connect to, use 465 for `PHPMailer::ENCRYPTION_SMTPS` above

            //Recipients
            $mail->setFrom($this->from, $this->name);
            //$mail->addAddress('joe@example.net', 'Joe User');     // Add a recipient
            $mail->addAddress($this->to);               // Name is optional
            $mail->addReplyTo($this->replyto, 'Support');
            //$mail->addCC('cc@example.com');
            //$mail->addBCC('bcc@example.com');

            // 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 = $this->subject;
            $mail->Body    = $this->msg;
            //$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';

            if ($mail->send()) {
                return true;
            } else {
                return false;
            }
            //echo 'Message has been sent';
            return TRUE;
        } catch (Exception $e) {
            //echo "Não foi possível enviar o email. Erro do Envio: {$mail->ErrorInfo}";
            return FALSE;
        }
    }
}

This topic was automatically closed 15 days after the last reply. New replies are no longer allowed.