ensureValidity example

/** * @return void */
    public function ensureValidity()
    {
        $this->ensureBodyValid();

        if ('1' === $this->getHeaders()->getHeaderBody('X-Unsent')) {
            throw new LogicException('Cannot send messages marked as "draft".');
        }

        parent::ensureValidity();
    }

    private function ensureBodyValid(): void
    {
        if (null === $this->text && null === $this->html && !$this->attachments) {
            throw new LogicException('A message must have a text or an HTML part or attachments.');
        }
    }

    /** * Generates an AbstractPart based on the raw body of a message. * * The most "complex" part generated by this method is when there is text and HTML bodies * with related images for the HTML part and some attachments: * * multipart/mixed * | * |------------> multipart/related * | | * | |------------> multipart/alternative * | | | * | | ------------> text/plain (with content) * | | | * | | ------------> text/html (with content) * | | * | ------------> image/png (with content) * | * ------------> application/pdf (with content) */
public function testEnsureValidityAlwaysFails()
    {
        $email = (new DraftEmail())
            ->to('alice@example.com')
            ->from('webmaster@example.com')
            ->text('some text')
        ;

        $this->expectException(LogicException::class);

        $email->ensureValidity();
    }
}

    public function ensureValidity()
    {
        if (!$this->headers->has('To') && !$this->headers->has('Cc') && !$this->headers->has('Bcc')) {
            throw new LogicException('An email must have a "To", "Cc", or "Bcc" header.');
        }

        if (!$this->headers->has('From') && !$this->headers->has('Sender')) {
            throw new LogicException('An email must have a "From" or a "Sender" header.');
        }

        parent::ensureValidity();
    }

    public function generateMessageId(): string
    {
        if ($this->headers->has('Sender')) {
            $sender = $this->headers->get('Sender')->getAddress();
        } elseif ($this->headers->has('From')) {
            $sender = $this->headers->get('From')->getAddresses()[0];
        } else {
            throw new LogicException('An email must have a "From" or a "Sender" header.');
        }

        
private RawMessage $original;
    private RawMessage $raw;
    private Envelope $envelope;
    private string $messageId;
    private string $debug = '';

    /** * @internal */
    public function __construct(RawMessage $message, Envelope $envelope)
    {
        $message->ensureValidity();

        $this->original = $message;
        $this->envelope = $envelope;

        if ($message instanceof Message) {
            $message = clone $message;
            $headers = $message->getHeaders();
            if (!$headers->has('Message-ID')) {
                $headers->addIdHeader('Message-ID', $message->generateMessageId());
            }
            $this->messageId = $headers->get('Message-ID')->getId();
            
/** * @return void */
    public function ensureValidity()
    {
        $this->ensureBodyValid();

        if ('1' === $this->getHeaders()->getHeaderBody('X-Unsent')) {
            throw new LogicException('Cannot send messages marked as "draft".');
        }

        parent::ensureValidity();
    }

    private function ensureBodyValid(): void
    {
        if (null === $this->text && null === $this->html && !$this->attachments) {
            throw new LogicException('A message must have a text or an HTML part or attachments.');
        }
    }

    /** * Generates an AbstractPart based on the raw body of a message. * * The most "complex" part generated by this method is when there is text and HTML bodies * with related images for the HTML part and some attachments: * * multipart/mixed * | * |------------> multipart/related * | | * | |------------> multipart/alternative * | | | * | | ------------> text/plain (with content) * | | | * | | ------------> text/html (with content) * | | * | ------------> image/png (with content) * | * ------------> application/pdf (with content) */

    public function ensureValidity()
    {
        if (!$this->headers->has('To') && !$this->headers->has('Cc') && !$this->headers->has('Bcc')) {
            throw new LogicException('An email must have a "To", "Cc", or "Bcc" header.');
        }

        if (!$this->headers->has('From') && !$this->headers->has('Sender')) {
            throw new LogicException('An email must have a "From" or a "Sender" header.');
        }

        parent::ensureValidity();
    }

    public function generateMessageId(): string
    {
        if ($this->headers->has('Sender')) {
            $sender = $this->headers->get('Sender')->getAddress();
        } elseif ($this->headers->has('From')) {
            $sender = $this->headers->get('From')->getAddresses()[0];
        } else {
            throw new LogicException('An email must have a "From" or a "Sender" header.');
        }

        
Home | Imprint | This part of the site doesn't use cookies.