Git Product home page Git Product logo

mail's Introduction

About Mail

This class is a no-frills way to send emails. I wrote it originally as part of my DirtyMVC framework, because I wanted to reduce/eliminate that application's dependency on external libraries, (such as PEAR Mail). Because it occurs to me that there might be other people out there that could use a reasonably powerful email library, who need it to be portable and not dependent upon something as heavy as PEAR, I decided to extract my Mail class from the DirtyMVC framework, and I'm making it available here.

Just a quick overview of some of the features before I move on to examples:

  • Sends mixed/multipart emails, (html and/or plaintext).
  • Sends attachments
  • Object-oriented.
  • Uses PHP's built-in mail() function.

Examples

Sending A Basic Text Email:

<?php
    $to      = "[email protected]";
    $from    = "[email protected]";
    $subject = "Testing Text Email";
    $body    = "This is a test email body that will be in text format";
    $mail    = new Mail($to, $from, $subject, $body);
    $mail->send();
?>

It's that simple. Granted, it's not a heck of a lot harder to do the same thing just using PHP's built-in mail() function, so let's take a look at a slightly more complicated example...

Sending An Email With Text and HTML:

<?php
    $to        = "[email protected]";
    $from      = "[email protected]";
    $subject   = "Testing Text Email";
    $text_body = "This is a test email body that will be in text format";
    $html_body = "<p>This is a <strong>test email body</strong> in HTML</p>";
    $mail      = new Mail($to, $from, $subject, $text_body, $html_body);
    $mail->send();
?>

Given the code above, email clients that can support HTML in their messages will get the HTML version of the email, whereas email clients that only support text will render the text version instead. Good luck making that happen with just one extra line of code using mail()!

Adding Additional Headers

<?php
    $to        = "[email protected]";
    $from      = "[email protected]";
    $subject   = "Testing Text Email";
    $text_body = "This is a test email body that will be in text format";
    $html_body = "<p>This is a <strong>test email body</strong> in HTML</p>";

    $mail      = new Mail($to, $from, $subject, $text_body, $html_body);

    $mail->add_header("Bcc: [email protected]");
    $mail->add_header("Cc: [email protected]");
    $mail->add_header("Reply-To: [email protected]");

    $mail->send();

?>

Although I debated the usefulness of adding members like 'cc' and 'bcc' to the Mail class, I felt in the end that it made far more sense to just provide an easy way to add any header at all to the email. In the example above, I show you how to add some common headers to an email, but you could add any header you need to using the same method. The only thing to bear in mind is that the string you pass must be a valid header string.

h3. Adding Attachments

<?php
    $to        = "[email protected]";
    $from      = "[email protected]";
    $subject   = "Testing Text Email";
    $text_body = "This is a test email body that will be in text format";
    $html_body = "<p>This is a <strong>test email body</strong> that will be in html format</p>";
    $mail      = new Mail($to, $from, $subject, $text_body, $html_body);

    $mail->add_attachment("/path/to/my_attachment.file");
    $mail->add_attachment("/path/to/my_other_attachment.file");

    $mail->send();
?>

To add an attachment to an email, all you have to do is call the add_attachment() method with the absolute path to the file you'd like to attach as the only argument. It's that simple.

mail's People

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.