Swift-Mailer错误,“给出[]邮箱中的地址不符合RFC”

时间:2011-06-28 14:25:08

标签: php email swiftmailer

我已经构建了一个简单的PHP联系表单,该表单应该通过Swift-Mailer脚本发送邮件。

问题是我一直收到此错误

  

未捕获的异常   'Swift_RfcComplianceException'用   消息'给邮箱中的地址[]   不符合RFC 2822,3.6.2。'

我猜这意味着我使用了无效的电子邮件地址。但由于我使用myaddress@gmail.com来测试脚本,问题可能就在其他地方。这是我的配置:

邮件发送到:

$my_mail = 'mymail@mydomain.com';
$my_name = 'My Name';

讯息内容:

$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;

我用来使用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>';
}
}

正如您所看到的,它是一个非常简单的表单,包含三个字段,名称,邮件和消息。我还为每个联系表单字段设置了其他验证,但我认为这里没什么问题。

感谢您的帮助。

编辑: 只需使用gmail作为smtp服务器进行测试。不幸的是,它仍然给出了相同的结果。

1 个答案:

答案 0 :(得分:2)

您的所有数据变量(地址,名称......)似乎都是全局的。除非您将它们作为参数(推荐方式)传递或使用global关键字(或$GLOBALS数组),否则无法从函数中读取全局变量。