Subversion-Projekte lars-tiefland.php_share

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<?php
2
 
3
/*
4
 * This file is part of SwiftMailer.
5
 * (c) 2004-2009 Chris Corbyn
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
 
11
//@require 'Swift/Mime/Message.php';
12
//@require 'Swift/Mime/MimePart.php';
13
//@require 'Swift/Mime/MimeEntity.php';
14
//@require 'Swift/Mime/HeaderSet.php';
15
//@require 'Swift/Mime/ContentEncoder.php';
16
 
17
/**
18
 * The default email message class.
19
 * @package Swift
20
 * @subpackage Mime
21
 * @author Chris Corbyn
22
 */
23
class Swift_Mime_SimpleMessage extends Swift_Mime_MimePart
24
  implements Swift_Mime_Message
25
{
26
 
27
  /**
28
   * Create a new SimpleMessage with $headers, $encoder and $cache.
29
   * @param Swift_Mime_HeaderSet $headers
30
   * @param Swift_Mime_ContentEncoder $encoder
31
   * @param Swift_KeyCache $cache
32
   * @param string $charset
33
   */
34
  public function __construct(Swift_Mime_HeaderSet $headers,
35
    Swift_Mime_ContentEncoder $encoder, Swift_KeyCache $cache, $charset = null)
36
  {
37
    parent::__construct($headers, $encoder, $cache, $charset);
38
    $this->getHeaders()->defineOrdering(array(
39
      'Return-Path',
40
      'Sender',
41
      'Message-ID',
42
      'Date',
43
      'Subject',
44
      'From',
45
      'Reply-To',
46
      'To',
47
      'Cc',
48
      'Bcc',
49
      'MIME-Version',
50
      'Content-Type',
51
      'Content-Transfer-Encoding'
52
      ));
53
    $this->getHeaders()->setAlwaysDisplayed(
54
      array('Date', 'Message-ID', 'From')
55
      );
56
    $this->getHeaders()->addTextHeader('MIME-Version', '1.0');
57
    $this->setDate(time());
58
    $this->setId($this->getId());
59
    $this->getHeaders()->addMailboxHeader('From');
60
  }
61
 
62
  /**
63
   * Always returns {@link LEVEL_TOP} for a message instance.
64
   * @return int
65
   */
66
  public function getNestingLevel()
67
  {
68
    return self::LEVEL_TOP;
69
  }
70
 
71
  /**
72
   * Set the subject of this message.
73
   * @param string $subject
74
   */
75
  public function setSubject($subject)
76
  {
77
    if (!$this->_setHeaderFieldModel('Subject', $subject))
78
    {
79
      $this->getHeaders()->addTextHeader('Subject', $subject);
80
    }
81
    return $this;
82
  }
83
 
84
  /**
85
   * Get the subject of this message.
86
   * @return string
87
   */
88
  public function getSubject()
89
  {
90
    return $this->_getHeaderFieldModel('Subject');
91
  }
92
 
93
  /**
94
   * Set the date at which this message was created.
95
   * @param int $date
96
   */
97
  public function setDate($date)
98
  {
99
    if (!$this->_setHeaderFieldModel('Date', $date))
100
    {
101
      $this->getHeaders()->addDateHeader('Date', $date);
102
    }
103
    return $this;
104
  }
105
 
106
  /**
107
   * Get the date at which this message was created.
108
   * @return int
109
   */
110
  public function getDate()
111
  {
112
    return $this->_getHeaderFieldModel('Date');
113
  }
114
 
115
  /**
116
   * Set the return-path (the bounce address) of this message.
117
   * @param string $address
118
   */
119
  public function setReturnPath($address)
120
  {
121
    if (!$this->_setHeaderFieldModel('Return-Path', $address))
122
    {
123
      $this->getHeaders()->addPathHeader('Return-Path', $address);
124
    }
125
    return $this;
126
  }
127
 
128
  /**
129
   * Get the return-path (bounce address) of this message.
130
   * @return string
131
   */
132
  public function getReturnPath()
133
  {
134
    return $this->_getHeaderFieldModel('Return-Path');
135
  }
136
 
137
  /**
138
   * Set the sender of this message.
139
   * This does not override the From field, but it has a higher significance.
140
   * @param string $sender
141
   * @param string $name optional
142
   */
143
  public function setSender($address, $name = null)
144
  {
145
    if (!is_array($address) && isset($name))
146
    {
147
      $address = array($address => $name);
148
    }
149
 
150
    if (!$this->_setHeaderFieldModel('Sender', (array) $address))
151
    {
152
      $this->getHeaders()->addMailboxHeader('Sender', (array) $address);
153
    }
154
    return $this;
155
  }
156
 
157
  /**
158
   * Get the sender of this message.
159
   * @return string
160
   */
161
  public function getSender()
162
  {
163
    return $this->_getHeaderFieldModel('Sender');
164
  }
165
 
166
  /**
167
   * Add a From: address to this message.
168
   *
169
   * If $name is passed this name will be associated with the address.
170
   *
171
   * @param string $address
172
   * @param string $name optional
173
   */
174
  public function addFrom($address, $name = null)
175
  {
176
    $current = $this->getFrom();
177
    $current[$address] = $name;
178
    return $this->setFrom($current);
179
  }
180
 
181
  /**
182
   * Set the from address of this message.
183
   *
184
   * You may pass an array of addresses if this message is from multiple people.
185
   *
186
   * If $name is passed and the first parameter is a string, this name will be
187
   * associated with the address.
188
   *
189
   * @param string $addresses
190
   * @param string $name optional
191
   */
192
  public function setFrom($addresses, $name = null)
193
  {
194
    if (!is_array($addresses) && isset($name))
195
    {
196
      $addresses = array($addresses => $name);
197
    }
198
 
199
    if (!$this->_setHeaderFieldModel('From', (array) $addresses))
200
    {
201
      $this->getHeaders()->addMailboxHeader('From', (array) $addresses);
202
    }
203
    return $this;
204
  }
205
 
206
  /**
207
   * Get the from address of this message.
208
   *
209
   * @return string
210
   */
211
  public function getFrom()
212
  {
213
    return $this->_getHeaderFieldModel('From');
214
  }
215
 
216
  /**
217
   * Add a Reply-To: address to this message.
218
   *
219
   * If $name is passed this name will be associated with the address.
220
   *
221
   * @param string $address
222
   * @param string $name optional
223
   */
224
  public function addReplyTo($address, $name = null)
225
  {
226
    $current = $this->getReplyTo();
227
    $current[$address] = $name;
228
    return $this->setReplyTo($current);
229
  }
230
 
231
  /**
232
   * Set the reply-to address of this message.
233
   *
234
   * You may pass an array of addresses if replies will go to multiple people.
235
   *
236
   * If $name is passed and the first parameter is a string, this name will be
237
   * associated with the address.
238
   *
239
   * @param string $addresses
240
   * @param string $name optional
241
   */
242
  public function setReplyTo($addresses, $name = null)
243
  {
244
    if (!is_array($addresses) && isset($name))
245
    {
246
      $addresses = array($addresses => $name);
247
    }
248
 
249
    if (!$this->_setHeaderFieldModel('Reply-To', (array) $addresses))
250
    {
251
      $this->getHeaders()->addMailboxHeader('Reply-To', (array) $addresses);
252
    }
253
    return $this;
254
  }
255
 
256
  /**
257
   * Get the reply-to address of this message.
258
   *
259
   * @return string
260
   */
261
  public function getReplyTo()
262
  {
263
    return $this->_getHeaderFieldModel('Reply-To');
264
  }
265
 
266
  /**
267
   * Add a To: address to this message.
268
   *
269
   * If $name is passed this name will be associated with the address.
270
   *
271
   * @param string $address
272
   * @param string $name optional
273
   */
274
  public function addTo($address, $name = null)
275
  {
276
    $current = $this->getTo();
277
    $current[$address] = $name;
278
    return $this->setTo($current);
279
  }
280
 
281
  /**
282
   * Set the to addresses of this message.
283
   *
284
   * If multiple recipients will receive the message and array should be used.
285
   *
286
   * If $name is passed and the first parameter is a string, this name will be
287
   * associated with the address.
288
   *
289
   * @param array $addresses
290
   * @param string $name optional
291
   */
292
  public function setTo($addresses, $name = null)
293
  {
294
    if (!is_array($addresses) && isset($name))
295
    {
296
      $addresses = array($addresses => $name);
297
    }
298
 
299
    if (!$this->_setHeaderFieldModel('To', (array) $addresses))
300
    {
301
      $this->getHeaders()->addMailboxHeader('To', (array) $addresses);
302
    }
303
    return $this;
304
  }
305
 
306
  /**
307
   * Get the To addresses of this message.
308
   *
309
   * @return array
310
   */
311
  public function getTo()
312
  {
313
    return $this->_getHeaderFieldModel('To');
314
  }
315
 
316
  /**
317
   * Add a Cc: address to this message.
318
   *
319
   * If $name is passed this name will be associated with the address.
320
   *
321
   * @param string $address
322
   * @param string $name optional
323
   */
324
  public function addCc($address, $name = null)
325
  {
326
    $current = $this->getCc();
327
    $current[$address] = $name;
328
    return $this->setCc($current);
329
  }
330
 
331
  /**
332
   * Set the Cc addresses of this message.
333
   *
334
   * If $name is passed and the first parameter is a string, this name will be
335
   * associated with the address.
336
   *
337
   * @param array $addresses
338
   * @param string $name optional
339
   */
340
  public function setCc($addresses, $name = null)
341
  {
342
    if (!is_array($addresses) && isset($name))
343
    {
344
      $addresses = array($addresses => $name);
345
    }
346
 
347
    if (!$this->_setHeaderFieldModel('Cc', (array) $addresses))
348
    {
349
      $this->getHeaders()->addMailboxHeader('Cc', (array) $addresses);
350
    }
351
    return $this;
352
  }
353
 
354
  /**
355
   * Get the Cc address of this message.
356
   *
357
   * @return array
358
   */
359
  public function getCc()
360
  {
361
    return $this->_getHeaderFieldModel('Cc');
362
  }
363
 
364
  /**
365
   * Add a Bcc: address to this message.
366
   *
367
   * If $name is passed this name will be associated with the address.
368
   *
369
   * @param string $address
370
   * @param string $name optional
371
   */
372
  public function addBcc($address, $name = null)
373
  {
374
    $current = $this->getBcc();
375
    $current[$address] = $name;
376
    return $this->setBcc($current);
377
  }
378
 
379
  /**
380
   * Set the Bcc addresses of this message.
381
   *
382
   * If $name is passed and the first parameter is a string, this name will be
383
   * associated with the address.
384
   *
385
   * @param array $addresses
386
   * @param string $name optional
387
   */
388
  public function setBcc($addresses, $name = null)
389
  {
390
    if (!is_array($addresses) && isset($name))
391
    {
392
      $addresses = array($addresses => $name);
393
    }
394
 
395
    if (!$this->_setHeaderFieldModel('Bcc', (array) $addresses))
396
    {
397
      $this->getHeaders()->addMailboxHeader('Bcc', (array) $addresses);
398
    }
399
    return $this;
400
  }
401
 
402
  /**
403
   * Get the Bcc addresses of this message.
404
   *
405
   * @return array
406
   */
407
  public function getBcc()
408
  {
409
    return $this->_getHeaderFieldModel('Bcc');
410
  }
411
 
412
  /**
413
   * Set the priority of this message.
414
   * The value is an integer where 1 is the highest priority and 5 is the lowest.
415
   * @param int $priority
416
   */
417
  public function setPriority($priority)
418
  {
419
    $priorityMap = array(
420
      1 => 'Highest',
421
      2 => 'High',
422
      3 => 'Normal',
423
      4 => 'Low',
424
      5 => 'Lowest'
425
      );
426
    $pMapKeys = array_keys($priorityMap);
427
    if ($priority > max($pMapKeys))
428
    {
429
      $priority = max($pMapKeys);
430
    }
431
    elseif ($priority < min($pMapKeys))
432
    {
433
      $priority = min($pMapKeys);
434
    }
435
    if (!$this->_setHeaderFieldModel('X-Priority',
436
      sprintf('%d (%s)', $priority, $priorityMap[$priority])))
437
    {
438
      $this->getHeaders()->addTextHeader('X-Priority',
439
        sprintf('%d (%s)', $priority, $priorityMap[$priority]));
440
    }
441
    return $this;
442
  }
443
 
444
  /**
445
   * Get the priority of this message.
446
   * The returned value is an integer where 1 is the highest priority and 5
447
   * is the lowest.
448
   * @return int
449
   */
450
  public function getPriority()
451
  {
452
    list($priority) = sscanf($this->_getHeaderFieldModel('X-Priority'),
453
      '%[1-5]'
454
      );
455
    return isset($priority) ? $priority : 3;
456
  }
457
 
458
  /**
459
   * Ask for a delivery receipt from the recipient to be sent to $addresses
460
   * @param array $addresses
461
   */
462
  public function setReadReceiptTo($addresses)
463
  {
464
    if (!$this->_setHeaderFieldModel('Disposition-Notification-To', $addresses))
465
    {
466
      $this->getHeaders()
467
        ->addMailboxHeader('Disposition-Notification-To', $addresses);
468
    }
469
    return $this;
470
  }
471
 
472
  /**
473
   * Get the addresses to which a read-receipt will be sent.
474
   * @return string
475
   */
476
  public function getReadReceiptTo()
477
  {
478
    return $this->_getHeaderFieldModel('Disposition-Notification-To');
479
  }
480
 
481
  /**
482
   * Attach a {@link Swift_Mime_MimeEntity} such as an Attachment or MimePart.
483
   * @param Swift_Mime_MimeEntity $entity
484
   */
485
  public function attach(Swift_Mime_MimeEntity $entity)
486
  {
487
    $this->setChildren(array_merge($this->getChildren(), array($entity)));
488
    return $this;
489
  }
490
 
491
  /**
492
   * Remove an already attached entity.
493
   * @param Swift_Mime_MimeEntity $entity
494
   */
495
  public function detach(Swift_Mime_MimeEntity $entity)
496
  {
497
    $newChildren = array();
498
    foreach ($this->getChildren() as $child)
499
    {
500
      if ($entity !== $child)
501
      {
502
        $newChildren[] = $child;
503
      }
504
    }
505
    $this->setChildren($newChildren);
506
    return $this;
507
  }
508
 
509
  /**
510
   * Attach a {@link Swift_Mime_MimeEntity} and return it's CID source.
511
   * This method should be used when embedding images or other data in a message.
512
   * @param Swift_Mime_MimeEntity $entity
513
   * @return string
514
   */
515
  public function embed(Swift_Mime_MimeEntity $entity)
516
  {
517
    $this->attach($entity);
518
    return 'cid:' . $entity->getId();
519
  }
520
 
521
  /**
522
   * Get this message as a complete string.
523
   * @return string
524
   */
525
  public function toString()
526
  {
527
    if (count($children = $this->getChildren()) > 0 && $this->getBody() != '')
528
    {
529
      $this->setChildren(array_merge(array($this->_becomeMimePart()), $children));
530
      $string = parent::toString();
531
      $this->setChildren($children);
532
    }
533
    else
534
    {
535
      $string = parent::toString();
536
    }
537
    return $string;
538
  }
539
 
540
  /**
541
   * Returns a string representation of this object.
542
   *
543
   * @return string
544
   *
545
   * @see toString()
546
   */
547
  public function __toString()
548
  {
549
    return $this->toString();
550
  }
551
 
552
  /**
553
   * Write this message to a {@link Swift_InputByteStream}.
554
   * @param Swift_InputByteStream $is
555
   */
556
  public function toByteStream(Swift_InputByteStream $is)
557
  {
558
    if (count($children = $this->getChildren()) > 0 && $this->getBody() != '')
559
    {
560
      $this->setChildren(array_merge(array($this->_becomeMimePart()), $children));
561
      parent::toByteStream($is);
562
      $this->setChildren($children);
563
    }
564
    else
565
    {
566
      parent::toByteStream($is);
567
    }
568
  }
569
 
570
  // -- Protected methods
571
 
572
  /** @see Swift_Mime_SimpleMimeEntity::_getIdField() */
573
  protected function _getIdField()
574
  {
575
    return 'Message-ID';
576
  }
577
 
578
  // -- Private methods
579
 
580
  /** Turn the body of this message into a child of itself if needed */
581
  private function _becomeMimePart()
582
  {
583
    $part = new parent($this->getHeaders()->newInstance(), $this->getEncoder(),
584
      $this->_getCache(), $this->_userCharset
585
      );
586
    $part->setContentType($this->_userContentType);
587
    $part->setBody($this->getBody());
588
    $part->setFormat($this->_userFormat);
589
    $part->setDelSp($this->_userDelSp);
590
    $part->_setNestingLevel($this->_getTopNestingLevel());
591
    return $part;
592
  }
593
 
594
  /** Get the highest nesting level nested inside this message */
595
  private function _getTopNestingLevel()
596
  {
597
    $highestLevel = $this->getNestingLevel();
598
    foreach ($this->getChildren() as $child)
599
    {
600
      $childLevel = $child->getNestingLevel();
601
      if ($highestLevel < $childLevel)
602
      {
603
        $highestLevel = $childLevel;
604
      }
605
    }
606
    return $highestLevel;
607
  }
608
 
609
}