Search code examples
phpswiftmailernewsletter

sending newsletter with swift 4.1.1


I would create a php newsletter script with SwiftMail 4.1.1 but I have only founded the wiki for Swift v.3 at http://swiftmailer.org/wikidocs/v3/sending/batch For Swift 4.1.1 there isn't wiki http://swiftmailer.org/wikidocs/v4/start

How can I do with for Swift v.4? The same code for v.3 is not working.

Thanks a lot.


Solution

  • Ok as per your request -

    If you want to send emails in a batch

    As per latest documentation at http://swiftmailer.org/docs/sending.html

    Sending Emails in Batch¶

    If you want to send a separate message to each recipient so that only their own address shows up in the To: field, follow the following recipe:

    require_once 'lib/swift_required.php';
    
    //Create the Transport
    $transport = Swift_SmtpTransport::newInstance('localhost', 25);
    
    //Create the Mailer using your created Transport
    $mailer = Swift_Mailer::newInstance($transport);
    
    //Create a message
    $message = Swift_Message::newInstance('Wonderful Subject')
      ->setFrom(array('[email protected]' => 'John Doe'))
      ->setBody('Here is the message itself')
      ;
    
    //Send the message
    $failedRecipients = array();
    $numSent = 0;
    $to = array('[email protected]', '[email protected]' => 'A name');
    
    foreach ($to as $address => $name)
    {
      $message->setTo(array($address => $name));
      $numSent += $this->send($message, $failedRecipients);
    }
    
    printf("Sent %d messages\n", $numSent);
    

    Using batchSend() - which I am not sure if is available in in your required version or not?

    $mailer_instance = Swift_Mailer::newInstance($mail_transport_instance);
    $message_instance = Swift_Message::newInstance()
          ->setSubject($subject)
          //Set the From address with an associative array
          ->setFrom($mail_from)
          //Set the To addresses with an associative array
          ->setTo($mail_to);
          //some other options as required
    
    $num_sent = $mailer_instance->batchSend($message_instance);
    

    I hope it helps.