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.

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

报告相同问题?

悬赏问题

  • ¥15 有赏,i卡绘世画不出
  • ¥15 如何用stata画出文献中常见的安慰剂检验图
  • ¥15 c语言链表结构体数据插入
  • ¥40 使用MATLAB解答线性代数问题
  • ¥15 COCOS的问题COCOS的问题
  • ¥15 FPGA-SRIO初始化失败
  • ¥15 MapReduce实现倒排索引失败
  • ¥15 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)
  • ¥15 找一位技术过硬的游戏pj程序员
  • ¥15 matlab生成电测深三层曲线模型代码