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 = "joe@example.com"; $from = "jim@example.com"; $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 = "joe@example.com"; $from = "jim@example.com"; $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 = "joe@example.com"; $from = "jim@example.com"; $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: someone@example.com"); $mail->add_header("Cc: someone_else@example.com"); $mail->add_header("Reply-To: email_me@example.com"); $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 = "joe@example.com"; $from = "jim@example.com"; $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.
版权声明:
1、该文章(资料)来源于互联网公开信息,我方只是对该内容做点评,所分享的下载地址为原作者公开地址。2、网站不提供资料下载,如需下载请到原作者页面进行下载。
3、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考学习用!
4、如文档内容存在违规,或者侵犯商业秘密、侵犯著作权等,请点击“违规举报”。