douxiong3234 2018-01-03 21:05
浏览 112
已采纳

SwiftMailer中EsmtpTransport.php中的问题

I am trying to send email from my application using SwiftMailer. I have installed the SwiftMailer package using git with the following command in my project folder:

git clone https://github.com/swiftmailer/swiftmailer.git

This showed me that swiftmailer is cloned properly in my project folder. Now when I am trying to send email from my application, it is showing the following error:

 <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 />

When I tried to get into the EsmtpTransport.php file, I found the following code written:

 /**
 * Returns the IP used to connect to the destination.
 *
 * @return string
 */
public function getSourceIp()
{
    return $this->params['sourceIp'] ?? null;  //*line number 211*
}

The code I have written in my application is as below:

<?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);
?>

I am unable to figure out the issue and tried searching the net and unfortunately not successful yet. Can anyone please help? Thanks in advance!

  • 写回答

1条回答 默认 最新

  • duanqiang3925 2018-01-03 21:15
    关注

    there's a typo in the code. They wanted to use ternary operator but instead : they put second ?. You doesn't use composer so you can just fix it yourself. Just change

    return $this->params['sourceIp'] ?? null;  //*line number 211*
    

    to

    return $this->params['sourceIp'] ?: null;  //*line number 211*
    

    or download v6.0.1, free from this bug, from here.


    @edit It's not a typo. It's Null coalescing operator. It has been introduced in PHP7.0 so I suppose you're using an older version of PHP. You should use PHP7+ because swiftmailer is aimed for this versions.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 Java-Oj-桌布的计算
  • ¥15 请问如何在openpcdet上对KITTI数据集的测试集进行结果评估?
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路