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.
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):
Than change the values for $name and $to. Save and run the script in your web browser.
This is a very important variable, in some cases you have to switch between “\r\n” and “\n”.
he mail priority, possible values are: 3 = normal, 2 = high, 4 = low (or others be careful)
The encoding style for the text mime parts, use “7bit” for the most western languages.
The encoding style for your text presentation, use it the same way like for HTML documents.
The text part of the mail message
The HTML part of the mail message
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.
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.
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.
This method is used to output all (error) messages created while using the class.
Change log for the PHP Attachment Mailer Class
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.
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.
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.
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.
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.