FileMailer.php 4.62 KB
Newer Older
Roman Mátyus's avatar
Roman Mátyus committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<?php
namespace RM;

use Nette\Object,
	Nette\DateTime,
	Nette\FileNotFoundException,
	Nette\InvalidArgumentException,
	Nette\InvalidStateException,
	Nette\Utils\Strings,
	Nette\Mail\IMailer,
	Nette\Mail\Message;

/**
 * Class for storing mails to filesystem.
 *
 * Configuring:
 * 	services:
 * 		nette.mailer:
 * 			class: RM\FileMailer
 * 			setup:
 * 				- $tempDir(%appDir%/../temp/mails)
 *
Roman Mátyus's avatar
v0.2    
Roman Mátyus committed
23
24
 * @author Jan Drábek, Roman Mátyus
 * @copyright (c) Jan Drábek 2013
Roman Mátyus's avatar
Roman Mátyus committed
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
 * @copyright (c) Roman Mátyus 2013
 * @license MIT
 * @package FileMailer
 */
class FileMailer extends Object implements IMailer
{
	/** @var string */
	public $tempDir;

	/** @var string */
	private $prefix;

	public function __construct()
	{
		$now = new DateTime();
		$this->prefix = $now->format("YmdHis")."-";
	}

	/**
	 * Store mails to files.
	 * @param  Message $message
	 */
	public function send(Message $message)
	{
		$this->checkRequirements();
		$content = $message->generateMessage();
Roman Mátyus's avatar
v0.2    
Roman Mátyus committed
51
52
53
54

		preg_match('/Message-ID: <(?<message_id>\w+)@\w+>/', $content, $matches);

		$path = $this->tempDir."/".$this->prefix.$matches['message_id'];
Roman Mátyus's avatar
Roman Mátyus committed
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
		if ($bytes = file_put_contents($path, $content))
			return $bytes;
		else
			throw new InvalidStateException("Unable to write email to '".$path."'");
	}

	/**
	 * Check requirements.
	 */
	public function checkRequirements()
	{
		if (is_null($this->tempDir))
			throw new InvalidArgumentException("Directory for temporary files is not defined.");
		if (!is_dir($this->tempDir)) {
			mkdir($this->tempDir);
			if (!is_dir($this->tempDir)) 
				throw new FileNotFoundException("'".$this->tempDir."' is not directory.");
		}
		if (!is_writable($this->tempDir))
			throw new InvalidArgumentException("Directory '".$this->tempDir."' is not writeable.");
	}

77
78
79
	/**
	 * Parser of stored files.
	 * @param  string $content
Roman Mátyus's avatar
v0.2    
Roman Mátyus committed
80
	 * @param  string $filename
81
82
	 * @return StdClass
	 */
Roman Mátyus's avatar
v0.2    
Roman Mátyus committed
83
	public static function mailParser($content, $filename = NULL)
84
85
86
87
88
89
	{
		$mess = explode("\r\n\r\n", $content);
		preg_match_all("/[a-zA-Z-]*: .*/", $mess[0], $matches);
		$header = array();
		foreach ($matches[0] as $line) {
			$temp = explode(": ",$line);
Roman Mátyus's avatar
v0.2    
Roman Mátyus committed
90
			$header[strtolower($temp[0])] = iconv_mime_decode(Strings::trim($temp[1]));
91
92
93
		}
		if (isset($header["date"]))
			$header["date"] = new DateTime($header["date"]);
Roman Mátyus's avatar
v0.2    
Roman Mátyus committed
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126

		$message_id = explode("@",$header['message-id']);
		$message_id = substr($message_id[0], 1);

		$attachments = array();
		$mess = array(
					"plain" => NULL,
					"html" => NULL,
				);
		if (preg_match("/multipart\/mixed/", $content)) { // mail with attachments
			foreach (explode("----------",$content) as $part) {
				if (preg_match("/Content-Type: text\/plain; charset=UTF-8/", $part)) {
					$tmp = explode("\r\n\r\n", $part);
					$mess["plain"] = Strings::trim($tmp[1]);
				} elseif (preg_match("/Content-Type: text\/html; charset=UTF-8/", $part)) {
					$tmp = explode("\r\n\r\n", $part);
					$mess["html"] = Strings::trim($tmp[1]);
				} elseif (preg_match("/Content-Disposition: attachment;/", $part)) {
					$tmp = explode("\r\n", $part);
					unset($tmp[0]);
					$part = implode("\r\n", $tmp);
					$tmp = explode("\r\n\r\n", $part);
					$tmp_header = explode("\r\n", $tmp[0]);
					$output = array(
							"type" => substr($tmp_header[0], 14),
							"encoding" => substr($tmp_header[1], 27),
							"filename" => substr($tmp_header[2], 43, -1),
							"data" => $tmp[1],
						);
					$attachments[md5($output['type'].$output['encoding'].$output['filename'].$output['data'])] = (object) $output;
				}
			}
		} elseif (preg_match("/multipart\/alternative/", $content)) { // html mail
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
			$mess = explode("\r\n\r\n----------", $content);
			$mess = substr($mess[1], 10, -22);
			$mess = explode("----------", $mess);
			$temp_mess = array();
			foreach ($mess as $part) {
				if (preg_match("/text\/html/", $part))
					$temp_mess["html"] = $part;
				elseif (preg_match("/text\/plain/", $part))
					$temp_mess["plain"] = $part;
			}
			$mess = $temp_mess;
			$temp_mess = array();
			foreach ($mess as $type => $part) {
				$temp_mess[$type] = explode("\r\n",$part);
				for($i=0;$i<=3;$i++)
					unset($temp_mess[$type][$i]);
				$temp_mess[$type] = implode("\r\n",$temp_mess[$type]);
			}
			$mess = $temp_mess;
Roman Mátyus's avatar
v0.2    
Roman Mátyus committed
146
		} elseif (preg_match("/text\/plain/", $content)) { // plaintext mail
147
			$mess = array(
Roman Mátyus's avatar
v0.2    
Roman Mátyus committed
148
						"plain" => $mess[1],
149
150
151
152
153
154
						"html" => NULL,
					);
		}

		return (object) array_merge(
				array(
Roman Mátyus's avatar
v0.2    
Roman Mátyus committed
155
					"filename" => $filename,
156
157
158
159
160
					"message_id" => $message_id,
					"header" => $header,
					"plain" => $mess['plain'],
					"html" => $mess['html'],
					"raw" => $content,
Roman Mátyus's avatar
v0.2    
Roman Mátyus committed
161
					"attachments" => $attachments,
162
163
164
165
				),
				$header
			);
	}
Roman Mátyus's avatar
v0.2    
Roman Mátyus committed
166
167
168
169
170
171

	public function getPrefix()
	{
		return $this->prefix;
	}
}