Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error : "501 5.5.4 Invalid domain name" when I attempt to send an email using PHP + Swiftmailer

I'm writing a very simple contact form that will allow blue collar workers to submit safety concern notices over our intranet. Contact form is displayed in HTML and asks for a Name, a From email, and the message to be sent. It always sends to our safety email.

The server and port are correct. It's an Exchange 2010 server, and we're using TSL. The server is configured to be able to receive email 'anonymously'. I can connect through the telnet command but I receive a "501 5.5.4 Invalid domain name" error when I try to send mail through the comment box.

define("EMAIL_SUBJECT", "Safety Concerns");     
    define("EMAIL_TO", "email");        

    // SMTP Configuration
    define("SMTP_SERVER", 'server');                
    define("SMTP_PORT", 25);                                

    // define("UPLOAD_DIR", '/var/www/tmp/');           // Default php upload dir

    // main method. It's the first method called
    function main($contactForm) {

        // Checks if something was sent to the contact form, if not, do nothing
        if (!$contactForm->isDataSent()) {
            return;
        }

        // validates the contact form and initialize the errors
        $contactForm->validate();

        $errors = array();

        // If the contact form is not valid:
        if (!$contactForm->isValid()) {
            // gets the error in the array $errors
            $errors = $contactForm->getErrors();

        } else {
            // If the contact form is valid:
            try {               
                // send the email created with the contact form
                $result = sendEmail($contactForm);              

                // after the email is sent, redirect and "die".
                // We redirect to prevent refreshing the page which would resend the form
                header("Location: ./success.php");
                die();
            } catch (Exception $e) {
                // an error occured while sending the email. 
                // Log the error and add an error message to display to the user.
                error_log('An error happened while sending email contact form: ' . $e->getMessage());
                $errors['oops'] = 'Ooops! an error occured while sending your email! Please try again later!';
            }

        }

        return $errors;
    }

    // Sends the email based on the information contained in the contact form
    function sendEmail($contactForm) {
        // Email part will create the email information needed to send an email based on 
        // what was inserted inside the contact form
        $emailParts = new EmailParts($contactForm);

        // This is the part where we initialize Swiftmailer with 
        // all the info initialized by the EmailParts class
        $emailMessage = Swift_Message::newInstance()
        ->setSubject($emailParts->getSubject())
        ->setFrom($emailParts->getFrom())
        ->setTo($emailParts->getTo())
        ->setBody($emailParts->getBodyMessage());

        // If an attachment was included, add it to the email
        // if ($contactForm->hasAttachment()) {
        //  $attachmentPath = $contactForm->getAttachmentPath();
        //  $emailMessage->attach(Swift_Attachment::fromPath($attachmentPath));
        //}

        // Another Swiftmailer configuration..
        $transport = Swift_SmtpTransport::newInstance(SMTP_SERVER, SMTP_PORT, 'tls');
        $mailer = Swift_Mailer::newInstance($transport);
        $result = $mailer->send($emailMessage);
        return $result;
    }

    // Initialize the ContactForm with the information of the form and the possible uploaded file.
    $contactForm = new ContactForm($_POST, $_FILES);

    // Call the "main" method. It will return a list of errors. 
    $errors = main($contactForm);

    require_once("./views/contactForm.php");
like image 671
Joshua Avatar asked Sep 28 '22 22:09

Joshua


1 Answers

The answer is simple, if mail host field has an FQDN defined(xx.yy.com) instead of an IP address, the server should be able to resolve the FQDN. Else it would trigger an error called Invalid domain name.

Hope this helps!

like image 154
Dilraj Rajan Avatar answered Oct 06 '22 18:10

Dilraj Rajan