我正在尝试使用SwiftMailer从我的应用程序发送电子邮件。我在项目文件夹中使用git和以下命令安装了SwiftMailer包:
git clone https://github.com/swiftmailer/swiftmailer.git
这告诉我swiftmailer在我的项目文件夹中正确克隆。现在,当我尝试从我的应用程序发送电子邮件时,它显示以下错误:
<b>Parse error</b>: syntax error, unexpected '?' in <b>C:\xampp\htdocs\myAppPath\swiftmailer\lib\classes\Swift\Transport\EsmtpTransport.php</b> on line <b>211</b><br />
当我尝试进入EsmtpTransport.php文件时,我发现了以下代码:
/**
* Returns the IP used to connect to the destination.
*
* @return string
*/
public function getSourceIp()
{
return $this->params['sourceIp'] ?? null; //*line number 211*
}
我在申请表中编写的代码如下:
<?php
$subject = 'SwiftMailer Test!';
$message = 'Hi! This is a test email from SwiftMailer';
$usernameEmail = "username";
$passwordEmail = "password";
try{
$transport = (new Swift_SmtpTransport('smtp.example.com', 25))
->setUsername($usernameEmail)
->setPassword($passwordEmail)
;
$message = Swift_Message::newInstance();
$message->setTo(array(
"recipient@somemail.com" => "Recipient Name"
));
$message->setSubject($subject);
$message->setBody($message);
$message->setFrom("email@example.com", "My App Team");
$mailer = Swift_Mailer::newInstance($transport);
$mailer->send($message, $failedRecipients);
?>
我无法弄清楚问题并尝试搜索网络,但遗憾的是还没有成功。有人可以帮忙吗?提前谢谢!
答案 0 :(得分:1)
代码中有拼写错误。他们想要使用三元运算符,而是:
他们放置第二个?
。你没有使用作曲家,所以你可以自己修复它。只需改变
return $this->params['sourceIp'] ?? null; //*line number 211*
到
return $this->params['sourceIp'] ?: null; //*line number 211*
或从here下载v6.0.1
,免除此错误。
@edit 这不是一个错字。这是Null coalescing operator。它已经在PHP7.0中引入,所以我想你使用的是旧版本的PHP。您应该使用PHP7 +,因为swiftmailer针对此版本。