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

Contributors

abbychau avatar jinairsos avatar stlewis avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

mail's Issues

Not able to send attachments

As stated on Docs, I did $mail->add_attachments($_FILES['myFile']); . Buy I am getting this error.

Warning: basename() expects parameter 1 to be string, array given
Warning: file_get_contents() expects parameter 1 to be a valid path, array given

Do I have to upload file on the server before sending email?

if i Use body_text & body_html & and have no attachment and use UTF-8 i have message

--PHP-mixed-c71e9898e2ff03c758cd9ad6d8f0fa39
Content-Type: multipart/alternative; boundary="PHP-alt-c71e9898e2ff03c758cd9ad6d8f0fa39"

--PHP-alt-c71e9898e2ff03c758cd9ad6d8f0fa39
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: 8bit

Опи�ание - ИЕготовление па�порта фа�ада

--PHP-alt-c71e9898e2ff03c758cd9ad6d8f0fa39
Content-Type: text/html; charset="UTF-8 "
Content-Transfer-Encoding: 8bit

Еготовление па�порта фа�ада

--PHP-alt-c71e9898e2ff03c758cd9ad6d8f0fa39--

but if i make attachment it all will be ok!

Not able to send html data mail

Help wanted!.

I created HTML form and all data want to send to mail but not able to get it.
screenshot from 2016-12-21 12 03 13
So how do I approach it? please help?
I go through you repo but not able implement.

Replace \r\n

I have to replace the \r\n into \n?
Now it works fine for me.

private function prepare_headers(){
  $this->set_default_headers();
  $this->header_string = implode("\r\n", $this->headers)."\r\n";
}

Thanks!

Some bugs

When i use it to send email,the subject and receiver show twice,is it your program bug? how can i fix it ..thank you~
i mean that the email i received is like this.
subject:subjectsubject
receiver: [email protected];[email protected]
but i only write it once in program!

Add the License

Hey guys I am just want to know is there compulsory to add License or not because some told me do need to add license in readme.md file and do not also open issue and close issue about it.So what do you think about it.

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.