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!