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

1

7

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.

Zhukov answered 28/6, 2011 at 14:25 Comment(4)
Dump out the content of $email and $my_email after you've done your trim/htmlspecialchars/strip_tags work and see what they looks like. The swiftmailer code itself looks ok, so possibly the addresses are getting mangled somewhere before they reach swiftmailer.Autolysis
And remove both, strip_tags and htmlspecialchars. Those two functions are not designed to work with email addresses and names but with HTML. I think you're mixing concepts here. Do proper escaping, not brain-dead escaping. Know what you do and why!Spray
@Marc_B That was what i suspected also. But they were correct... My other guess is could the error refer not to the sent to address but the receiver address? i will try testing with a gmail address as receiver and with gmail smtp server as well...Zhukov
@hakare ok I left only the trim function, but i still get the same errorZhukov
B
2

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).

Beau answered 28/6, 2011 at 14:51 Comment(2)
but how to define global varialble in Mail::send('filename' , $data, function($message)) { .... ?Ramos
@Ramos My tip is that you forget about globals, they just make code harder to write and maintain.Dressing

© 2022 - 2024 — McMap. All rights reserved.