PHP Attachment Mailer Class

I wrote this class to integrate more functionality for my PHP classes like Access_user, DB_cart or any other script that’s using the PHP mail function. You can use this PHP class for every web application where the standard text mail message is not enough or where file and image attachments are required. The class has no upload function, use for advanced file upload functionality the Easy Upload Class. The current version covers functions like: e-mail address check (using regular expressions and a test against the domain’s MX record), all header rows are filtered to prevent spam via web mail forms, the class can handle multiple file and image attachments. The current version is able to send HTML email messages with inline image attachments. If you’re looking for a SMTP solution check this article about the PHPMailer class.

PHP Script Download

Documentation

For a first test unzip all files into a directory, than modify this row inside the example file named “attach_mailer_example.php” (the example attachment files are an image and a zip file):

require($_SERVER[‘DOCUMENT_ROOT’].”/classes/attach_mailer/attach_mailer_class.php”);

Than change the values for $name and $to. Save and run the script in your web browser.

Variables

define(“LIBR”, “\n”);

This is a very important variable, in some cases you have to switch between “\r\n” and “\n”.

define(“PRIORITY”, 3);

he mail priority, possible values are: 3 = normal, 2 = high, 4 = low (or others be careful)

define(“TRANS_ENC”, “7bit”);

The encoding style for the text mime parts, use “7bit” for the most western languages.

define(“ENCODING”, “iso-8859-1”);

The encoding style for your text presentation, use it the same way like for HTML documents.

var $text_body;

The text part of the mail message

var $html_body;

The HTML part of the mail message

Public methods

attach_mailer($name = “”, $from, $to, $cc = “”, $bcc = “”, $subject = “”)

The constructor method, the attributes “$from” and “$to”. The subject is not required by the class to send a message, but you should always define a clear subject for all e-mails. While creating an object only the two e-mail addresses are validated. If all data for the email message is OK a boolean is set. If the boolean ($valid_mail_adresses) is “true” a mail will be send.

add_attach_file($file, $encoding = “base64”, $dispo = “attachment”, $type = “application/octet-stream”)

Use this method for every (non-inline) attachment you like to send, inside a loop it’s very easy to handle a dynamic number of files.

add_html_image($img_name)

Use this method to add images which are used inside the HTML email message (for embedded images). In that it’s required to link any external image. The function will create the internal link inside the email to show the inline images inside HTML email message.

process_mail()

This method will send the email if the boolean ($valid_mail_adresses) is set to “true”. You have to know that it’s also possible to send an empty mail where only the “from” and “to” e-mail address is entered.

get_msg_str()

This method is used to output all (error) messages created while using the class.

Change log for the PHP Attachment Mailer Class

Version 1.21

We added some example file that upload multiple files and send the files as multiple attachments in the generated mail message. Check the forum for the tutorial.

Version 1.20

Since this version the class is changed into a full featured html mailer class incl. html mail + (inline) attachments, alternative text format, inline attachments mixed with external attachments and much more. Most methods are changed and the structure how an objects is defined is updated, too. You need to update formerly mail scripts, check the updated documentation.

Version 1.03

I noticed there is sometimes a problem with the mail function and the return path. Some mail servers need a valid notation if the mail can’t be delivered. I added the “-f” option to the process_mail method.

Version 1.02

The process_mail() method returns a boolean now to give more information to a possible next step inside an application. There was a small bug inside the upload_and_mail_example.php file. The delete file object must be changed to act with file upload class.

Version 1.01

The new example demonstrates how to use this class together with some PHPĀ upload functionality. This example form / script needs an object of the Easy Upload class available on this website.