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 myaddress@gmail.com to test the scrip the problem is probably somewhere else. This is my configuration:

Where the mail is sent to:

$my_mail = 'mymail@mydomain.com';
$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.

Maverick
  • 1,988
  • 5
  • 24
  • 31
  • 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. – Marc B Jun 28 '11 at 14:30
  • 2
    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! – hakre Jun 28 '11 at 14:39
  • @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... – Maverick Jun 28 '11 at 14:41
  • @hakare ok I left only the trim function, but i still get the same error – Maverick Jun 28 '11 at 14:44

1 Answers1

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

Álvaro González
  • 142,137
  • 41
  • 261
  • 360