Report abuse

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
<?php
// +---------------------------------------------------------------------------+
// | This file is part of the %%PROJECT_NAME%% project.                        |
// | Copyright (C) Lemieux Bedard Communication                                |
// |                                                                           |
// | For the full copyright and license information, please view the LICENSE   |
// | file that was distributed with this source code.                          |
// +---------------------------------------------------------------------------+

/**
 * LemieuxMail is a class which used to make the process of sending mails using
 * the eazycomponents mail class easier. This class use templates to create
 * the mail content.
 * @package core
 * @subpackage mail
 * @author Jean-Philippe Dery (jean-philippe.dery@lemieuxbedard.com)
 * @copyright Lemieux Bedard Communication (jean-philippe.dery@lemieuxbedard.com)
 * @since 1.0.0
 * @version 1.0.0
 */
class LemieuxMail extends AgaviAttributeHolder
{
	/**
	 * @var object The agavi context.
	 */
	protected $context = null;

	/**
	 * @var object The template parser object.
	 */
	protected $template = null;

	/**
	 * @var object The swift email sender object.
	 */
	protected $swift = null;

	/**
	 * @var string The receiver.
	 */
	protected $to = null;
	
	/**
	 * @var string The sender.
	 */
	protected $from = null;
	
	/**
	 * @var string The subject.
	 */
	protected $subject = null;

	/**
	 * @var string CC and BCC.
	 */
	protected $cc, $bcc = null;

	/**
	 * Constructor. Set the template object which render the message to be send
	 * to the user. Since the email is sent using ezcomponents mail classes, the
	 * content type of the teplate determine wheteher or not the nature of the
	 * mail composer object.
	 * @param object The agavi context.
	 * @param string The template name.
	 * @param string The receiver email address.
	 * @param string The receiver email name.
	 * @return void
	 * @author Jean-Philippe Dery (jean-philippe.dery@lemieuxbedard.com)
	 * @since 1.0.0
	 */
	function __construct(AgaviContext $context, $template, $subject, $toName, $toEmail, $fromName, $fromEmail)
	{
		$this->context = $context;
		$this->template = $template;
		$this->subject = $subject;
		$this->setFrom($fromName, $fromEmail);
		$this->setTo($toName, $toEmail);
		$this->swift = new Swift($this->getSwiftConnection());		
	}

	/**
	 * Set the receiver email address. This method accepts only email address
	 * without the receiver's name. Please note that this parameter may contains
	 * agavi config variables.
	 * @param string The email address.
	 * @return void
	 * @author Jean-Philippe Dery (jean-philippe.dery@lemieuxbedard.com)
	 * @since 1.0.0
	 */
	public function setTo($name, $address)
	{
		$this->to = new LemieuxMailAddress($address, $name);
	}

	/**
	 * Set the receiver email address. This method accepts only email address
	 * without the receiver's name. Please note that this parameter may contains
	 * agavi config variables.
	 * @param string The email address.
	 * @param string The encoding.
	 * @return void.
	 * @author Jean-Philippe Dery (jean-philippe.dery@lemieuxbedard.com)
	 * @since 1.0.0
	 */
	public function setCC($name, $address)
	{
		$this->cc = new LemieuxMailAddress($address, $name);
	}

	/**
	 * Clear the CC value.
	 * @return void.
	 * @author Jean-Philippe Dery (jean-philippe.dery@lemieuxbedard.com)
	 * @since 1.0.0
	 */
	public function clearCC()
	{
		$this->cc = null;
	}

	/**
	 * Set the receiver email address. This method accepts only email address
	 * without the receiver's name. Please note that this parameter may contains
	 * agavi config variables.
	 * @param string The email address.
	 * @param string The email name.
	 * @param string The encoding.
	 * @return void.
	 * @author Jean-Philippe Dery (jean-philippe.dery@lemieuxbedard.com)
	 * @since 1.0.0
	 */
	public function setFrom($name, $address)
	{
		$this->from = new LemieuxMailAddress($address, $name);
	}

	/**
	 * Set the subject of the mail address. Please note that this parameter
	 * may contains agavi config variables.
	 * @param string The subject.
	 * @return void.
	 * @author Jean-Philippe Dery (jean-philippe.dery@lemieuxbedard.com)
	 * @since 1.0.0
	 */
	public function setSubject($subject)
	{
		$this->subject = $subject;
	}

	/**
	 * Return the swift connection object.
	 * @return object The swift connection object.
	 * @author Jean-Philippe Dery (jean-philippe.dery@lemieuxbedard.com)
	 * @since 1.0.0
	 */
	protected function getSwiftConnection()
	{
		return AgaviConfig::get('core.debug') ? return new Swift_Connection_Sendmail() : return new Swift_Connection_SMTP('mail.lemieuxbedard.com');
	}
	
	/**
	 * Return the swift message object.
	 * @return object The swift connection object.
	 * @author Jean-Philippe Dery (jean-philippe.dery@lemieuxbedard.com)
	 * @since 1.0.0
	 */
	protected function getSwiftMessage()
	{
		return new Swift_Message($this->subject, $this->render());
	}

    /**
	 * Start the rendering process by loading the template file under an output
	 * buffer and caching the results and display it. The common object used in
	 * most agavi template will be created too.
	 * @return string The rendered result.
     * @author Jean-Philippe Dery (jean-philippe.dery@lemieuxbedard.com)
     * @since 1.0.0
	 */
	public function render()
	{
		$layer = new LemieuxMailTemplateLayer();
		$layer->initialize($this->context);
		$layer->setParameter('template', $this->template);
		$layer->setParameter('extension', '.php');
		$renderer = new LemieuxMailRenderer();
		$renderer->initialize($this->context, array('assigns' => array('routing' => 'ro', 'translation_manager' => 'tm')));
		return $renderer->render($layer, $this->getAttributes());
	}

	/**
	 * Send the email. Replace all the variable for the value and send the
	 * email to the receiver.
	 * @return void.
	 * @author Jean-Philippe Dery (jean-philippe.dery@lemieuxbedard.com)
	 * @since 1.0.0
	 * @todo Make better.
	 */
	public function send()
	{
		$message = $this->getSwiftMessage();
		// add the cc and bcc if they have been set. Only the sender and receiver
		// informations are required
		if ($this->cc != null) $message->setCC($this->cc->mail, $this->cc->name); 
		if ($this->bcc != null) $message->setBcc($this->bcc->mail, $this->bcc->name);
		// at this point we are ready to send the email
		return $this->swift->send($message,
			new Swift_Address($this->to->mail, $this->to->name),
			new Swift_Address($this->from->mail, $this->from->name)
		);
	}
}

/**
 * LemieuxMailAddress is a simple component to store an email address with the
 * name of the owner.
 * @package core
 * @subpackage mail
 * @author Jean-Philippe Dery (jean-philippe.dery@lemieuxbedard.com)
 * @copyright Lemieux Bedard Communication (jean-philippe.dery@lemieuxbedard.com)
 * @since 1.0.0
 * @version 1.0.0
 */
class LemieuxMailAddress 
{
	public $name = '';
	public $mail = '';
	public function __construct($mail, $name = '') {
		$this->name = $name;
		$this->mail = $mail;
	}
}
?>

<?php
// +---------------------------------------------------------------------------+
// | This file is part of a ImNoDesigner project.                              |
// | Copyright (C) Lemieux Bedard Communication                                |
// |                                                                           |
// | For the full copyright and license information, please view the LICENSE   |
// | file that was distributed with this source code.                          |
// +---------------------------------------------------------------------------+

/**
 * A renderer produces the output as defined by a View.
 * @package core
 * @subpackage control
 * @author David Zülke <dz@bitxtender.com>
 * @author Jean-Philippe Dery (jean-philippe.dery@lemieuxbedard.com)
 * @copyright Lemieux Bedard Communication (jean-philippe.dery@lemieuxbedard.com)
 * @since 1.0.0
 * @version 1.0.0
 */
class LemieuxMailRenderer extends AgaviPhpRenderer
{
	protected $moreAssigns = array();
}
?>

<?php
// +---------------------------------------------------------------------------+
// | This file is part of a ImNoDesigner project.                              |
// | Copyright (C) Lemieux Bedard Communication                                |
// |                                                                           |
// | For the full copyright and license information, please view the LICENSE   |
// | file that was distributed with this source code.                          |
// +---------------------------------------------------------------------------+

/**
 * Template layer implementation for templates fetched using a PHP stream.
 * @package core
 * @subpackage control
 * @author David Zülke <dz@bitxtender.com>
 * @author Jean-Philippe Dery (jean-philippe.dery@lemieuxbedard.com)
 * @copyright  Jean-Philippe Dery
 * @since 1.0.0
 * @version 1.0.0
 */
class LemieuxMailTemplateLayer extends AgaviStreamTemplateLayer
{
	/**
	 * Constructor
	 * @param array Initial parameters.
	 * @author David Zülke <dz@bitxtender.com>
	 * @since 0.11.0
	 */
	public function __construct(array $parameters = array())
	{
		$targets = array();
		if (AgaviConfig::get('core.use_translation')) {
			$targets[] = '${directory}/${locale}/${template}${extension}';
			$targets[] = '${directory}/${template}.${locale}${extension}';
		}
		$targets[] = '${directory}/${template}${extension}';
		parent::__construct(array_merge(array(
			'directory' => AgaviConfig::get('core.template_dir') . '/mails',
			'scheme' => 'file',
			'check' => true,
			'targets' => $targets,
		), $parameters));
	}
}
?>