我构建了一个简单的 PHP 联系表单,该表单应该通过 Swiftmailer 脚本发送邮件。
问题是我不断收到此错误
未捕获的异常
'Swift_RfcComplianceException' 与
message '给定邮箱中的地址[]
不符合 RFC 2822, 3.6.2。'
我想这意味着我使用了无效的电子邮件地址。但由于我正在使用[电子邮件受保护] /cdn-cgi/l/email-protection为了测试该纸条,问题可能出在其他地方。这是我的配置:
邮件发送至:
$my_mail = '[email protected] /cdn-cgi/l/email-protection';
$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 服务器进行测试。不幸的是,它仍然给出相同的精确结果。
您的所有数据变量(地址、名称...)似乎都是全局的。全局变量无法从函数内部读取,除非将它们作为参数传递(推荐方式)或使用global
关键字(或$GLOBALS
array).
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)