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.
$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. – Autolysisstrip_tags
andhtmlspecialchars
. 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