Sending Email with PHP

This is something I have needed to do a number of times. I have also seen numerous requests on the web and in forums for instructions on how to do this. So here are a couple of simple PHP classes I have written to accomplish this.

You can download the complete source code from here: SendEmail

 

First, here is the class SendEmail. This is the class that does all the work. It uses PEAR "Mail" and "Mail_mime".



  1. <?php
  2.  
  3. require_once 'Mail.php';
  4. require_once 'Mail/mime.php';
  5. require_once 'MimeTypes.php';
  6.  
  7. /**
  8.  * A simple class that contains functions for sending email either with or
  9.  * without attachments.
  10.  *
  11.  * @version Version 1.0
  12.  * @author Brian Ngure <brian@pixie.co.ke>
  13.  * @copyright Copyright (c) 2010, Brian Ngure
  14.  * @license http://www.gnu.org/licenses/gpl.html GNU Public License
  15.  */
  16. class SendEmail
  17. {
  18.     /**
  19.      * Constructor.
  20.      */
  21.     function __construct() {
  22.     }
  23.  
  24.     /**
  25.      * Sends an email without attachments.
  26.      *
  27.      * @param string $from the senders email address
  28.      * @param array $to string array of recipients email addresses
  29.      * @param array $cc string array of Cc recipients email addresses
  30.      * @param array $bcc string array of Bcc recipients email addresses
  31.      * @param string $subject the message subject
  32.      * @param string $message the message content
  33.      * @param string $host the smtp host e.g. smtp.gmail.com
  34.      * @param string $emailUserName the email account user name e.g. support or
  35.      *                              support@example.com
  36.      * @param string $emailPassword the email account password
  37.      * @param string $replyTo the email address that will specified as the
  38.      *                        "reply to" address when the recipient hits the
  39.      *                        reply button
  40.      * @param bool $plainText TRUE to send the email in plain text, FALSE to
  41.      *                        send it using HTML format
  42.      *
  43.      * @return bool TRUE on successful sending, throws an Exception otherwise
  44.      */
  45.     public static function sendMailNoAttachments($from, $to, $cc, $bcc,
  46.             $subject, $message, $host, $emailUserName, $emailPassword, $replyTo,
  47.             $plainText)
  48.     {
  49.         $toAddresses = "";
  50.         $ccAddresses = "";
  51.         $bccAddresses = "";
  52.         $recipients = NULL;
  53.  
  54.         if (!empty($to)) {
  55.             // Create a comma separated string of "to" addresses
  56.             foreach ($to as $address) {
  57.                 $toAddresses .= $address. ", ";
  58.             }
  59.             $toAddresses = substr($toAddresses, 0, strlen($toAddresses) - 2);
  60.         }
  61.  
  62.         if (!empty($cc)) {
  63.             // Create a comma separated string of "cc" addresses
  64.             foreach ($cc as $address) {
  65.                 $ccAddresses .= $address. ", ";
  66.             }
  67.             $ccAddresses = substr($ccAddresses, 0, strlen($ccAddresses) - 2);
  68.         }
  69.  
  70.         if (!empty($bcc)) {
  71.             // Create a comma separated string of "bcc" addresses
  72.             foreach ($bcc as $address) {
  73.                 $bccAddresses .= $address. ", ";
  74.             }
  75.             $bccAddresses = substr($bccAddresses, 0, strlen($bccAddresses) - 2);
  76.         }
  77.  
  78.         if (!empty($to) && !empty($cc) && !empty($bcc)) {
  79.             $recipients = array(
  80.                 'To' => $toAddresses,
  81.                 'Cc' => $ccAddresses,
  82.                 'Bcc' => $bccAddresses);
  83.         } else if (!empty($to) && !empty($cc) && empty($bcc)) {
  84.             $recipients = array(
  85.                 'To' => $toAddresses,
  86.                 'Cc' => $ccAddresses);
  87.         } else if (!empty($to) && empty($cc) && !empty($bcc)) {
  88.             $recipients = array(
  89.                 'To' => $toAddresses,
  90.                 'Bcc' => $bccAddresses);
  91.         } else if (!empty($to) && empty($cc) && empty($bcc)) {
  92.             $recipients = array('To' => $toAddresses);
  93.         } else if (empty($to) && !empty($cc) && empty($bcc)) {
  94.             $recipients = array('Cc' => $ccAddresses);
  95.         } else if (empty($to) && empty($cc) && !empty($bcc)) {
  96.             $recipients = array('Bcc' => $bccAddresses);
  97.         } else if (empty($to) && empty($cc) && empty($bcc)) {
  98.             throw new Exception("[ERROR: ". date("Y-m-d H:i:s"). "] You must "
  99.                     . "specify at least one \"To:\" email address.");
  100.         } else {
  101.             throw new Exception("[ERROR: ". date("Y-m-d H:i:s"). "] UNDEFINED "
  102.                     . "ERROR: Please check your system.");
  103.         }
  104.  
  105.         // For HTML formatted or plain text email
  106.         $content_type;
  107.         $mime_ver;
  108.         if ($plainText) {
  109.             $content_type = "text/plain; charset=iso-8859-1";
  110.             $mime_ver = "1.0";
  111.         } else {
  112.             $content_type = "text/html; charset=iso-8859-1";
  113.             $mime_ver = "1.0";
  114.         }
  115.  
  116.         if (!empty($cc)) {
  117.             $headers = array ('From' => $from, 'To' => $toAddresses,
  118.                 'Cc' => $ccAddresses, 'Reply-To' => $replyTo,
  119.                 'Subject' => $subject, 'Content-type' => $content_type,
  120.                 'MIME-Version' => $mime_ver);
  121.         } else {
  122.             $headers = array ('From' => $from, 'To' => $toAddresses,
  123.                 'Reply-To' => $replyTo, 'Subject' => $subject,
  124.                 'Content-type' => $content_type, 'MIME-Version' => $mime_ver);
  125.         }
  126.  
  127.         $smtp = Mail::factory('smtp',
  128.                 array ('host' => $host,
  129.                     'auth' => true,
  130.                     'username' => $emailUserName,
  131.                     'password' => $emailPassword));
  132.  
  133.         $mail = $smtp->send($recipients, $headers, $message);
  134.  
  135.         if (PEAR::isError($mail)) {
  136.             throw new Exception("[ERROR: ". date("Y-m-d H:i:s"). "] Could not "
  137.                     . "send email to: ". $toAddresses. " Got error: "
  138.                     . $mail->getMessage());
  139.         }
  140.  
  141.         return TRUE;
  142.     }
  143.  
  144.     /**
  145.      * Sends an email with attachments.
  146.      *
  147.      * @param string $from the senders email address
  148.      * @param array $to string array of recipients email addresses
  149.      * @param array $cc string array of Cc recipients email addresses
  150.      * @param array $bcc string array of Bcc recipients email addresses
  151.      * @param string $subject the message subject
  152.      * @param string $message the message content
  153.      * @param string $host the smtp host e.g. smtp.gmail.com
  154.      * @param string $emailUserName the email account user name e.g. support or
  155.      *                              support@example.com
  156.      * @param string $emailPassword the email account password
  157.      * @param string $replyTo the email address that will specified as the
  158.      *                        "reply to" address when the recipient hits the
  159.      *                        reply button
  160.      * @param bool $plainText TRUE to send the email in plain text, FALSE to
  161.      *                        send it using HTML format
  162.      * @param array $attachments array of strings which are the complete paths
  163.      *                           to the files to attach
  164.      *
  165.      * @return bool TRUE on successful sending, throws an Exception otherwise
  166.      */
  167.     public static function sendMailWithAttachments($from, $to, $cc, $bcc,
  168.             $subject, $message, $host, $emailUserName, $emailPassword, $replyTo,
  169.             $plainText, $attachments)
  170.     {
  171.         $crlf = "\n";
  172.         $toAddresses = "";
  173.         $ccAddresses = "";
  174.         $bccAddresses = "";
  175.         $recipients = NULL;
  176.  
  177.         if (!empty($to)) {
  178.             // Create a comma separated string of "to" addresses
  179.             foreach ($to as $address) {
  180.                 $toAddresses .= $address. ", ";
  181.             }
  182.             $toAddresses = substr($toAddresses, 0, strlen($toAddresses) - 2);
  183.         }
  184.  
  185.         if (!empty($cc)) {
  186.             // Create a comma separated string of "cc" addresses
  187.             foreach ($cc as $address) {
  188.                 $ccAddresses .= $address. ", ";
  189.             }
  190.             $ccAddresses = substr($ccAddresses, 0, strlen($ccAddresses) - 2);
  191.         }
  192.  
  193.         if (!empty($bcc)) {
  194.             // Create a comma separated string of "bcc" addresses
  195.             foreach ($bcc as $address) {
  196.                 $bccAddresses .= $address. ", ";
  197.             }
  198.             $bccAddresses = substr($bccAddresses, 0, strlen($bccAddresses) - 2);
  199.         }
  200.  
  201.         if (!empty($to) && !empty($cc) && !empty($bcc)) {
  202.             $recipients = array(
  203.                 'To' => $toAddresses,
  204.                 'Cc' => $ccAddresses,
  205.                 'Bcc' => $bccAddresses);
  206.         } else if (!empty($to) && !empty($cc) && empty($bcc)) {
  207.             $recipients = array(
  208.                 'To' => $toAddresses,
  209.                 'Cc' => $ccAddresses);
  210.         } else if (!empty($to) && empty($cc) && !empty($bcc)) {
  211.             $recipients = array(
  212.                 'To' => $toAddresses,
  213.                 'Bcc' => $bccAddresses);
  214.         } else if (!empty($to) && empty($cc) && empty($bcc)) {
  215.             $recipients = array('To' => $toAddresses);
  216.         } else if (empty($to) && !empty($cc) && empty($bcc)) {
  217.             $recipients = array('Cc' => $ccAddresses);
  218.         } else if (empty($to) && empty($cc) && !empty($bcc)) {
  219.             $recipients = array('Bcc' => $bccAddresses);
  220.         } else if (empty($to) && empty($cc) && empty($bcc)) {
  221.             throw new Exception("[ERROR: ". date("Y-m-d H:i:s"). "] You must "
  222.                     . "specify at least one \"To:\" email address.");
  223.         } else {
  224.             throw new Exception("[ERROR: ". date("Y-m-d H:i:s"). "] UNDEFINED "
  225.                     . "ERROR: Please check your system.");
  226.         }
  227.  
  228.         // Creating the Mime message
  229.         $mime = new Mail_mime($crlf);
  230.  
  231.         // For HTML formatted or plain text email
  232.         if ($plainText) {
  233.             $mime->setTXTBody($message);
  234.         } else {
  235.             $mime->setHTMLBody($message);
  236.         }
  237.  
  238.         if (!empty($cc)) {
  239.             $headers = array ('From' => $from, 'To' => $toAddresses,
  240.                 'Cc' => $ccAddresses, 'Reply-To' => $replyTo,
  241.                 'Subject' => $subject);
  242.         } else {
  243.             $headers = array ('From' => $from, 'To' => $toAddresses,
  244.                 'Reply-To' => $replyTo, 'Subject' => $subject);
  245.         }
  246.  
  247.         // Add any attachments
  248.         foreach ($attachments as $attachment) {
  249.             // Get the file extension so we can determine the content type
  250.             $fileExtension = substr($attachment, stripos($attachment, ".") + 1);
  251.             $content_type = "text/plain"; // The default content type
  252.             if (array_key_exists($fileExtension, MimeTypes::$mimeTypes)) {
  253.                 $content_type = MimeTypes::$mimeTypes["$fileExtension"];
  254.             }
  255.  
  256.             // Add the attachment to the email
  257.             $mime->addAttachment($attachment, $content_type, $attachment, TRUE);
  258.         }
  259.  
  260.         $body = $mime->get();
  261.         $headers = $mime->headers($headers);
  262.  
  263.         $smtp = Mail::factory('smtp',
  264.                 array ('host' => $host,
  265.                     'auth' => true,
  266.                     'username' => $emailUserName,
  267.                     'password' => $emailPassword));
  268.  
  269.         $mail = $smtp->send($recipients, $headers, $body);
  270.  
  271.         if (PEAR::isError($mail)) {
  272.             throw new Exception("[ERROR: ". date("Y-m-d H:i:s"). "] Could not "
  273.                     . "send email to: ". $toAddresses. " Got error: "
  274.                     . $mail->getMessage());
  275.         }
  276.  
  277.         return TRUE;
  278.     }
  279. }
  280. ?>

Here is the class MimeTypes. This class is basically just a list of known mime types that is used when sending mail with attachments.


  1. <?php
  2.  
  3. /**
  4.  * This class contains a listing of mime type for email attachments.
  5.  *
  6.  * @version Version 1.0
  7.  * @author Brian Ngure <brian@pixie.co.ke>
  8.  * @copyright Copyright (c) 2010, Brian Ngure
  9.  * @license http://www.gnu.org/licenses/gpl.html GNU Public License
  10.  */
  11. class MimeTypes
  12. {
  13.     /**
  14.      * List of mail mime types for email attachments.
  15.      *
  16.      * @var array
  17.      */
  18.     public static $mimeTypes = array(
  19.         "3dm" => "x-world/x-3dmf",
  20.         "3dmf" => "x-world/x-3dmf",
  21.         "a" => "application/octet-stream",
  22.         "aab" => "application/x-authorware-bin",
  23.         "aam" => "application/x-authorware-map",
  24.         "aas" => "application/x-authorware-seg",
  25.         "abc" => "text/vnd.abc",
  26.         "acgi" => "text/html",
  27.         "afl" => "video/animaflex",
  28.         "ai" => "application/postscript",
  29.         "aif" => "audio/x-aiff",
  30.         "aifc" => "audio/x-aiff",
  31.         "aiff" => "audio/x-aiff",
  32.         "aim" => "application/x-aim",
  33.         "aip" => "text/x-audiosoft-intra",
  34.         "ani" => "application/x-navi-animation",
  35.         "aos" => "application/x-nokia-9000-communicator-add-on-software",
  36.         "aps" => "application/mime",
  37.         "arc" => "application/octet-stream",
  38.         "arj" => "application/arj",
  39.         "art" => "image/x-jg",
  40.         "asf" => "video/x-ms-asf",
  41.         "asm" => "text/x-asm",
  42.         "asp" => "text/asp",
  43.         "asx" => "video/x-ms-asf",
  44.         "au" => "audio/x-au",
  45.         "avi" => "video/avi",
  46.         "avs" => "video/avs-video",
  47.         "bcpio" => "application/x-bcpio",
  48.         "bin" => "application/x-binary",
  49.         "bm" => "image/bmp",
  50.         "bmp" => "image/bmp",
  51.         "boo" => "application/book",
  52.         "book" => "application/book",
  53.         "boz" => "application/x-bzip2",
  54.         "bsh" => "application/x-bsh",
  55.         "bz" => "application/x-bzip",
  56.         "bz2" => "application/x-bzip2",
  57.         "c" => "text/x-c",
  58.         "c++" => "text/plain",
  59.         "cat" => "application/vnd.ms-pki.seccat",
  60.         "cc" => "text/x-c",
  61.         "ccad" => "application/clariscad",
  62.         "cco" => "application/x-cocoa",
  63.         "cdf" => "application/cdf",
  64.         "cer" => "application/x-x509-ca-cert",
  65.         "cha" => "application/x-chat",
  66.         "chat" => "application/x-chat",
  67.         "class" => "application/x-java-class",
  68.         "com" => "application/octet-stream",
  69.         "conf" => "text/plain",
  70.         "cpio" => "application/x-cpio",
  71.         "cpp" => "text/x-c",
  72.         "cpt" => "application/x-cpt",
  73.         "crl" => "application/pkcs-crl",
  74.         "crt" => "application/x-x509-ca-cert",
  75.         "csh" => "application/x-csh",
  76.         "css" => "text/css",
  77.         "cxx" => "text/plain",
  78.         "dcr" => "application/x-director",
  79.         "deepv" => "application/x-deepv",
  80.         "def" => "text/plain",
  81.         "der" => "application/x-x509-ca-cert",
  82.         "dif" => "video/x-dv",
  83.         "dir" => "application/x-director",
  84.         "dl" => "video/dl",
  85.         "doc" => "application/msword",
  86.         "dot" => "application/msword",
  87.         "dp" => "application/commonground",
  88.         "drw" => "application/drafting",
  89.         "dump" => "application/octet-stream",
  90.         "dv" => "video/x-dv",
  91.         "dvi" => "application/x-dvi",
  92.         "dwf" => "model/vnd.dwf",
  93.         "dwg" => "application/acad",
  94.         "dxf" => "application/dxf",
  95.         "dxr" => "application/x-director",
  96.         "el" => "text/x-script.elisp",
  97.         "elc" => "application/x-bytecode.elisp",
  98.         "env" => "application/x-envoy",
  99.         "eps" => "application/postscript",
  100.         "es" => "application/x-esrehber",
  101.         "etx" => "text/x-setext",
  102.         "evy" => "application/x-envoy",
  103.         "exe" => "application/octet-stream",
  104.         "f" => "text/x-fortran",
  105.         "f77" => "text/x-fortran",
  106.         "f90" => "text/x-fortran",
  107.         "fdf" => "application/vnd.fdf",
  108.         "fif" => "application/fractals",
  109.         "fli" => "video/x-fli",
  110.         "flo" => "image/florian",
  111.         "flx" => "text/vnd.fmi.flexstor",
  112.         "fmf" => "video/x-atomic3d-feature",
  113.         "for" => "text/x-fortran",
  114.         "fpx" => "image/vnd.fpx",
  115.         "frl" => "application/freeloader",
  116.         "funk" => "audio/make",
  117.         "g" => "text/plain",
  118.         "g3" => "image/g3fax",
  119.         "gif" => "image/gif",
  120.         "gl" => "video/gl",
  121.         "gsd" => "audio/x-gsm",
  122.         "gsm" => "audio/x-gsm",
  123.         "gsp" => "application/x-gsp",
  124.         "gss" => "application/x-gss",
  125.         "gtar" => "application/x-gtar",
  126.         "gz" => "application/x-gzip",
  127.         "gzip" => "application/x-gzip",
  128.         "h" => "text/x-h",
  129.         "hdf" => "application/x-hdf",
  130.         "help" => "application/x-helpfile",
  131.         "hgl" => "application/vnd.hp-hpgl",
  132.         "hh" => "text/x-h",
  133.         "hlb" => "text/x-script",
  134.         "hlp" => "application/x-winhelp",
  135.         "hpg" => "application/vnd.hp-hpgl",
  136.         "hpgl" => "application/vnd.hp-hpgl",
  137.         "hqx" => "application/binhex",
  138.         "hta" => "application/hta",
  139.         "htc" => "text/x-component",
  140.         "htm" => "text/html",
  141.         "html" => "text/html",
  142.         "htmls" => "text/html",
  143.         "htt" => "text/webviewhtml",
  144.         "htx" => "text/html",
  145.         "ice" => "x-conference/x-cooltalk",
  146.         "ico" => "image/x-icon",
  147.         "idc" => "text/plain",
  148.         "ief" => "image/ief",
  149.         "iefs" => "image/ief",
  150.         "iges" => "application/iges",
  151.         "iges" => "model/iges",
  152.         "igs" => "application/iges",
  153.         "ima" => "application/x-ima",
  154.         "imap" => "application/x-httpd-imap",
  155.         "inf" => "application/inf",
  156.         "ins" => "application/x-internett-signup",
  157.         "ip" => "application/x-ip2",
  158.         "isu" => "video/x-isvideo",
  159.         "it" => "audio/it",
  160.         "iv" => "application/x-inventor",
  161.         "ivr" => "i-world/i-vrml",
  162.         "ivy" => "application/x-livescreen",
  163.         "jam" => "audio/x-jam",
  164.         "jav" => "text/x-java-source",
  165.         "java" => "text/x-java-source",
  166.         "jcm" => "application/x-java-commerce",
  167.         "jfif" => "image/jpeg",
  168.         "jfif-tbnl" => "image/jpeg",
  169.         "jpe" => "image/jpeg",
  170.         "jpeg" => "image/jpeg",
  171.         "jpg" => "image/jpeg",
  172.         "jps" => "image/x-jps",
  173.         "js" => "application/x-javascript",
  174.         "jut" => "image/jutvision",
  175.         "kar" => "music/x-karaoke",
  176.         "ksh" => "application/x-ksh",
  177.         "la" => "audio/x-nspaudio",
  178.         "lam" => "audio/x-liveaudio",
  179.         "latex" => "application/x-latex",
  180.         "lha" => "application/x-lha",
  181.         "lhx" => "application/octet-stream",
  182.         "list" => "text/plain",
  183.         "lma" => "audio/x-nspaudio",
  184.         "log" => "text/plain",
  185.         "lsp" => "application/x-lisp",
  186.         "lst" => "text/plain",
  187.         "lsx" => "text/x-la-asf",
  188.         "ltx" => "application/x-latex",
  189.         "lzh" => "application/x-lzh",
  190.         "lzx" => "application/octet-stream",
  191.         "m" => "text/x-m",
  192.         "m1v" => "video/mpeg",
  193.         "m2a" => "audio/mpeg",
  194.         "m2v" => "video/mpeg",
  195.         "m3u" => "audio/x-mpequrl",
  196.         "man" => "application/x-troff-man",
  197.         "map" => "application/x-navimap",
  198.         "mar" => "text/plain",
  199.         "mbd" => "application/mbedlet",
  200.         "mc$" => "application/x-magic-cap-package-1.0",
  201.         "mcd" => "application/mcad",
  202.         "mcf" => "image/vasa",
  203.         "mcp" => "application/netmc",
  204.         "me" => "application/x-troff-me",
  205.         "mht" => "message/rfc822",
  206.         "mhtml" => "message/rfc822",
  207.         "mid" => "audio/midi",
  208.         "midi" => "audio/midi",
  209.         "mif" => "application/x-mif",
  210.         "mime" => "message/rfc822",
  211.         "mjf" => "audio/x-vnd.audioexplosion.mjuicemediafile",
  212.         "mjpg" => "video/x-motion-jpeg",
  213.         "mm" => "application/x-meme",
  214.         "mme" => "application/base64",
  215.         "mod" => "audio/mod",
  216.         "moov" => "video/quicktime",
  217.         "mov" => "video/quicktime",
  218.         "movie" => "video/x-sgi-movie",
  219.         "mp2" => "audio/mpeg",
  220.         "mp3" => "audio/mpeg3",
  221.         "mpa" => "audio/mpeg",
  222.         "mpc" => "application/x-project",
  223.         "mpe" => "video/mpeg",
  224.         "mpeg" => "video/mpeg",
  225.         "mpg" => "video/mpeg",
  226.         "mpga" => "audio/mpeg",
  227.         "mpp" => "application/vnd.ms-project",
  228.         "mpt" => "application/x-project",
  229.         "mpv" => "application/x-project",
  230.         "mpx" => "application/x-project",
  231.         "mrc" => "application/marc",
  232.         "ms" => "application/x-troff-ms",
  233.         "mv" => "video/x-sgi-movie",
  234.         "my" => "audio/make",
  235.         "mzz" => "application/x-vnd.audioexplosion.mzz",
  236.         "nap" => "image/naplps",
  237.         "naplps" => "image/naplps",
  238.         "nc" => "application/x-netcdf",
  239.         "ncm" => "application/vnd.nokia.configuration-message",
  240.         "nif" => "image/x-niff",
  241.         "niff" => "image/x-niff",
  242.         "nix" => "application/x-mix-transfer",
  243.         "nsc" => "application/x-conference",
  244.         "nvd" => "application/x-navidoc",
  245.         "o" => "application/octet-stream",
  246.         "oda" => "application/oda",
  247.         "omc" => "application/x-omc",
  248.         "omcd" => "application/x-omcdatamaker",
  249.         "omcr" => "application/x-omcregerator",
  250.         "p" => "text/x-pascal",
  251.         "p10" => "application/pkcs10",
  252.         "p12" => "application/pkcs-12",
  253.         "p7a" => "application/x-pkcs7-signature",
  254.         "p7c" => "application/pkcs7-mime",
  255.         "p7m" => "application/pkcs7-mime",
  256.         "p7r" => "application/x-pkcs7-certreqresp",
  257.         "p7s" => "application/pkcs7-signature",
  258.         "part" => "application/pro_eng",
  259.         "pas" => "text/pascal",
  260.         "pbm" => "image/x-portable-bitmap",
  261.         "pcl" => "application/vnd.hp-pcl",
  262.         "pct" => "image/x-pict",
  263.         "pcx" => "image/x-pcx",
  264.         "pdb" => "chemical/x-pdb",
  265.         "pdf" => "application/pdf",
  266.         "pfunk" => "audio/make",
  267.         "pgm" => "image/x-portable-graymap",
  268.         "pic" => "image/pict",
  269.         "pict" => "image/pict",
  270.         "pkg" => "application/x-newton-compatible-pkg",
  271.         "pko" => "application/vnd.ms-pki.pko",
  272.         "pl" => "text/x-script.perl",
  273.         "plx" => "application/x-pixclscript",
  274.         "pm" => "text/x-script.perl-module",
  275.         "pm4" => "application/x-pagemaker",
  276.         "pm5" => "application/x-pagemaker",
  277.         "png" => "image/png",
  278.         "pnm" => "application/x-portable-anymap",
  279.         "pot" => "application/mspowerpoint",
  280.         "pov" => "model/x-pov",
  281.         "ppa" => "application/vnd.ms-powerpoint",
  282.         "ppm" => "image/x-portable-pixmap",
  283.         "pps" => "application/mspowerpoint",
  284.         "ppt" => "application/powerpoint",
  285.         "ppz" => "application/mspowerpoint",
  286.         "pre" => "application/x-freelance",
  287.         "prt" => "application/pro_eng",
  288.         "ps" => "application/postscript",
  289.         "psd" => "application/octet-stream",
  290.         "pvu" => "paleovu/x-pv",
  291.         "pwz" => "application/vnd.ms-powerpoint",
  292.         "py" => "text/x-script.python",
  293.         "pyc" => "applicaiton/x-bytecode.python",
  294.         "qcp" => "audio/vnd.qcelp",
  295.         "qd3" => "x-world/x-3dmf",
  296.         "qd3d" => "x-world/x-3dmf",
  297.         "qif" => "image/x-quicktime",
  298.         "qt" => "video/quicktime",
  299.         "qtc" => "video/x-qtc",
  300.         "qti" => "image/x-quicktime",
  301.         "qtif" => "image/x-quicktime",
  302.         "ra" => "audio/x-realaudio",
  303.         "ram" => "audio/x-pn-realaudio",
  304.         "ras" => "image/cmu-raster",
  305.         "rast" => "image/cmu-raster",
  306.         "rexx" => "text/x-script.rexx",
  307.         "rf" => "image/vnd.rn-realflash",
  308.         "rgb" => "image/x-rgb",
  309.         "rm" => "audio/x-pn-realaudio",
  310.         "rmi" => "audio/mid",
  311.         "rmm" => "audio/x-pn-realaudio",
  312.         "rmp" => "audio/x-pn-realaudio",
  313.         "rng" => "application/ringing-tones",
  314.         "rnx" => "application/vnd.rn-realplayer",
  315.         "roff" => "application/x-troff",
  316.         "rp" => "image/vnd.rn-realpix",
  317.         "rpm" => "audio/x-pn-realaudio-plugin",
  318.         "rt" => "text/richtext",
  319.         "rtf" => "application/rtf",
  320.         "rtx" => "application/rtf",
  321.         "rv" => "video/vnd.rn-realvideo",
  322.         "s" => "text/x-asm",
  323.         "s3m" => "audio/s3m",
  324.         "saveme" => "application/octet-stream",
  325.         "sbk" => "application/x-tbook",
  326.         "scm" => "application/x-lotusscreencam",
  327.         "sdml" => "text/plain",
  328.         "sdp" => "application/sdp",
  329.         "sdr" => "application/sounder",
  330.         "sea" => "application/sea",
  331.         "set" => "application/set",
  332.         "sgm" => "text/sgml",
  333.         "sgml" => "text/sgml",
  334.         "sh" => "application/x-sh",
  335.         "shar" => "application/x-bsh",
  336.         "shtml" => "text/x-server-parsed-html",
  337.         "sid" => "audio/x-psid",
  338.         "sit" => "application/x-sit",
  339.         "skd" => "application/x-koan",
  340.         "skm" => "application/x-koan",
  341.         "skp" => "application/x-koan",
  342.         "skt" => "application/x-koan",
  343.         "sl" => "application/x-seelogo",
  344.         "smi" => "application/smil",
  345.         "smil" => "application/smil",
  346.         "snd" => "audio/basic",
  347.         "sol" => "application/solids",
  348.         "spc" => "application/x-pkcs7-certificates",
  349.         "spl" => "application/futuresplash",
  350.         "spr" => "application/x-sprite",
  351.         "sprite" => "application/x-sprite",
  352.         "src" => "application/x-wais-source",
  353.         "ssi" => "text/x-server-parsed-html",
  354.         "ssm" => "application/streamingmedia",
  355.         "sst" => "application/vnd.ms-pki.certstore",
  356.         "step" => "application/step",
  357.         "stl" => "application/sla",
  358.         "stp" => "application/step",
  359.         "sv4cpio" => "application/x-sv4cpio",
  360.         "sv4crc" => "application/x-sv4crc",
  361.         "svf" => "image/x-dwg",
  362.         "svr" => "application/x-world",
  363.         "swf" => "application/x-shockwave-flash",
  364.         "t" => "application/x-troff",
  365.         "talk" => "text/x-speech",
  366.         "tar" => "application/x-tar",
  367.         "tbk" => "application/x-tbook",
  368.         "tcl" => "application/x-tcl",
  369.         "tcsh" => "text/x-script.tcsh",
  370.         "tex" => "application/x-tex",
  371.         "texi" => "application/x-texinfo",
  372.         "texinfo" => "application/x-texinfo",
  373.         "text" => "text/plain",
  374.         "tgz" => "application/x-compressed",
  375.         "tif" => "image/tiff",
  376.         "tiff" => "image/tiff",
  377.         "tr" => "application/x-troff",
  378.         "tsi" => "audio/tsp-audio",
  379.         "tsp" => "application/dsptype",
  380.         "tsv" => "text/tab-separated-values",
  381.         "turbot" => "image/florian",
  382.         "txt" => "text/plain",
  383.         "uil" => "text/x-uil",
  384.         "uni" => "text/uri-list",
  385.         "unis" => "text/uri-list",
  386.         "unv" => "application/i-deas",
  387.         "uri" => "text/uri-list",
  388.         "uris" => "text/uri-list",
  389.         "ustar" => "application/x-ustar",
  390.         "uu" => "text/x-uuencode",
  391.         "uue" => "text/x-uuencode",
  392.         "vcd" => "application/x-cdlink",
  393.         "vcs" => "text/x-vcalendar",
  394.         "vda" => "application/vda",
  395.         "vdo" => "video/vdo",
  396.         "vew" => "application/groupwise",
  397.         "viv" => "video/vivo",
  398.         "vivo" => "video/vivo",
  399.         "vmd" => "application/vocaltec-media-desc",
  400.         "vmf" => "application/vocaltec-media-file",
  401.         "voc" => "audio/voc",
  402.         "vos" => "video/vosaic",
  403.         "vox" => "audio/voxware",
  404.         "vqe" => "audio/x-twinvq-plugin",
  405.         "vqf" => "audio/x-twinvq",
  406.         "vql" => "audio/x-twinvq-plugin",
  407.         "vrml" => "application/x-vrml",
  408.         "vrt" => "x-world/x-vrt",
  409.         "vsd" => "application/x-visio",
  410.         "vst" => "application/x-visio",
  411.         "vsw" => "application/x-visio",
  412.         "w60" => "application/wordperfect6.0",
  413.         "w61" => "application/wordperfect6.1",
  414.         "w6w" => "application/msword",
  415.         "wav" => "audio/wav",
  416.         "wb1" => "application/x-qpro",
  417.         "wbmp" => "image/vnd.wap.wbmp",
  418.         "web" => "application/vnd.xara",
  419.         "wiz" => "application/msword",
  420.         "wk1" => "application/x-123",
  421.         "wmf" => "windows/metafile",
  422.         "wml" => "text/vnd.wap.wml",
  423.         "wmlc" => "application/vnd.wap.wmlc",
  424.         "wmls" => "text/vnd.wap.wmlscript",
  425.         "wmlsc" => "application/vnd.wap.wmlscriptc",
  426.         "word" => "application/msword",
  427.         "wp" => "application/wordperfect",
  428.         "wp5" => "application/wordperfect",
  429.         "wp6" => "application/wordperfect",
  430.         "wpd" => "application/wordperfect",
  431.         "wq1" => "application/x-lotus",
  432.         "wri" => "application/x-wri",
  433.         "wrl" => "application/x-world",
  434.         "wrz" => "model/vrml",
  435.         "wsc" => "text/scriplet",
  436.         "wsrc" => "application/x-wais-source",
  437.         "wtk" => "application/x-wintalk",
  438.         "xbm" => "image/x-xbitmap",
  439.         "xdr" => "video/x-amt-demorun",
  440.         "xgz" => "xgl/drawing",
  441.         "xif" => "image/vnd.xiff",
  442.         "xl" => "application/excel",
  443.         "xla" => "application/excel",
  444.         "xlb" => "application/excel",
  445.         "xlc" => "application/excel",
  446.         "xld" => "application/excel",
  447.         "xlk" => "application/excel",
  448.         "xll" => "application/excel",
  449.         "xlm" => "application/excel",
  450.         "xls" => "application/excel",
  451.         "xlt" => "application/excel",
  452.         "xlv" => "application/excel",
  453.         "xlw" => "application/excel",
  454.         "xm" => "audio/xm",
  455.         "xml" => "text/xml",
  456.         "xmz" => "xgl/movie",
  457.         "xpix" => "application/x-vnd.ls-xpix",
  458.         "xpm" => "image/xpm",
  459.         "x-png" => "image/png",
  460.         "xsr" => "video/x-amt-showrun",
  461.         "xwd" => "image/x-xwd",
  462.         "xyz" => "chemical/x-pdb",
  463.         "z" => "application/x-compress",
  464.         "zip" => "application/zip",
  465.         "zoo" => "application/octet-stream",
  466.         "zsh" => "text/x-script.zsh");
  467. }
  468. ?>

And finally, here is a small test case on usage:


  1. <?php
  2.  
  3. require_once 'SendEmail.php';
  4.  
  5. $testNoAttachment = TRUE;
  6. $testAttachment = TRUE;
  7.  
  8. if ($testNoAttachment) {
  9.     $to = NULL;
  10.     $cc = NULL;
  11.     $bcc = NULL;
  12.  
  13.     $from = "Your Name <you@gmail.com>";
  14.     $to = array("someone@example.com", "anothersomeone@example.com");
  15.     $cc = array("thirdperson@example.com", "fourthperson@example.com");
  16.     $bcc = array("fifthperson@example.com");
  17.     $subject = "My Subject";
  18.     $message = "<h1>Hi There</h1><br />How are you?<br /><br />"
  19.              . "<strong>Great</strong>";
  20.     $host = "smtp.gmail.com";
  21.     $emailUserName = "you@gmail.com";
  22.     $emailPassword = "Password";
  23.     $replyTo = "you@gmail.com";
  24.     $plainText = FALSE; // Use html
  25.  
  26.     echo SendEmail::sendMailNoAttachments($from, $to, $cc, $bcc, $subject,
  27.             $message, $host, $emailUserName, $emailPassword, $replyTo,
  28.             $plainText). "\n";
  29. }
  30.  
  31. if ($testAttachment) {
  32.     $to = NULL;
  33.     $cc = NULL;
  34.     $bcc = NULL;
  35.  
  36.     $from = "Your Name <you@example.com>";
  37.     $to = array("someone@example.com", "anothersomeone@example.com");
  38.     $cc = array("thirdperson@example.com", "fourthperson@example.com");
  39.     $bcc = array("fifthperson@example.com");
  40.     $subject = "My Subject";
  41.     $message = "Hello there";
  42.     $host = "smtp.example.com";
  43.     $emailUserName = "you@example.com";
  44.     $emailPassword = "Password";
  45.     $replyTo = "you@example.com";
  46.     $plainText = TRUE; // Use plain text
  47.     $attachments = array(getcwd(). "test_attachments/smppv50.pdf");
  48.  
  49.     echo SendEmail::sendMailWithAttachments($from, $to, $cc, $bcc, $subject,
  50.             $message, $host, $emailUserName, $emailPassword, $replyTo,
  51.             $plainText, $attachments). "\n";
  52. }
  53. ?>

That's all there is to it.

By Brian Last updated: 13 August 2010, 14:36

Comments

Gravatar Image #1 AK 2656 days ago.

Hey Thanks a lot!

This will come in real handy in the new PHP app. Looking forward to more of your posts!

AK (Go Go Elite Traveller)

Comments now closed.