Source for file zmail.inc.php

Documentation is available at zmail.inc.php

  1. <?php
  2. /**
  3. *  This file is part of the VCL for PHP project
  4. *
  5. *  Copyright (c) 2004-2008 qadram software S.L. <support@qadram.com>
  6. *
  7. *  Checkout AUTHORS file for more information on the developers
  8. *
  9. *  This library is free software; you can redistribute it and/or
  10. *  modify it under the terms of the GNU Lesser General Public
  11. *  License as published by the Free Software Foundation; either
  12. *  version 2.1 of the License, or (at your option) any later version.
  13. *
  14. *  This library is distributed in the hope that it will be useful,
  15. *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  17. *  Lesser General Public License for more details.
  18. *
  19. *  You should have received a copy of the GNU Lesser General Public
  20. *  License along with this library; if not, write to the Free Software
  21. *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
  22. *  USA
  23. *
  24. */
  25.  
  26. require_once("vcl/vcl.inc.php");
  27. use_unit("Zend/zcommon.inc.php");
  28. use_unit("classes.inc.php");
  29.  
  30. //Include all Zend Framework stuff required
  31. use_unit("Zend/framework/library/Zend/Mail.php");
  32. use_unit("Zend/framework/library/Zend/Mail/Transport/Smtp.php");
  33. use_unit("Zend/framework/library/Zend/Mail/Transport/Sendmail.php");
  34.  
  35. /**
  36. * A non-visual component to let you send e-mail easily
  37. *
  38. * Use this component if you need to send e-mail in your application. This Zend Framework
  39. * based component allows you to decide the transport (SMTP, Sendmail, etc) and provide properties
  40. * to let you set the target of the e-mails, the Cc, the Bcc, etc.
  41. * Is also able to send e-mail in HTML and include attachments.
  42. */
  43. class ZMail extends Component
  44. {
  45.     protected $_bodytext="";
  46.  
  47.     /**
  48.     * Use this property to specify the body of the e-mail in plain-text format.
  49.     *
  50.     * This property is a string property you can use to set the contents of the
  51.     * e-mail in plain-text format.
  52.     *
  53.     * @return string 
  54.     */
  55.     function getBodyText(return $this->_bodytext}
  56.     function setBodyText($value$this->_bodytext=$value}
  57.     function defaultBodyText(return ""}
  58.  
  59.     protected $_bodyhtml="";
  60.  
  61.     /**
  62.     * Use this property to specify the body of the e-mail in HTML format
  63.     *
  64.     * You can use it in combination with BodyText to send a multipart e-mail in
  65.     * which the contents can be selected to be shown in plain-text or in HTML.
  66.     *
  67.     * @return string 
  68.     */
  69.     function getBodyHTML(return $this->_bodyhtml}
  70.     function setBodyHTML($value$this->_bodyhtml=$value}
  71.     function defaultBodyHTML(return ""}
  72.  
  73.     protected $_subject="";
  74.  
  75.     /**
  76.     * Specifies the subject of the e-mail to be sent
  77.     *
  78.     * @return string 
  79.     */
  80.     function getSubject(return $this->_subject}
  81.     function setSubject($value$this->_subject=$value}
  82.     function defaultSubject(return ""}
  83.  
  84.     protected $_fromname="";
  85.  
  86.     /**
  87.     * It allows you to select the name of the sender of the e-mail
  88.     *
  89.     * Use it in combination with FromEmail to add the real name of the sender
  90.     *
  91.     * i.e.  John Doe <john.doe@unknown.com>
  92.     *
  93.     * John Doe is the FromName property, while john.doe@unknown.com is FromEmail
  94.     *
  95.     * @return string 
  96.     */
  97.     function getFromName(return $this->_fromname}
  98.     function setFromName($value$this->_fromname=$value}
  99.     function defaultFromName(return ""}
  100.  
  101.     protected $_fromemail="";
  102.  
  103.     /**
  104.     * It allows you to select the email of the sender
  105.     *
  106.     * Use it in combination with FromName to add e-mail of the sender
  107.     *
  108.     * i.e.  John Doe <john.doe@unknown.com>
  109.     *
  110.     * John Doe is the FromName property, while john.doe@unknown.com is FromEmail
  111.     *
  112.     * @return string 
  113.     */
  114.     function getFromEmail(return $this->_fromemail}
  115.     function setFromEmail($value$this->_fromemail=$value}
  116.     function defaultFromEmail(return ""}
  117.  
  118.     protected $_transport=null;
  119.  
  120.     /**
  121.     * Use this property to specify the transport to use to send the e-mail
  122.     *
  123.     * By default, the sendmail method is used, is basically like using the PHP mail() function, this method
  124.     * is used if no transport is attached.
  125.     *
  126.     * But you can also customize the process by using some ZMailTransport class, like ZMailTransportSMTP or
  127.     * ZMailTransportSendmail.
  128.     * Those components also have properties can be modified to customize the sending process.
  129.     *
  130.     * @return ZMailTransport 
  131.     */
  132.     function getTransport(return $this->_transport}
  133.     function setTransport($value$this->_transport=$this->fixupProperty($value)}
  134.     function defaultTransport(return null}
  135.  
  136.     function loaded()
  137.     {
  138.         parent::loaded();
  139.         $this->setTransport($this->_transport);
  140.     }
  141.  
  142.     protected $_to=array();
  143.  
  144.     /**
  145.     * This property specifies the targets of the e-mail
  146.     *
  147.     * This is an array property to allow you specify the recipients of the e-mail
  148.     * you want to send.
  149.     *
  150.     * The array has a form of:
  151.     *
  152.     * <code>
  153.     * array(
  154.     * 'e-mail 1'=>'recipient name 1',
  155.     * 'e-mail 2'=>'recipient name 2'
  156.     * );
  157.     * </code>
  158.     *
  159.     * At design-time, you can use the property editor to make up this array.
  160.     *
  161.     * @return array 
  162.     */
  163.     function getTo(return $this->_to}
  164.     function setTo($value$this->_to=$value}
  165.     function defaultTo(return array()}
  166.  
  167.     protected $_cc=array();
  168.  
  169.     /**
  170.     * This property specifies the copies of the e-mail
  171.     *
  172.     * This is an array property to allow you specify the extra recipients of the e-mail
  173.     * you want to send.
  174.     *
  175.     * The array has a form of:
  176.     *
  177.     * <code>
  178.     * array(
  179.     * 'e-mail 1'=>'recipient name 1',
  180.     * 'e-mail 2'=>'recipient name 2'
  181.     * );
  182.     * </code>
  183.     *
  184.     * At design-time, you can use the property editor to make up this array.
  185.     *
  186.     * @return array 
  187.     */
  188.     function getCc(return $this->_cc}
  189.     function setCc($value$this->_cc=$value}
  190.     function defaultCc(return array()}
  191.  
  192.     protected $_bcc=array();
  193.  
  194.     /**
  195.     * This property specifies the hidden copies of the e-mail
  196.     *
  197.     * This is an array property to allow you specify the extra hidden recipients of the e-mail
  198.     * you want to send.
  199.     *
  200.     * The array has a form of:
  201.     *
  202.     * <code>
  203.     * array(
  204.     * 'e-mail 1'=>'recipient name 1',
  205.     * 'e-mail 2'=>'recipient name 2'
  206.     * );
  207.     * </code>
  208.     *
  209.     * At design-time, you can use the property editor to make up this array.
  210.     *
  211.     * @return array 
  212.     */
  213.     function getBcc(return $this->_bcc}
  214.     function setBcc($value$this->_bcc=$value}
  215.     function defaultBcc(return array()}
  216.  
  217.     protected $_attachments=array();
  218.  
  219.     /**
  220.     * This property specifies the attachments you want to add to the e-mail
  221.     *
  222.     * This is an array property to allow you specify the attachments you want to
  223.     * add to the e-mail
  224.     *
  225.     * The array has a form of:
  226.     *
  227.     * <code>
  228.     * array(
  229.     * '0'=>'pathtothefile 1',
  230.     * '0'=>'pathtothefile 2'
  231.     * );
  232.     * </code>
  233.     *
  234.     * At design-time, you can use the property editor to make up this array.
  235.     *
  236.     * By default, the attachments are interpreted as paths to the files you want to
  237.     * attach, but if you want to customize the attachments to include other information,
  238.     * or to customize the way attachments are handled, use the OnCustomizeAttachment, which
  239.     * let's you select the content, mimetype, disposition and filename for an attachment.
  240.     *
  241.     * @return array 
  242.     */
  243.     function getAttachments(return $this->_attachments}
  244.     function setAttachments($value$this->_attachments=$value}
  245.     function defaultAttachments(return array()}
  246.  
  247.     protected $_oncustomizeattachment=null;
  248.  
  249.     /**
  250.     * This event allows you to customize how attachments are handled.
  251.     *
  252.     * Use this event if you want to change the way attachments are handled, this event
  253.     * is fired just before add the attachment to the e-mail, and you get in $params
  254.     * the information of the attachment. Index key and name you set on the Attachments property.
  255.     *
  256.     * To modify the way to handle an specific attachment, return an array with the new information.
  257.     *
  258.     * The $params property has this form:
  259.     *
  260.     * <code>
  261.     * array(
  262.     * '0'=>'attachment 1',
  263.     * '1'=>'attachment 2'
  264.     * );
  265.     * </code>
  266.     *
  267.     * You have to return an array which can have the following values:
  268.     * <code>
  269.     * array(
  270.     * 'body'=>'binary content for the attachment', // If this is not set, the contents will be gathered from a file
  271.     * 'mimetype'=>'type of the attachment',        // i.e.: image/gif, you can use the Zend_Mime constants
  272.     * 'disposition'=>'attachment',                 // You can use attachment or inline
  273.     * 'encodign'=>'base64',                        // You can use the Zend_Mime constants
  274.     * 'filename'=>'nameforthefile'                 // Name of the file to be shown
  275.     * );
  276.     * </code>
  277.     *
  278.     * If some of the values are not present, the default value will be used.
  279.     *
  280.     * <code>
  281.     *  function zmCustomCustomizeAttachment($sender, $params)
  282.     *  {
  283.     *   $result=array();
  284.     *   list($key, $attachment)=each($params);
  285.     *   if ($attachment=='index.php')
  286.     *   {
  287.     *       $result['mimetype']=Zend_Mime::TYPE_TEXT;
  288.     *       $result['disposition']=Zend_Mime::DISPOSITION_INLINE;
  289.     *   }
  290.     *   return($result);
  291.     *  }
  292.     * </code>
  293.     *
  294.     * @return mixed 
  295.     */
  296.     function getOnCustomizeAttachment(return $this->_oncustomizeattachment}
  297.     function setOnCustomizeAttachment($value$this->_oncustomizeattachment=$value}
  298.     function defaultOnCustomizeAttachment(return null}
  299.  
  300.     protected $_headers=array();
  301.  
  302.     /**
  303.     * If you need to add your custom headers to the message, use this property
  304.     *
  305.     * This property allows you to add your own headers to the mail message, it's
  306.     * an array property in the form:
  307.     *
  308.     * <code>
  309.     * array(
  310.     * '0'=>'header=value',
  311.     * '1'=>'header=value'
  312.     * );
  313.     * </code>
  314.     *
  315.     * You can use the same header name several times, so the values will be added
  316.     *
  317.     * @return array 
  318.     */
  319.     function getHeaders(return $this->_headers}
  320.     function setHeaders($value$this->_headers=$value}
  321.     function defaultHeaders(return array()}
  322.  
  323.     /**
  324.     * Sends the e-mail
  325.     *
  326.     * Use this method once all the properties and attachments are set. This method also
  327.     * fires the OnCustomizeAttachment when is adding attachments to the e-mail.
  328.     */
  329.     function send()
  330.     {
  331.         $mail = new Zend_Mail();
  332.  
  333.         if (trim($this->_fromemail)!=''$mail->setFrom($this->_fromemail,$this->_fromname);
  334.  
  335.         if (trim($this->_bodytext)!=''$mail->setBodyText($this->_bodytext);
  336.  
  337.         if (trim($this->_bodyhtml)!=''$mail->setBodyHtml($this->_bodyhtml);
  338.  
  339.         if (trim($this->_subject)!=''$mail->setSubject($this->_subject);
  340.  
  341.         if (count($this->_to)>=1)
  342.         {
  343.             reset($this->_to);
  344.             while(list($email$name)=each($this->_to)) $mail->addTo($email,$name);
  345.         }
  346.  
  347.         if (count($this->_cc)>=1)
  348.         {
  349.             reset($this->_cc);
  350.             while(list($email$name)=each($this->_cc)) $mail->addCc($email,$name);
  351.         }
  352.  
  353.         if (count($this->_bcc)>=1)
  354.         {
  355.             reset($this->_bcc);
  356.             while(list($email$name)=each($this->_bcc)) $mail->addBcc($email,$name);
  357.         }
  358.  
  359.         if (count($this->_attachments)>=1)
  360.         {
  361.             reset($this->_attachments);
  362.             while(list($key$path)=each($this->_attachments))
  363.             {
  364.                 $contents='';
  365.                 $mimetype=Zend_Mime::TYPE_OCTETSTREAM;
  366.                 $disposition=Zend_Mime::DISPOSITION_ATTACHMENT;
  367.                 $encoding=Zend_Mime::ENCODING_BASE64;
  368.                 $filename=basename($path);
  369.                 $params=array();
  370.  
  371.                 if ($this->_oncustomizeattachment!=null)
  372.                 {
  373.                     $params=$this->callEvent('oncustomizeattachment',array($key=>$path));
  374.                 }
  375.  
  376.                 if (isset($params['body'])) $body=$params['body'];
  377.                 else $body=file_get_contents($path);
  378.  
  379.                 if (isset($params['mimetype'])) $mimetype=$params['mimetype'];
  380.                 if (isset($params['disposition'])) $disposition=$params['disposition'];
  381.                 if (isset($params['encoding'])) $encoding=$params['encoding'];
  382.                 if (isset($params['filename'])) $filename=$params['filename'];
  383.  
  384.                 $mail->createAttachment($body,$mimetype,$disposition,$encoding,$filename);
  385.             }
  386.         }
  387.  
  388.         if (count($this->_headers)>=1)
  389.         {
  390.             $keys=array();
  391.             reset($this->_headers);
  392.             while(list($key$line)=each($this->_headers))
  393.             {
  394.                 $parts=explode('=',$line);
  395.                 $header=$parts[0];
  396.                 $value=$parts[1];
  397.                 $append=array_key_exists($header,$keys);
  398.                 $mail->addHeader($header,$value,$append);
  399.                 $keys[$header]=1;
  400.             }
  401.  
  402.         }
  403.  
  404.         $transport=null;
  405.  
  406.         if (is_object($this->_transport))  $transport=$this->_transport->readTransport();
  407.  
  408.         $mail->send($transport);
  409.     }
  410. }
  411.  
  412. define('saNone','saNone');
  413. define('saPlain','saPlain');
  414. define('saLogin','saLogin');
  415. define('saCRAM_MD5','saCRAM_MD5');
  416.  
  417. /**
  418. * Base class for transports
  419. *
  420. * Inherit from this class if you want to create your own transport
  421. */
  422. class ZMailTransport extends Component
  423. {
  424. }
  425.  
  426. /**
  427. * Allows ZMail to use an SMTP server for sending e-mails
  428. *
  429. * This class inherits from ZMailTransport and allows you to customize every aspect
  430. * of the mail sending, like host, authentication, etc.
  431. */
  432. {
  433.     protected $_host="127.0.0.1";
  434.  
  435.     /**
  436.     * Use this property to specify the SMTP server
  437.     *
  438.     * This property must contain the SMTP server to use, it can be an IP address
  439.     * or a host name.
  440.     *
  441.     * @return string 
  442.     */
  443.     function getHost(return $this->_host}
  444.     function setHost($value$this->_host=$value}
  445.     function defaultHost(return "127.0.0.1"}
  446.  
  447.     protected $_authentication=saNone;
  448.  
  449.     /**
  450.     * Specifies the authentication method to use
  451.     *
  452.     * Valid values are:
  453.     *
  454.     * saNone
  455.     * saPlain
  456.     * saLogin
  457.     * saCRAM_MD5
  458.     *
  459.     * @return string 
  460.     */
  461.     function getAuthentication(return $this->_authentication}
  462.     function setAuthentication($value$this->_authentication=$value}
  463.     function defaultAuthentication(return saNone}
  464.  
  465.     protected $_username="";
  466.  
  467.     /**
  468.     * If the authentication method requires a user name, must be set on this
  469.     * property.
  470.     *
  471.     * @return string 
  472.     */
  473.     function getUserName(return $this->_username}
  474.     function setUserName($value$this->_username=$value}
  475.     function defaultUserName(return ""}
  476.  
  477.  
  478.     protected $_userpassword="";
  479.  
  480.     /**
  481.     * If the authentication method requires a password, must be set on this
  482.     * property.
  483.     *
  484.     * @return string 
  485.     */
  486.     function getUserPassword(return $this->_userpassword}
  487.     function setUserPassword($value$this->_userpassword=$value}
  488.     function defaultUserPassword(return ""}
  489.  
  490.     /**
  491.     * This method creates the Zend_Mail transport, set the properties and returns
  492.     * it.
  493.     *
  494.     * This method is called by ZMail component to create and customize the transport
  495.     * to be used for sending e-mails.
  496.     *
  497.     * @return Zend_Mail_Transport_Smtp 
  498.     */
  499.     function readTransport()
  500.     {
  501.         $config=array();
  502.  
  503.         switch ($this->_authentication)
  504.         {
  505.             case saPlain$config['auth']='plain'break;
  506.             case saLogin$config['auth']='login'break;
  507.             case saCRAM_MD5$config['auth']='crammd5'break;
  508.         }
  509.  
  510.         if ($this->_username!=''$config['username']=$this->_username;
  511.         if ($this->_userpassword!=''$config['password']=$this->_userpassword;
  512.  
  513.         $result=new Zend_Mail_Transport_Smtp($this->_host,$config);
  514.  
  515.         return($result);
  516.     }
  517.  
  518. }
  519.  
  520. /**
  521. * Allows ZMail to use sendmail for sending e-mails
  522. *
  523. * This class inherits from ZMailTransport and allows you add sendmail parameters
  524. * for the mail sending.
  525. * This transport is basically like using the PHP mail() function
  526. *
  527. */
  528. {
  529.     protected $_parameters="";
  530.  
  531.     /**
  532.     * This property allows you to set parameters to be sent to sendmail
  533.     *
  534.     * Use this property to specify parameters to sendmail
  535.     * i.e. '-freturn_to_me@example.com'
  536.     *
  537.     * @return string 
  538.     */
  539.     function getParameters(return $this->_parameters}
  540.     function setParameters($value$this->_parameters=$value}
  541.     function defaultParameters(return ""}
  542.  
  543.     /**
  544.     * This method creates the Zend_Mail transport, set the properties and returns
  545.     * it.
  546.     *
  547.     * This method is called by ZMail component to create and customize the transport
  548.     * to be used for sending e-mails.
  549.     *
  550.     * @return Zend_Mail_Transport_Sendmail 
  551.     */
  552.     function readTransport()
  553.     {
  554.         $result=new Zend_Mail_Transport_Sendmail($this->_parameters);
  555.         return($result);
  556.     }
  557.  
  558. }
  559.  
  560. ?>

Documentation generated on Fri, 26 Dec 2008 11:46:57 +0100 by phpDocumentor 1.4.0a2