Code Monkey home page Code Monkey logo

php-simple-mail's Introduction

README

Build Status Latest Stable Version Scrutinizer Quality Score Code Coverage Total Downloads License PHP 7 ready

Introduction

Simple Mail Class provides a simple, chainable wrapper for creating and sending emails using the PHP mail() function. There are better options out there for sending SMTP email, which are more secure and more reliable than the mail() function. However, sometimes you just need to send a simple email. That's what we cover.

Installation via Composer

$ composer require eoghanobrien/php-simple-mail

Usage

Instantiating the class.

You have two options, you can 'new up' the class in the traditional way:

$mailer = new SimpleMail();

or instantiate it using the named static constructor make()

$mailer = SimpleMail::make();

The static constructor can be useful when you want to continue chaining methods after instantiating.

SimpleMail::make()
->setTo($email, $name)
->setFrom($fromEmail, $fromName)
->setSubject($subject)
->setMessage($message)
->send();

To header

The To header can be called multiple time, in order to pass more than one To address, simply call the setTo method as many times as needed. It takes two string parameters. The first parameter is for the email address, the second is for the name.

SimpleMail::make()
 ->setTo($email1, $name1)
 ->setTo($email2, $name2);

From header

You can carbon copy one or more addresses using the setBcc method. It takes two string parameters. The first parameter is for the email address, the second is for the name.

SimpleMail::make()
  ->setFrom('[email protected]', 'John Smith');

Cc header

You can carbon copy one or more addresses using the setCc method. It takes an array of $name => $email pairs. Alternatively, you can pass a simple numerically keyed array an the value is assumed to be the email.

SimpleMail::make()
  ->setCc(['John Smith', '[email protected]');

Bcc header

You can blind carbon copy one or more addresses using the setBcc method. It takes an array of $name => $email pairs. Alternatively, you can pass a simple numerically keyed array an the value is assumed to be the email.

SimpleMail::make()
  ->setBcc(['John Smith', '[email protected]');

Subject header

You can set the subject using setSubject method. It takes a string as the only parameter.

SimpleMail::make()
    ->setSubject("Important information about your account");

Message header

You can set the message using setMessage method. It takes a string as the only parameter.

SimpleMail::make()
    ->setMessage("My important message!");

HTML emails

If you want to include HTML in your email. Simply call the setHtml() method. It takes no parameters.

SimpleMail::make()
    ->setMessage("<strong>My important message!</strong>")
    ->setHtml();

send emails

Once you've set all your headers. Use the send() method to finally send it on it's way.

SimpleMail::make()
    ->setMessage("<strong>My important message!</strong>")
    ->send();

Full example of sending an email

$send = SimpleMail::make()
    ->setTo($email, $name)
    ->setFrom($fromEmail, $fromName)
    ->setSubject($subject)
    ->setMessage($message)
    ->setReplyTo($replyEmail, $replyName)
    ->setCc(['Bill Gates' => '[email protected]'])
    ->setBcc(['Steve Jobs' => '[email protected]'])
    ->setHtml()
    ->setWrap(100)
    ->send();
    
echo ($send) ? 'Email sent successfully' : 'Could not send email';

Example of sending an email with attachments

If you are sending an attachment there is no need to add any addGenericHeader()'s. To properly send the attachments the necessary headers will be set for you. You can also chain as many attachments as you want (see example).

$send = SimpleMail::make()
    ->setTo($email, $name)
    ->setFrom($fromEmail, $fromName)
    ->setSubject($subject)
    ->setMessage($message)
    ->setReplyTo($replyEmail, $replyName)
    ->setCc(['Bill Gates' => '[email protected]'])
    ->setBcc(['Steve Jobs' => '[email protected]'])
    ->setHtml()
    ->setWrap(100)
    ->addAttachment('example/pbXBsZSwgY2hh.jpg', 'lolcat_finally_arrived.jpg')
    ->addAttachment('example/lolcat_what.jpg')
    ->send();
    
echo ($send) ? 'Email sent successfully' : 'Could not send email';

License

php-simple-mail is free and unencumbered public domain software. For more information, see http://opensource.org/licenses/MIT or the accompanying MIT file.

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.