1
|
<?php
|
2
|
/* --------------------------------------------------------------
|
3
|
xtc_php_mail.inc.php 2015-07-01 gm
|
4
|
Gambio GmbH
|
5
|
http://www.gambio.de
|
6
|
Copyright (c) 2015 Gambio GmbH
|
7
|
Released under the GNU General Public License (Version 2)
|
8
|
[http://www.gnu.org/licenses/gpl-2.0.html]
|
9
|
--------------------------------------------------------------
|
10
|
|
11
|
|
12
|
based on:
|
13
|
(c) 2003 nextcommerce (xtc_php_mail.inc.php,v 1.17 2003/08/24); www.nextcommerce.org
|
14
|
(c) 2003 XT-Commerce - community made shopping http://www.xt-commerce.com ($Id: xtc_php_mail.inc.php 1129 2005-08-05 11:46:11Z mz $)
|
15
|
|
16
|
|
17
|
Released under the GNU General Public License
|
18
|
---------------------------------------------------------------------------------------*/
|
19
|
|
20
|
// include the mail classes
|
21
|
function xtc_php_mail($from_email_address,
|
22
|
$from_name,
|
23
|
$to_email_address,
|
24
|
$to_name,
|
25
|
$forwarding_to,
|
26
|
$reply_email_address,
|
27
|
$reply_name,
|
28
|
$path_to_attachment,
|
29
|
$path_to_more_attachments,
|
30
|
$email_subject,
|
31
|
$message_body_html,
|
32
|
$message_body_plain)
|
33
|
{
|
34
|
// Get email service.
|
35
|
$emailService = StaticGXCoreLoader::getService('Email');
|
36
|
|
37
|
// Sender
|
38
|
$from_email_address = trim($from_email_address);
|
39
|
if(empty($from_email_address))
|
40
|
{
|
41
|
$from_email_address = trim(EMAIL_FROM);
|
42
|
}
|
43
|
|
44
|
if(empty($from_name))
|
45
|
{
|
46
|
$from_name = STORE_OWNER;
|
47
|
}
|
48
|
|
49
|
$sender = MainFactory::create('EmailContact', MainFactory::create('EmailAddress', $from_email_address),
|
50
|
MainFactory::create('ContactType', ContactType::SENDER),
|
51
|
MainFactory::create('ContactName', $from_name));
|
52
|
|
53
|
// Recipient
|
54
|
$recipient = MainFactory::create('EmailContact',
|
55
|
MainFactory::create('EmailAddress', trim((string)$to_email_address)),
|
56
|
MainFactory::create('ContactType', ContactType::RECIPIENT),
|
57
|
MainFactory::create('ContactName', (string)$to_name));
|
58
|
|
59
|
// Subject & Content
|
60
|
$subject = MainFactory::create('EmailSubject', (string)$email_subject);
|
61
|
|
62
|
$content = (EMAIL_USE_HTML == 'true') ? MainFactory::create('EmailContent',
|
63
|
(string)$message_body_html) : MainFactory::create('EmailContent',
|
64
|
nl2br((string)$message_body_plain));
|
65
|
|
66
|
// Create Email Object
|
67
|
$email = $emailService->create($sender, $recipient, $subject, $content);
|
68
|
|
69
|
// Content Plain
|
70
|
$email->setContentPlain(MainFactory::create('EmailContent', (string)$message_body_plain));
|
71
|
|
72
|
// Reply To
|
73
|
$reply_email_address = trim($reply_email_address);
|
74
|
if(!empty($reply_email_address))
|
75
|
{
|
76
|
$replyTo = MainFactory::create('EmailContact', MainFactory::create('EmailAddress', $reply_email_address),
|
77
|
MainFactory::create('ContactType', ContactType::REPLY_TO),
|
78
|
MainFactory::create('ContactName', (string)$reply_name));
|
79
|
$email->setReplyTo($replyTo);
|
80
|
}
|
81
|
|
82
|
// BCC Contacts
|
83
|
if(!empty($forwarding_to))
|
84
|
{
|
85
|
$email->setBcc(MainFactory::create('ContactCollection'));
|
86
|
$bccAddressesArray = explode(',', $forwarding_to);
|
87
|
foreach($bccAddressesArray AS $emailAddress)
|
88
|
{
|
89
|
$bccContact = MainFactory::create('EmailContact', MainFactory::create('EmailAddress', trim($emailAddress)),
|
90
|
MainFactory::create('ContactType', ContactType::BCC),
|
91
|
MainFactory::create('ContactName', ''));
|
92
|
$email->getBcc()->add($bccContact);
|
93
|
}
|
94
|
}
|
95
|
|
96
|
// Attachments
|
97
|
// EmailService does not currently use a display name for the attachments.
|
98
|
$attachments = MainFactory::create('AttachmentCollection');
|
99
|
if(!empty($path_to_attachment))
|
100
|
{
|
101
|
if(is_array($path_to_attachment) && empty($path_to_attachment) == false)
|
102
|
{
|
103
|
foreach($path_to_attachment as $file)
|
104
|
{
|
105
|
if(is_string($file))
|
106
|
{
|
107
|
$path = $file;
|
108
|
$name = '';
|
109
|
}
|
110
|
else
|
111
|
{
|
112
|
$path = $file['path'];
|
113
|
if(isset($file['name']))
|
114
|
{
|
115
|
$name = $file['name'];
|
116
|
}
|
117
|
}
|
118
|
|
119
|
// It is possible that some sections of the app will send an invalid attachment path.
|
120
|
if(file_exists((string)$path))
|
121
|
{
|
122
|
$attachmentPath = MainFactory::create('AttachmentPath', (string)$path);
|
123
|
$attachmentName = MainFactory::create('AttachmentName', (string)$name);
|
124
|
$attachments->add(MainFactory::create('EmailAttachment', $attachmentPath, $attachmentName));
|
125
|
}
|
126
|
else
|
127
|
{
|
128
|
// create a new error log - attachment file is empty
|
129
|
$log = LogControl::get_instance();
|
130
|
$log->notice('Email attachment file does not exist in the server: ' . (string)$path);
|
131
|
}
|
132
|
}
|
133
|
}
|
134
|
else
|
135
|
{
|
136
|
if(is_string($path_to_attachment))
|
137
|
{
|
138
|
$attachmentPath = MainFactory::create('AttachmentPath', $path_to_attachment);
|
139
|
$attachments->add(MainFactory::create('EmailAttachment', $attachmentPath));
|
140
|
}
|
141
|
}
|
142
|
}
|
143
|
|
144
|
if(!empty($path_to_more_attachments))
|
145
|
{
|
146
|
$attachmentsArray = preg_split('/[;,]/', $path_to_more_attachments);
|
147
|
foreach($attachmentsArray as $path)
|
148
|
{
|
149
|
$path = trim($path);
|
150
|
|
151
|
if($path !== '')
|
152
|
{
|
153
|
$attachmentPath = MainFactory::create('AttachmentPath', $path);
|
154
|
$attachments->add(MainFactory::create('EmailAttachment', $attachmentPath));
|
155
|
}
|
156
|
}
|
157
|
}
|
158
|
|
159
|
$email->setAttachments($attachments);
|
160
|
|
161
|
// Stores and sends the email object.
|
162
|
$emailService->send($email);
|
163
|
|
164
|
// Use the AttachmentsHandler class to store the files into the "uploads/attachments" directory.
|
165
|
$emailCollection = MainFactory::create('EmailCollection', $email);
|
166
|
$attachmentsHandler = MainFactory::create('AttachmentsHandler');
|
167
|
$attachmentsHandler->processCollection($emailCollection);
|
168
|
$emailService->writeCollection($emailCollection); // Save the new mail attachment paths into the database.
|
169
|
|
170
|
// Compatibility value, sections of the app still check for result.
|
171
|
return true;
|
172
|
}
|