0

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 Answers1

2

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.

Wolen
  • 874
  • 6
  • 15
  • I tried using the version you mentioned. The above issue is now gone. But now it is showing another error "Call to undefined method Swift_Message::newInstance()". Can you please help? – CoolJavaProgrammer Jan 03 '18 at 21:34
  • @CoolJavaProgrammer what is ```newInstance()``` method? There's no static method ```newInstance()```. You create new instance of class using ```new``` keyword. [This doc](https://swiftmailer.symfony.com/docs/messages.html) will help you. – Wolen Jan 03 '18 at 21:39
  • this issue is also solved from the link you provided. Now I am getting error "Class Egulias/EmailValidator/EmailValidator.php does not exist". I am using dynamic recipient addresses. Is this class mandatory? – CoolJavaProgrammer Jan 03 '18 at 21:57