-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCalendarEmail.php
369 lines (329 loc) · 9.33 KB
/
CalendarEmail.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
<?php
// Import PHPMailer classes into the global namespace
// These must be at the top of your script, not inside a function
use PHPMailer\PHPMailer\PHPMailer;
use Kigkonsult\Icalcreator\Vcalendar;
// Load Composer's autoloader
require 'vendor/autoload.php';
/**
* Class CalendarEmail
* @property string $headers
* @property string $calendarBody
* @property string $calendarOrganizer
* @property string $calendarOrganizerEmail
* @property string $calendarLocation
* @property string $calendarDate
* @property string $calendarStartTime
* @property string $calendarEndTime
* @property string $calendarSubject
* @property string $calendarDescription
* @property array $calendarParticipants
* @property string $urlString
* @property PHPMailer $mail
*
*/
class CalendarEmail extends Message
{
private $headers;
private $calendarBody;
private $calendarOrganizer;
private $calendarOrganizerEmail;
private $calendarParticipants = array();
private $calendarLocation;
private $calendarDate;
private $calendarStartTime;
private $calendarEndTime;
private $calendarSubject;
private $calendarDescription;
private $urlString;
private $mail;
/**
* @return PHPMailer
*/
public function getMail()
{
return $this->mail;
}
/**
* @param PHPMailer $mail
*/
public function setMail($mail)
{
$this->mail = $mail;
}
/**
* @return string
*/
public function getUrlString()
{
return $this->urlString;
}
/**
* @param string $urlString
*/
public function setUrlString($urlString)
{
$this->urlString = $urlString;
}
/**
* @return string
*/
public function getHeaders()
{
return $this->headers;
}
/**
* @param string $headers
*/
public function setHeaders($headers)
{
$this->headers = $headers;
}
/**
* @return string
*/
public function getCalendarBody()
{
return $this->calendarBody;
}
/**
* @param string $calendarBody
*/
public function setCalendarBody($calendarBody)
{
$this->calendarBody = $calendarBody;
}
/**
* @return string
*/
public function getCalendarOrganizer()
{
return $this->calendarOrganizer;
}
/**
* @param string $calendarOrganizer
*/
public function setCalendarOrganizer($calendarOrganizer)
{
$this->calendarOrganizer = $calendarOrganizer;
}
/**
* @return string
*/
public function getCalendarOrganizerEmail()
{
return $this->calendarOrganizerEmail;
}
/**
* @param string $calendarOrganizerEmail
*/
public function setCalendarOrganizerEmail($calendarOrganizerEmail)
{
$this->calendarOrganizerEmail = $calendarOrganizerEmail;
}
/**
* @return string
*/
public function getCalendarLocation()
{
return $this->calendarLocation;
}
/**
* @param string $calendarLocation
*/
public function setCalendarLocation($calendarLocation)
{
$this->calendarLocation = $calendarLocation;
}
/**
* @return string
*/
public function getCalendarDate()
{
return $this->calendarDate;
}
/**
* @param string $calendarDate
*/
public function setCalendarDate($calendarDate)
{
$this->calendarDate = $calendarDate;
}
/**
* @return string
*/
public function getCalendarStartTime()
{
return $this->calendarStartTime;
}
/**
* @param string $calendarStartTime
*/
public function setCalendarStartTime($calendarStartTime)
{
$this->calendarStartTime = $calendarStartTime;
}
/**
* @return string
*/
public function getCalendarEndTime()
{
return $this->calendarEndTime;
}
/**
* @param string $calendarEndTime
*/
public function setCalendarEndTime($calendarEndTime)
{
$this->calendarEndTime = $calendarEndTime;
}
/**
* @return string
*/
public function getCalendarSubject()
{
return $this->calendarSubject;
}
/**
* @param string $calendarSubject
*/
public function setCalendarSubject($calendarSubject)
{
$this->calendarSubject = $calendarSubject;
}
/**
* @return string
*/
public function getCalendarDescription()
{
return $this->calendarDescription;
}
/**
* @param string $calendarDescription
*/
public function setCalendarDescription($calendarDescription)
{
$this->calendarDescription = $calendarDescription;
}
/**
* @return array
*/
public function getCalendarParticipants()
{
return $this->calendarParticipants;
}
/**
* @param array $calendarParticipants
*/
public function setCalendarParticipants($calendarParticipants)
{
foreach ($calendarParticipants as $name => $participant){
$this->calendarParticipants[$name] = $participant;
}
}
/**
* @param array $param
*/
public function prepareCalendarData($param){
$this->setCalendarOrganizerEmail($param['calendarOrganizerEmail']);
$this->setCalendarOrganizer($param['calendarOrganizer']);
$this->setCalendarSubject($param['calendarSubject']);
$this->setCalendarDescription($param['calendarDescription']);
$this->setcalendarLocation($param['calendarLocation']);
$this->setCalendarDate($param['calendarDate']);
$this->setCalendarStartTime($param['calendarStartTime']);
$this->setCalendarEndTime($param['calendarEndTime']);
$this->setCalendarParticipants($param['calendarParticipants']);
}
/**
* send calendar event email
* @param array $param
* @return bool
*/
public function sendCalendarEmail($param){
try {
$this->prepareCalendarData($param);
$vcalendar = Vcalendar::factory([Vcalendar::UNIQUE_ID => (string)(mt_rand()),])
// with calendaring info
->setMethod(Vcalendar::REQUEST)
->setXprop(
Vcalendar::X_WR_CALNAME,
$this->getSubject()
)
->setXprop(
Vcalendar::X_WR_CALDESC,
$this->getSubject()
)
->setXprop(
Vcalendar::X_WR_RELCALID,
"3E26604A-50F4-4449-8B3E-E4F4932D05B5"
)
->setXprop(
Vcalendar::X_WR_TIMEZONE,
"America/Los_Angeles"
);
$event1 = $vcalendar->newVevent()
->setTransp(Vcalendar::OPAQUE)
->setClass(Vcalendar::P_BLIC)
->setSequence(1)
// describe the event
->setSummary($this->getSubject())
->setDescription(
$this->getCalendarDescription(),
[
Vcalendar::ALTREP =>
'CID:<[email protected]>'
]
)
//->setComment( 'It\'s going to be fun..' )
// place the event
//->setLocation( 'Kafé Ekorren Stockholm' )
//->setGeo( '59.32206', '18.12485' )
// set the time
->setDtstart(
new DateTime(
$this->getCalendarDate() . "T" . $this->getCalendarStartTime(),
new DateTimezone('America/Los_Angeles')
)
)
->setDtend(
new DateTime(
$this->getCalendarDate() . "T" . $this->getCalendarEndTime(),
new DateTimezone("America/Los_Angeles")
)
)
// organizer, chair and some participants
->setOrganizer(
$this->getCalendarOrganizerEmail(),
[Vcalendar::CN => $this->getCalendarOrganizer()]
);
//set event participants
foreach ($this->getCalendarParticipants() as $name => $e) {
$event1->setAttendee(
$e,
[
Vcalendar::ROLE => Vcalendar::REQ_PARTICIPANT,
Vcalendar::PARTSTAT => Vcalendar::NEEDS_ACTION,
Vcalendar::RSVP => Vcalendar::TRUE,
Vcalendar::CN => $name,
]
);
}
//generate event string.
$vcalendarString =
// apply appropriate Vtimezone with Standard/DayLight components
$vcalendar->vtimezonePopulate()
// and create the (string) calendar
->createCalendar();
$tempFile = sys_get_temp_dir() . "/invite.ics";
$fileHandle = fopen($tempFile, "w");
fwrite($fileHandle, $vcalendarString);
$this->setAttachment($tempFile, 'invite.ics');
fclose($fileHandle);
$result = $this->send();
} catch (\LogicException $e) {
} catch (\Exception $e) {
echo $e->getMessage();
}
}
}