1
|
|
|
<?php |
|
|
|
|
2
|
|
|
(!cfip()) ? header('HTTP/1.1 401 Unauthorized') : 0; |
3
|
|
|
|
4
|
|
|
class Mail extends Base { |
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Mail form contact site admin |
7
|
|
|
* @param senderName string senderName |
8
|
|
|
* @param senderEmail string senderEmail |
9
|
|
|
* @param senderSubject string senderSubject |
10
|
|
|
* @param senderMessage string senderMessage |
11
|
|
|
* @param email string config Email address |
12
|
|
|
* @param subject string header subject |
13
|
|
|
* @return bool |
14
|
|
|
**/ |
15
|
|
|
public function contactform($senderName, $senderEmail, $senderSubject, $senderMessage) { |
16
|
|
|
$this->debug->append("STA " . __METHOD__, 4); |
17
|
|
View Code Duplication |
if (empty($senderEmail) || !filter_var($senderEmail, FILTER_VALIDATE_EMAIL)) { |
|
|
|
|
18
|
|
|
$this->setErrorMessage($this->getErrorMsg('E0023')); |
19
|
|
|
return false; |
20
|
|
|
} |
21
|
|
|
if (strlen(strip_tags($senderMessage)) < strlen($senderMessage)) { |
22
|
|
|
$this->setErrorMessage($this->getErrorMsg('E0024')); |
23
|
|
|
return false; |
24
|
|
|
} |
25
|
|
|
$aData['senderName'] = $senderName; |
|
|
|
|
26
|
|
|
$aData['senderEmail'] = $senderEmail; |
27
|
|
|
$aData['senderSubject'] = $senderSubject; |
28
|
|
|
$aData['senderMessage'] = $senderMessage; |
29
|
|
|
$aData['email'] = $this->setting->getValue('website_email', '[email protected]'); |
|
|
|
|
30
|
|
|
$aData['subject'] = 'Contact Form'; |
31
|
|
|
if ($this->sendMail('contactform/body', $aData)) { |
32
|
|
|
return true; |
33
|
|
|
} else { |
34
|
|
|
$this->setErrorMessage( 'Unable to send email' ); |
35
|
|
|
return false; |
36
|
|
|
} |
37
|
|
|
return false; |
|
|
|
|
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Send a mail with templating via Smarty and Siftmailer |
42
|
|
|
* @param template string Template name within the mail folder, no extension |
43
|
|
|
* @param aData array Data array with some required fields |
44
|
|
|
* subject : Mail Subject |
45
|
|
|
* email : Destination address |
46
|
|
|
**/ |
47
|
|
|
public function sendMail($template, $aData, $throttle=false) { |
48
|
|
|
// Prepare SMTP transport and mailer |
49
|
|
|
$transport_type = $this->config['swiftmailer']['type']; |
|
|
|
|
50
|
|
|
if ($transport_type == 'sendmail') { |
51
|
|
|
$transport = Swift_SendmailTransport::newInstance($this->config['swiftmailer'][$transport_type]['path'] . ' ' . $this->config['swiftmailer'][$transport_type]['options']); |
52
|
|
|
} else if ($this->config['swiftmailer']['type'] == 'smtp') { |
53
|
|
|
$transport = Swift_SmtpTransport::newInstance($this->config['swiftmailer']['smtp']['host'], $this->config['swiftmailer']['smtp']['port'], $this->config['swiftmailer']['smtp']['encryption']); |
54
|
|
|
if (!empty($this->config['swiftmailer']['smtp']['username']) && !empty($this->config['swiftmailer']['smtp']['password'])) { |
55
|
|
|
$transport->setUsername($this->config['swiftmailer']['smtp']['username']); |
56
|
|
|
$transport->setPassword($this->config['swiftmailer']['smtp']['password']); |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
$mailer = Swift_Mailer::newInstance($transport); |
|
|
|
|
60
|
|
|
|
61
|
|
|
// Throttle mails to x per minute, used for newsletter for example |
62
|
|
|
if ($this->config['swiftmailer']['type'] == 'smtp' && $throttle) { |
63
|
|
|
$mailer->registerPlugin(new Swift_Plugins_ThrottlerPlugin( |
64
|
|
|
$this->config['swiftmailer']['smtp']['throttle'], Swift_Plugins_ThrottlerPlugin::MESSAGES_PER_MINUTE |
65
|
|
|
)); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
// Prepare the smarty templates used |
69
|
|
|
$this->smarty->clearCache(TEMPLATE_DIR . '/mail/' . $template . '.tpl'); |
70
|
|
|
$this->smarty->clearCache(TEMPLATE_DIR . '/mail/subject.tpl'); |
71
|
|
|
$this->smarty->assign('WEBSITENAME', $this->setting->getValue('website_name')); |
72
|
|
|
$this->smarty->assign('SUBJECT', $aData['subject']); |
73
|
|
|
$this->smarty->assign('DATA', $aData); |
74
|
|
|
|
75
|
|
|
// Create new message for Swiftmailer |
76
|
|
|
$senderEmail = $this->setting->getValue('website_email', '[email protected]'); |
77
|
|
|
$senderName = $this->setting->getValue('website_name', '[email protected]'); |
78
|
|
|
$message = Swift_Message::newInstance() |
79
|
|
|
->setSubject($this->smarty->fetch(TEMPLATE_DIR . '/mail/subject.tpl')) |
80
|
|
|
->setFrom(array( $senderEmail => $senderName)) |
81
|
|
|
->setTo($aData['email']) |
82
|
|
|
->setSender($senderEmail) |
83
|
|
|
->setReturnPath($senderEmail) |
84
|
|
|
->setBody($this->smarty->fetch(TEMPLATE_DIR . '/mail/' . $template . '.tpl'), 'text/html'); |
85
|
|
|
if (isset($aData['senderName']) && |
86
|
|
|
isset($aData['senderEmail']) && |
87
|
|
|
strlen($aData['senderName']) > 0 && |
88
|
|
|
strlen($aData['senderEmail']) > 0 && |
89
|
|
|
filter_var($aData['senderEmail'], FILTER_VALIDATE_EMAIL)) |
90
|
|
|
$message->setReplyTo(array($aData['senderEmail'] => $aData['senderName'])); |
91
|
|
|
|
92
|
|
|
// Send message out with configured transport |
93
|
|
|
try { |
94
|
|
|
if ($mailer->send($message)) return true; |
95
|
|
|
} catch (Exception $e) { |
96
|
|
|
$this->setErrorMessage($e->getMessage()); |
97
|
|
|
return false; |
98
|
|
|
} |
99
|
|
|
$this->setErrorMessage($this->sqlError('E0031')); |
100
|
|
|
return false; |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
// Make our class available automatically |
105
|
|
|
$mail = new Mail (); |
106
|
|
|
$mail->setDebug($debug); |
107
|
|
|
$mail->setMysql($mysqli); |
108
|
|
|
$mail->setSmarty($smarty); |
109
|
|
|
$mail->setConfig($config); |
110
|
|
|
$mail->setSetting($setting); |
111
|
|
|
$mail->setErrorCodes($aErrorCodes); |
112
|
|
|
|
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.