5
\$\begingroup\$

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:

<?php

if ((isset($_POST['email'])) && (strlen(trim($_POST['email'])) > 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()
        ->setSubject('Subject 1')
        ->setFrom(array('info@domain.com' => 'Info'))
        ->setTo(array(
            'name1@domain.com',
            'name2@domain.com',
        ))
        ->setBody($info['message_support'],'text/html');

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

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

$info = array(
    'email' => $email,
    'message_support' => '<p>Dear Support</p><p>Hello World</p>',
    'message_client' => '<p>Dear Client</p><p>Hello World</p>'  
);

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

?>
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.