Swift_Mailer::send PHP Method

send() public method

All recipients (with the exception of Bcc) will be able to see the other recipients this message was sent to. Recipient/sender data will be retrieved from the Message object. The return value is the number of recipients who were accepted for delivery.
public send ( Swift_Mime_Message $message, array &$failedRecipients = null ) : integer
$message Swift_Mime_Message
$failedRecipients array An array of failures by-reference
return integer The number of successful recipients. Can be 0 which indicates failure
    public function send(Swift_Mime_Message $message, &$failedRecipients = null)
    {
        $failedRecipients = (array) $failedRecipients;
        if (!$this->_transport->isStarted()) {
            $this->_transport->start();
        }
        $sent = 0;
        try {
            $sent = $this->_transport->send($message, $failedRecipients);
        } catch (Swift_RfcComplianceException $e) {
            foreach ($message->getTo() as $address => $name) {
                $failedRecipients[] = $address;
            }
        }
        return $sent;
    }

Usage Example

Ejemplo n.º 1
0
 /**
  * Process email model sending.
  *
  * @param EmailModel $model
  * @return Email
  * @throws \Swift_SwiftException
  */
 public function process(EmailModel $model)
 {
     $this->assertModel($model);
     $messageDate = new \DateTime('now', new \DateTimeZone('UTC'));
     /** @var \Swift_Message $message */
     $message = $this->mailer->createMessage();
     $message->setDate($messageDate->getTimestamp());
     $message->setFrom($this->getAddresses($model->getFrom()));
     $message->setTo($this->getAddresses($model->getTo()));
     $message->setSubject($model->getSubject());
     $message->setBody($model->getBody(), $model->getType() === 'html' ? 'text/html' : 'text/plain');
     $messageId = $message->generateId();
     if (!$this->mailer->send($message)) {
         throw new \Swift_SwiftException('An email was not delivered.');
     }
     $origin = $this->getEmailOrigin($model->getFrom());
     $this->emailEntityBuilder->setOrigin($origin);
     $email = $this->emailEntityBuilder->email($model->getSubject(), $model->getFrom(), $model->getTo(), $messageDate, $messageDate, $messageDate);
     $email->addFolder($origin->getFolder(FolderType::SENT));
     $email->setEmailBody($this->emailEntityBuilder->body($model->getBody(), $model->getType() === 'html', true));
     $email->setMessageId($messageId);
     // persist the email and all related entities such as folders, email addresses etc.
     $this->emailEntityBuilder->getBatch()->persist($this->getEntityManager());
     // associate the email with the target entity if exist
     if ($model->hasEntity()) {
         $targetEntity = $this->doctrineHelper->getEntity($model->getEntityClass(), $model->getEntityId());
         if ($targetEntity) {
             $this->emailActivityManager->addAssociation($email, $targetEntity);
         }
     }
     // flush all changes to the database
     $this->getEntityManager()->flush();
     return $email;
 }
All Usage Examples Of Swift_Mailer::send