TYPO3 API  SVNRelease
class.t3lib_mail_mboxtransport.php
Go to the documentation of this file.
00001 <?php
00002 /***************************************************************
00003  *  Copyright notice
00004  *
00005  *  (c) 2010-2011 Ernesto Baschny <ernst@cron-it.de>
00006  *  All rights reserved
00007  *
00008  *  This script is part of the TYPO3 project. The TYPO3 project is
00009  *  free software; you can redistribute it and/or modify
00010  *  it under the terms of the GNU General Public License as published by
00011  *  the Free Software Foundation; either version 2 of the License, or
00012  *  (at your option) any later version.
00013  *
00014  *  The GNU General Public License can be found at
00015  *  http://www.gnu.org/copyleft/gpl.html.
00016  *  A copy is found in the textfile GPL.txt and important notices to the license
00017  *  from the author is found in LICENSE.txt distributed with these scripts.
00018  *
00019  *
00020  *  This script is distributed in the hope that it will be useful,
00021  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00022  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00023  *  GNU General Public License for more details.
00024  *
00025  *  This copyright notice MUST APPEAR in all copies of the script!
00026  ***************************************************************/
00027 
00028 /**
00029  * Adapter for Swift_Mailer to be used by TYPO3 extensions.
00030  *
00031  * This will use the setting in TYPO3_CONF_VARS to choose the correct transport
00032  * for it to work out-of-the-box.
00033  *
00034  * $Id$
00035  *
00036  * @author Ernesto Baschny <ernst@cron-it.de>
00037  * @package TYPO3
00038  * @subpackage t3lib
00039  */
00040 class t3lib_mail_MboxTransport implements Swift_Transport {
00041 
00042     /**
00043      * @var string The file to write our mails into
00044      */
00045     private $debugFile;
00046 
00047     /**
00048      * Create a new MailTransport
00049      * @param Swift_Transport_Log $log
00050      */
00051     public function __construct($debugFile) {
00052         $this->debugFile = $debugFile;
00053     }
00054 
00055     /**
00056      * Not used.
00057      */
00058     public function isStarted() {
00059         return FALSE;
00060     }
00061 
00062     /**
00063      * Not used.
00064      */
00065     public function start() {
00066     }
00067 
00068     /**
00069      * Not used.
00070      */
00071     public function stop() {
00072     }
00073 
00074     /**
00075      * Outputs the mail to a text file according to RFC 4155.
00076      *
00077      * @param Swift_Mime_Message $message The message to send
00078      * @param string[] &$failedRecipients To collect failures by-reference, nothing will fail in our debugging case
00079      * @return int
00080      * @throws Exception
00081      */
00082     public function send(Swift_Mime_Message $message, &$failedRecipients = null) {
00083         $message->generateId();
00084 
00085             // Create a mbox-like header
00086         $mboxFrom = $this->getReversePath($message);
00087         $mboxDate = strftime('%c', $message->getDate());
00088         $messageStr = sprintf('From %s  %s', $mboxFrom, $mboxDate) . LF;
00089 
00090             // Add the complete mail inclusive headers
00091         $messageStr .= $message->toString();
00092         $messageStr .= LF . LF;
00093 
00094             // Write the mbox file
00095         $file = @fopen($this->debugFile, 'a');
00096         if (!$file) {
00097             throw new Exception(
00098                 sprintf('Could not write to file "%s" when sending an email to debug transport', $this->debugFile),
00099                 1291064151
00100             );
00101         }
00102 
00103         flock($file, LOCK_EX);
00104         @fwrite($file, $messageStr);
00105         flock($file, LOCK_UN);
00106         @fclose($file);
00107 
00108         t3lib_div::fixPermissions($this->debugFile);
00109 
00110             // Return every receipient as "delivered"
00111         $count = (
00112             count((array) $message->getTo())
00113             + count((array) $message->getCc())
00114             + count((array) $message->getBcc())
00115         );
00116         return $count;
00117     }
00118 
00119     /**
00120      * Determine the best-use reverse path for this message
00121      *
00122      * @param Swift_Mime_Message $message
00123      * @return mixed|null
00124      */
00125     private function getReversePath(Swift_Mime_Message $message) {
00126         $return = $message->getReturnPath();
00127         $sender = $message->getSender();
00128         $from = $message->getFrom();
00129         $path = NULL;
00130         if (!empty($return)) {
00131             $path = $return;
00132         } elseif (!empty($sender)) {
00133             $keys = array_keys($sender);
00134             $path = array_shift($keys);
00135         } elseif (!empty($from)) {
00136             $keys = array_keys($from);
00137             $path = array_shift($keys);
00138         }
00139         return $path;
00140     }
00141 
00142     /**
00143      * Register a plugin in the Transport.
00144      *
00145      * @param Swift_Events_EventListener $plugin
00146      */
00147     public function registerPlugin(Swift_Events_EventListener $plugin) {
00148         return TRUE;
00149     }
00150 }
00151 
00152 if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_mail_mboxtransport.php']) {
00153     include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_mail_mboxtransport.php']);
00154 }
00155 
00156 ?>