Below is the sample code where first time calling is working but second time similar thing failing to send mail: You can see both of the functions are symmetric.
<?php
include_once "Swift-5.1.0/lib/swift_required.php";
$transport = Swift_SmtpTransport::newInstance('smtp.sendgrid.net', 587);
$transport->setUsername('Username');//smtp Access
$transport->setPassword('Password');//smtp Access
$swift = Swift_Mailer::newInstance($transport);
function call1(){
//...subject/from/to/text/html
global $swift;
$message=NULL;
$message = new Swift_Message($subject);
$headers = $message->getHeaders();
$message->setFrom($from);
$message->setTo($to);
$htmlPart = new Swift_MimePart($html, 'text/html');
$message->attach($htmlPart);
$textPart = new Swift_MimePart($text, 'text/plain');
$message->attach($textPart);
if ($recipients = $swift->send($message, $failures))
{
$mailsent = true;
return true;
} else {
$mailsent = false;
return false;
}
}
function call2(){
//...subject/from/to/text/html
global $swift;
$message=NULL;
$message = new Swift_Message($subject);
$headers = $message->getHeaders();
$message->setFrom($from);
$message->setTo($to);
$htmlPart = new Swift_MimePart($html, 'text/html');
$message->attach($htmlPart);
$textPart = new Swift_MimePart($text, 'text/plain');
$message->attach($textPart);
if ($recipients = $swift->send($message, $failures))
{
$mailsent = true;
return true;
} else {
$mailsent = false;
return false;
}
}
call1();//Working
call2();//Not Working
call1();//Not Working
call2();//Not Working
?>