-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathphpMailMerge.send.php
373 lines (350 loc) · 15.8 KB
/
phpMailMerge.send.php
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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
<?php
###############################################################################
#
# phpMailMerge v1.0.5 | (c) 2018 Ralf Bartsch (@r84r)
#
# license & info: https://github.com/r84r/phpMailMerge
#
###############################################################################
#
# Sends a serial letter in multipart MIME format to all tagged entries of a
# CSV database. Depending on the language, a German and English version will
# be generated. In addition to the HTML message, the email also contains an
# alternative plain text message. If attachments are defined, they will be
# added.
#
###############################################################################
#
# phpMailMerge.send.php | execution script
#
# revisions:
# 2018-09-04, Ralf Bartsch
# - mod: prepared for GitHub: comments and script messages set in english
# 2018-06-13, Ralf Bartsch
# - new: added default language (if language selector doesn't match)
# - new: use company name as email name if first name is empty
# 2017-05-04, Ralf Bartsch
# - new: added additional language selectors "de" and "en"
# 2017-02-08, Ralf Bartsch
# - new: added sleep() and flush() at the end of the loop
# 2016-12-20, Ralf Bartsch
# - new: added inline images
# - mod: renamed some variables
# - fix: correct encoding
#
# initial release:
# 2016-10-10, Ralf Bartsch
# - for the GKT symposium (www.gleitketten.de)
#
###############################################################################
error_reporting(E_ERROR | E_WARNING | E_PARSE | E_NOTICE);
# basic setup and functions
$eol = "\r\n";
function encodeString($string) {
$mimePrefs = array ("scheme" => "Q",
"input-charset" => "utf-8",
"output-charset" => "utf-8",
"line-length" => 76,
"line-break-chars" => $eol);
return preg_replace("/^:\s+/", "", iconv_mime_encode("", $string, $mimePrefs));
}
# generate seperators for the different parts of the email
$mimeBoundary = "-----=" . md5(uniqid(mt_rand(), 1));
$mimeBoundary_alt = "-----=alt" . md5(uniqid(mt_rand(), 1));
$mimeBoundary_rel = "-----=rel" . md5(uniqid(mt_rand(), 1));
# load setup (abort script if an error occur)
require("phpMailMerge.setup.php");
$message_de = file_get_contents($message_de);
$message_en = file_get_contents($message_en);
if ($message_de === false or $message_en === FALSE) {
exit("At least one HTML message can not be found.");
}
# sender
$from = $fromMail;
if ($fromMail !== "") {
if ($fromName !== "") {
$from = encodeString($fromName)." <".$fromMail.">";
}
}
# reply
$replyTo = $replyMail;
if ($replyMail !== "") {
if ($replyName !== "") {
$replyTo = encodeString($replyName)." <".$replyMail.">";
}
}
if ($replyTo == "") {
$replyTo = $from;
$replyName = $fromName;
$replyMail = $fromMail;
}
# import german attachments
if ($attachments_de !== "") {
$files = explode(";", $attachments_de);
$attachments_de = array();
foreach ($files as $file) {
if (file_exists($file) === FALSE) { exit("Attachment \"".basename($file)."\" not found."); }
$name = basename($file);
$size = filesize($file);
$data = chunk_split(base64_encode(file_get_contents($file)));
$type = mime_content_type($file);
$attachments_de[] = array("name"=>$name, "size"=>$size, "type"=>$type, "data"=>$data);
}
}
# import english attachments
if ($attachments_en !== "") {
$files = explode(";", $attachments_en);
$attachments_en = array();
foreach ($files as $file) {
if (file_exists($file) === FALSE) { exit("Attachment \"".basename($file)."\" not found."); }
$name = basename($file);
$size = filesize($file);
$data = chunk_split(base64_encode(file_get_contents($file)));
$type = mime_content_type($file);
$attachments_en[] = array("name"=>$name, "size"=>$size, "type"=>$type, "data"=>$data);
}
}
# import german images
if ($inlineImages_de !== "") {
$files = explode(";", $inlineImages_de);
$images_de = array();
foreach ($files as $file) {
if (file_exists($file) === FALSE) { exit("Image \"".basename($file)."\" not found."); }
$name = basename($file);
$size = filesize($file);
$data = chunk_split(base64_encode(file_get_contents($file)));
$type = mime_content_type($file);
$images_de[] = array("name"=>$name, "size"=>$size, "type"=>$type, "data"=>$data);
}
}
# import english images
if ($inlineImages_en !== "") {
$files = explode(";", $inlineImages_en);
$images_en = array();
foreach ($files as $file) {
if (file_exists($file) === FALSE) { exit("Image \"".basename($file)."\" not found."); }
$name = basename($file);
$size = filesize($file);
$data = chunk_split(base64_encode(file_get_contents($file)));
$type = mime_content_type($file);
$images_en[] = array("name"=>$name, "size"=>$size, "type"=>$type, "data"=>$data);
}
}
# create plain text version
# 1) remove line breaks
# 2) delete complete <head>
# 3) convert HTML breaks to line breaks
# 4) remove all HTML tags
# 5) convert HTML special chars to normal chars
# 6) replace special chars
# german
$message_plain_de = preg_replace("/\r|\n/", "", $message_de); #1
$message_plain_de = preg_replace("%<head>.*</head>%", "", $message_plain_de); #2
$message_plain_de = preg_replace("/\<br(\s*)?\/?\>/i", $eol, $message_plain_de); #3
$message_plain_de = strip_tags($message_plain_de); #4
$message_plain_de = htmlspecialchars_decode($message_plain_de); #5
$message_plain_de = str_replace("•", "-", $message_plain_de); #6
# english
$message_plain_en = preg_replace("/\r|\n/", "", $message_en); #1
$message_plain_en = preg_replace("%<head>.*</head>%", "", $message_plain_en); #2
$message_plain_en = preg_replace("/\<br(\s*)?\/?\>/i", $eol, $message_plain_en); #3
$message_plain_en = strip_tags($message_plain_en); #4
$message_plain_en = htmlspecialchars_decode($message_plain_en); #5
$message_plain_en = str_replace("•", "-", $message_plain_en); #6
# create HTML version
$message_html_de = $message_de;
$message_html_en = $message_en;
# open log file for append new log ########################################
$log = fopen($logFile, "a");
fputs($log, $eol);
fputs($log, date("Y-m-d H:i:s").$eol);
###########################################################################
# open datenbase, run through and send an email to each entry #############
###########################################################################
$row = 0;
if (($handle = fopen($databaseFile, "r")) !== FALSE) {
while (($data = fgetcsv($handle, $maxReadLength, $delimiter)) !== FALSE) {
$row++;
# insert element at beginning (to beginn with counter 1)
array_splice($data, 0, 0, "");
# identify language and pass proper blocks
# if necessary don't consider email in mailing letter
$language = strtolower($data[$databaseColumnLanguage]);
if ($defaultLanguage != "") {
if ((($language == "d") or ($language == "de") or
($language == "e") or ($language == "en")) == false) {
$language = $defaultLanguage;
}
}
if (($language == "d") or ($language == "de")) {
$subject = $subject_de;
$message_plain = $message_plain_de;
$message_html = $message_html_de;
$attachments = $attachments_de;
$images = $images_de;
} elseif (($language == "e") or ($language == "en")) {
$subject = $subject_en;
$message_plain = $message_plain_en;
$message_html = $message_html_en;
$attachments = $attachments_en;
$images = $images_en;
} else {
continue;
}
# generate salutation
$toMail = $data[$databaseColumnEmail];
$salutation = $data[$databaseColumnTitle];
$company = $data[$databaseColumnCompany];
if ($data[$databaseColumnName] == "") {
$toName = $company;
} else {
if ($databaseColumnFirstName == 0) {
$toName = $company;
} else {
$toName = $data[$databaseColumnFirstName]." ".$data[$databaseColumnName];
}
$salutation .= ($data[$databaseColumnAcTitle] !== "") ? (" ".$data[$databaseColumnAcTitle]) : ("");
$salutation .= " ".$data[$databaseColumnName];
}
# replace placeholders
$message_plain = str_replace("%SUBJECT%", $subject, $message_plain);
$message_plain = str_replace("%SALUTATION%", $salutation, $message_plain);
$message_html = str_replace("%SUBJECT%", $subject, $message_html);
$message_html = str_replace("%SALUTATION%", $salutation, $message_html);
# generate recipient
$to = $toMail;
if ($toName !== "") {
$to = encodeString($toName)." <".$toMail.">";
}
###################################################################
#
# multipart/mixed
# +- multipart/alternative
# | +- text/plain
# | +- multipart/relative
# | +- text/html
# | +- image/jpeg (inline)
# +- image/jpeg (attached)
#
# > without inline elements "multipart/relative" gets "text/html"
# > without attached images the structure begins with
# "multipart/alternative"
#
###################################################################
# create multipart/related or rather text/html
$html = array();
if (!is_array($images)) {
$html[] = "Content-Type: text/html; charset=utf-8";
$html[] = "Content-Transfer-Encoding: 7bit";
$html[] = "";
$html[] = $message_html;
} else {
$html[] = "Content-Type: multipart/related;";
$html[] = "\tboundary=\"$mimeBoundary_rel\"";
$html[] = "";
$html[] = "--$mimeBoundary_rel";
$html[] = "Content-Type: text/html; charset=utf-8";
$html[] = "Content-Transfer-Encoding: 7bit";
$html[] = "";
$html[] = $message_html;
$html[] = "";
$count = 0;
foreach($images as $image) {
$count++;
$html[] = "--$mimeBoundary_rel";
$html[] = "Content-Type: ".$image['type'].";";
$html[] = "\tname=\"".$image['name']."\";";
$html[] = "Content-Disposition: inline;";
$html[] = "\tfilename=\"".$image['name']."\";";
$html[] = "Content-Transfer-Encoding: base64";
$html[] = "Content-ID: <img".$count.">";
$html[] = "";
$html[] = $image['data'];
}
$html[] = "--$mimeBoundary_rel--";
}
###################################################################
# create header ###################################################
$headers = array();
$headers[] = "From: $from";
$headers[] = "Reply-To: $replyTo";
$headers[] = "X-Mailer: PHP/".phpversion();
$headers[] = "MIME-Version: 1.0";
if (is_array($attachments)) {
$headers[] = "Content-type: multipart/mixed;";
} else {
$headers[] = "Content-type: multipart/alternative;";
# $mimeBoundary_alt überschreiben/gleichsetzen
$mimeBoundary_alt = $mimeBoundary;
}
$headers[] = "\tboundary=\"".$mimeBoundary."\"";
###################################################################
# create body/message #############################################
$body = array();
$body[] = "This is a multi-part message in MIME format.";
$body[] = "";
if (is_array($attachments)) {
$body[] = "--$mimeBoundary";
$body[] = "Content-Type: multipart/alternative;";
$body[] = "\tboundary=\"$mimeBoundary_alt\"";
$body[] = "";
}
$body[] = "--$mimeBoundary_alt";
$body[] = "Content-Type: text/plain; charset=utf-8";
$body[] = "Content-Transfer-Encoding: 7bit";
$body[] = "";
$body[] = $message_plain;
$body[] = "";
$body[] = "--$mimeBoundary_alt";
$body = array_merge(array_values($body), array_values($html));
$body[] = "";
$body[] = "--$mimeBoundary_alt--";
if (is_array($attachments)) {
foreach($attachments as $attachment) {
$body[] = "";
$body[] = "--$mimeBoundary";
$body[] = "Content-Disposition: attachment;";
$body[] = "\tfilename=\"".$attachment['name']."\";";
$body[] = "Content-Length: .".$attachment['size'].";";
$body[] = "Content-Type: ".$attachment['type'].";";
$body[] = "\tname=\"".$attachment['name']."\"";
$body[] = "Content-Transfer-Encoding: base64";
$body[] = "";
$body[] = $attachment['data'];
}
$body[] = "--$mimeBoundary--";
}
###################################################################
# send email and log it in browser and file #######################
if ($data[$databaseColumnName] == "") {
$info = "Company " . $company . "\t" . $toMail;
} else {
if ($company == "") {
$info = $toName . "\t" . $toMail;
} else {
$info = $toName . " (" . $company . ")\t" . $toMail;
}
}
if (mail($to,
encodeString($subject),
implode($eol, $body),
implode($eol, $headers),
"-f $fromMail")) {
$report = ($row-1) . "\tSent:\t" .$info;
} else {
$report = ($row-1) . "\tFail:\t" .$info;
}
echo $report."<br>".$eol;
fputs($log, $report.$eol);
# clear output puffer (for instant browser output)
flush();
ob_flush();
# wait a short time (can be removed if mailer is fast)
sleep(0.1);
}
# close datenbase
fclose($handle);
}
# close log file
fclose($log);
?>