Swift-Mailer错误,“给定邮箱的地址[]”不符合RFC“ [英] Swift-Mailer error, "Address in mailbox given [] does not comply with RFC"

查看:4219
本文介绍了Swift-Mailer错误,“给定邮箱的地址[]”不符合RFC“的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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


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

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

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

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:

邮件发送到:

$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发送邮件的功能:

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.

感谢您的帮助。

编辑:
只需使用gmail作为smtp服务器进行测试。

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

推荐答案

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

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

这篇关于Swift-Mailer错误,“给定邮箱的地址[]”不符合RFC“的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆