Swift-Mailer error, "Address in mailbox given [] does not comply with RFC"

22,130

All your data variables (addresses, names...) appear to be global. Global variables cannot be read from within functions unless you pass them as parameters (the recommended way) or use the global keyword (or the $GLOBALS array).

Share:
22,130
Maverick
Author by

Maverick

Updated on June 29, 2020

Comments

  • Maverick
    Maverick almost 4 years

    I have built a simple PHP contact form that is supposed to send mail trough the Swift-Mailer script.

    Problem is I keep getting this error

    Uncaught exception 'Swift_RfcComplianceException' with message 'Address in mailbox given [] does not comply with RFC 2822, 3.6.2.'

    Which I guess means I am using an invalid e-mail address. But since I am using [email protected] to test the scrip the problem is probably somewhere else. This is my configuration:

    Where the mail is sent to:

    $my_mail = '[email protected]';
    $my_name = 'My Name';
    

    The content of the message:

    $name = trim($_POST['name']);
    $email = trim($_POST['email']);
    $message = trim($_POST['message']);
    $date = date('d/m/Y H:i:s'); 
    $ipaddress = $_SERVER['REMOTE_ADDR'];  
    
    $content = $message.'\n\nSent on: '.$date.' From: '.$ipaddress;
    

    The function i use to send the mail using swiftmailer:

    function send_mail () {
    require('/path/to/swift_required.php');
    
    //The means of transport
    $transport = Swift_SmtpTransport::newInstance('mail.mydomain.com', 25);
    $transport->setUsername('myusername');
    $transport->setPassword('mypass');
    
    $mailer = Swift_Mailer::newInstance($transport);
    
    //The message
    $mail = Swift_Message::newInstance();
    $mail->setSubject('Hello');
    $mail->setFrom(array($email => $name ));
    $mail->setTo(array($my_mail => $my_name));
    $mail->setBody($content, 'text/plain');
    
    //Sending the message
    $test = $mailer->send($mail);
    
    if ($test) {
        echo '<p>Thank you for contacting us '.$name.'! We will get in touch soon.</p>';
    }
    else {
              echo '<p>Something went wrong. Please try again later.</p>';
    }
    }
    

    As you can see it is really simple form with three fields, name, mail and message. I also have other validation set up for each of contact form fields, but I think it is of little concern here.

    Thank you for the help.

    Edit: Just test with using gmail as the smtp server. Unfortunately it still gives the same exact results.

  • TarangP
    TarangP over 3 years
    but how to define global varialble in Mail::send('filename' , $data, function($message)) { .... ?
  • Álvaro González
    Álvaro González over 3 years
    @TarangP My tip is that you forget about globals, they just make code harder to write and maintain.