php swiftmailer two message instances with one mailer instance? I created a simple function for swiftmailer to send two different messages for support and client. Not sure i did it the rigth way as I am using two different instances for message and one mailer instance. Everything works just fine but am I doing it the right way or I have to create separate mailer instance for each message? Here is my code, if someone could point out if it is ok or could be done better:

&lt;?php

if ((isset($_POST['email'])) &amp;&amp; (strlen(trim($_POST['email'])) &gt; 0)) {
    $email = stripslashes(strip_tags($_POST['email']));
} else {$email = 'No data';}

require_once '/home/sitename/public_html/inc/swift/lib/swift_required.php';

function send_email($info){     

    $transport = Swift_MailTransport::newInstance();

    $mailer = Swift_Mailer::newInstance($transport);   

    //Create the message for support
    $message = Swift_Message::newInstance()
        -&gt;setSubject('Subject 1')
        -&gt;setFrom(array('info@domain.com' =&gt; 'Info'))
        -&gt;setTo(array(
            'name1@domain.com',
            'name2@domain.com',
        ))
        -&gt;setBody($info['message_support'],'text/html');

    //Create the message for client    
    $message2 = Swift_Message::newInstance()
        -&gt;setSubject('Subject 2')
        -&gt;setFrom(array('info@domain.com' =&gt; 'Info'))
        -&gt;setTo(array(
            $info['email']          
        ))
        -&gt;setBody($info['message_client'],'text/html');

    if ($mailer-&gt;send($message) &amp;&amp; $mailer-&gt;send($message2)) 
        return true;
    else
        return false;               
}

$info = array(
    'email' =&gt; $email,
    'message_support' =&gt; '&lt;p&gt;Dear Support&lt;/p&gt;&lt;p&gt;Hello World&lt;/p&gt;',
    'message_client' =&gt; '&lt;p&gt;Dear Client&lt;/p&gt;&lt;p&gt;Hello World&lt;/p&gt;'  
);

if (send_email($info))
{
  header("Location:/thank-you");  
}
else
{
  header("Location:/error");
}

?&gt;
Note: this is intended to be a Community wiki since it was taken from a deleted answer by yannis; The suggestion to do this was here

Using two message instances is fine as is most of your code. There is one major issue though, you don't validate the email, you just check if the $_POST variable was set and is not empty but you don't check if the email format is &quot;name@domain.tld&quot;. The simplest thing you could do is:
$email = 
    isset($_POST[&quot;email&quot;]) &amp;&amp; !empty($_POST[&quot;email&quot;]) 
    ? filter_var($email, filter_var($_POST[&quot;email&quot;], FILTER_SANITIZE_EMAIL) )
    : &quot;No data&quot;;

The filter extension greatly helps you sanitize and validate variables. It's on by default since PHP 5.2, if you are working on an earlier version, you can substitute it with an email validation regular expression. By the way, if you are unfamiliar with the conditional format I'm using, it's called ternary operator and it goes something like this:
$variable = 
    ( condition )
    ? ( value if condition is true )
    : ( value if condition is false );

When actually sending the email, you should do:
if( $email != &quot;No Data&quot; &amp;&amp; send_email($info) ) {
  header(&quot;Location:/thank-you&quot;);  
} else {
  header(&quot;Location:/error&quot;);
}

That way and since the check $email != &quot;No Data&quot; is first, send_email($info) will never execute for a missing or invalid email. So the 2 instances of a hefty library will never instantiate needlessly.
Lastly, a very minor change, that might be considered a matter of style. Here:
if ($mailer-&gt;send($message) &amp;&amp; $mailer-&gt;send($message2)) 
    return true;
else
    return false;  

You are returning a boolean value based on the boolean return of $mailer-&gt;send($message) &amp;&amp; $mailer-&gt;send($message2). You can simply rewrite as:
return $mailer-&gt;send($message) &amp;&amp; $mailer-&gt;send($message2);