Giter Club home page Giter Club logo

mailer's Introduction

oliver-schoendorn/mailer

This is a little zendframework/zend-mail wrapper that will automagically construct proper multipart mime mails.

Please feel free to submit pull requests.

Installation

composer require oliver-schoendorn/mailer

Usage example

Create new Mail object and initialize mail meta data

<?php

use OS\Mail\Mail;

$mail = (new Mail())
    ->setSubject('email subject')
    ->setSender('[email protected]', 'Foo Bar')
    ->setFrom('[email protected]', 'Foo Bar')
    ->setTo('[email protected]', 'Receipt name');

Add inline attachments

This is optional, but if you are using inline attachments, you have to keep track of the MailAttachment object, in order to reference it in the html mail body.

If an inline attachment is not being referenced, some mail clients will show the attachment or a preview below the mail, which might break your carefully crafted mail layout.

<?php

use OS\Mail\MailAttachment;

$inlineImage = (new MailAttachment('file.jpg'))
   ->setContent(fopen('path/to/file.jpg', 'r'))
   ->setMimeType('image/jpg');

$mail->addInlineAttachment($inlineImage);

Add body parts

<?php

use Zend\Mime\Mime;
use OS\Mail\MailBody;

$mail->addBodyPart((new MailBody())
    ->setMimeType(Mime::TYPE_TEXT)
    ->setContent('Plain text mail content'));

$mail->addBodyPart((new MailBody())
    ->setMimeType(Mime::TYPE_HTML)
    ->setContent('<html><body><p>Html mail content<img src="' . $inlineImage . '" /></p></body></html>'));

Add (non-inline) attachments

Usually, these only appear attachment in a context menu or similar, based on the mail client used.

<?php
use OS\Mail\MailAttachment;

$mail->addAttachment((new MailAttachment('file.jpg'))
    ->setContent(fopen('path/to/file.jpg', 'r'))
    ->setMimeType('image/jpg'));

Sending the mail

To send an mail, you can either define the transport object directly using the Zend classes or you can use the built in MailTransportFactory, which provides some type hinting.

<?php
use OS\Mail\MailTransportFactory;

$mailTransport = (new MailTransportFactory())->createSmtpTransportWithLogin(
    $config['mail']['user'],
    $config['mail']['pass'],
    $config['mail']['encryption'],
    $config['mail']['name'],
    $config['mail']['host'],
    $config['mail']['port']
);

$mail->send($mailTransport);

License

Copyright 2018 Oliver Schöndorn

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

mailer's People

Contributors

oliver-schoendorn avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.