/
mailer.gtw
151 lines (108 loc) · 4.85 KB
/
mailer.gtw
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
~~LANG:FR@frman:mail~~
jMailer is a useful class for sending emails in controllers or business classes. It is based on the famous [[http://phpmailer.codeworxtech.com|PHPMailer]] class (version 2.3).
Here are its features:
* support **attachments**
* **HTML** or **TEXT** email
* **Multiple recipients**
* **Multiple sending** channel (mail(), sendmail)
* **SMTP authentication** support
* Mail **templates** through jTpl
You can find many PHPMailer use cases over the web. So, we won't document all of the features above.
However, jMailer brings some additional features :
* default configuration of jMailer is stored in the configuration file of the application
* you can use jTpl templates to generate your emails content.
* errors messages are localized in properties files (jLocale)
* errors are raised and logged through exceptions.
===== Configuration file =====
jMailer options shall be edited in //var/config/defaultconfig.ini.php//. Look at the [mailer] section.
==== Configuration options ====
* //webmasterEmail//: default sender email
* //webmasterName//: default sender name
* //mailerType//: indicates how to send mails (default: "mail")
* "mail": PHP mail function
* "sendmail": use the server program
* "smtp": direct connection to an SMTP host
* //hostname//: used in //Message-Id// and //Received// headers, and also as default //HELO// string. If empty, the value returned by SERVER_NAME is used or 'localhost.localdomain'.
* //sendmailPath//: path to sendmail program
To send mails with a SMTP server:
* //smtpHost//: SMTP hostname
* //smtpPort//: SMTP port
* //smtpSecure//: secured connection or not (possible value: "", "ssl", "tls")
* //smtpHelo//: SMTP //Helo// message
* //smtpAuth//: turn on/off SMTP authentication
* //smtpUsername//: SMTP login
* //smtpPassword//: SMTP password
* //smtpTimeout//: SMTP server time out
jMailer uses also general parameters //locale// and //charset// to setup the language and charset of emails.
Of course, you can change all of these parameters directly with properties and methods of jMailer class. See reference documentation.
===== Sending a simple mail =====
Sending a text email.
<code php>
$mail = new jMailer();
$mail->Subject = 'Subject';
$mail->Body = 'Text content';
$mail->AddAddress('recipientId@exemple.com' , 'recipient name');
$mail->Send();
</code>
===== Sending a mail using a template =====
You can use the powerfull template engine of Jelix to send an email. You have to create a template in your module. In this template, you can use meta values to specify your email parameters.
Example, in modules/mymodule/templates/supermail.tpl
<code html>
{meta Subject 'Super mail.'}
{meta Priority 1}
{meta From 'your name<your@email.com>'}
{meta Sender 'your@email.com'}
{meta ReplyTo array('your name<your@email.com>')}
{meta to array($name.'<'.$email.'>')}
{meta cc array('titi <titi@example.com>')}
{meta bcc array('tutu <tutu@example.com>')}
Hello {$name},
Here is an email generated by jMailer and Jelix.
</code>
And in your PHP code :
<code php>
$mail = new jMailer();
$tpl = $mail->Tpl('mymodule~supermail');
$tpl->assign('name', 'Mister Toto');
$tpl->assign('email', 'toto@example.com');
$mail->Send();
</code>
Notice that @@C@jMailer@@ will create template variables corresponding to some PHPMailer properties, so you can use it them anywhere in the template
<code html>
{meta Subject 'Super mail.'}
{meta Priority 1}
{meta From 'your name<your@email.com>'}
{meta Sender 'your@email.com'}
{meta ReplyTo array('your name<your@email.com>')}
{meta to array($name.'<'.$email.'>')}
{meta cc array('titi <titi@example.com>', 'tutu <tutu@example.com>')}
Hello {$name},
Here is an email generated by jMailer and Jelix.
This email is send to :
{foreach $cc as $person}
- {$person[0]}
{/foreach}
</code>
===== Sending an HTML email =====
You have to call the @@M@isHTML@@ method like in this example:
<code php>
$mail = new jMailer();
$mail->isHTML(true);
$mail->Body = "<p>My <strong>HTML</strong> email !</p>";
$mail->AddAddress('toto@example.com' , 'Toto');
$mail->Send();
</code>
If you use a HTML template, indicate @@true@@ to the method @@M@Tpl@@.
<code php>
$tpl = $mail->Tpl('mymodule~supermail', true);
</code>
===== Sending an attachment =====
You can add attachment easily in your email, by giving the path of the file stored somewhere on the hard drive. You can also indicate an other name for the file.
<code php>
$mail = new jMailer();
$mail->AddAttachment(jApp::varPath('original_filename.pdf'), 'new_filename.pdf');
...
$mail->Send();
</code>
===== Advanced features =====
Since jMailer inherits from the PHPmailer class, read [[http://phpmailer.worxware.com/index.php?pg=methods|the documentation of PHPMailer]] to know more about its features.