Skip to content

Commit 0799bf1

Browse files
committed
MailMessage: add setDate method, add fluent API
1 parent 4c7ebf0 commit 0799bf1

File tree

2 files changed

+45
-6
lines changed

2 files changed

+45
-6
lines changed

src/Mail/MailMessage.php

+42-5
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
namespace Eventum\Mail;
1515

16+
use Date_Helper;
1617
use DateTime;
1718
use DomainException;
1819
use Eventum\Mail\Helper\DecodePart;
@@ -26,6 +27,7 @@
2627
use Zend\Mail\Header\AbstractAddressList;
2728
use Zend\Mail\Header\Cc;
2829
use Zend\Mail\Header\ContentType;
30+
use Zend\Mail\Header\Date;
2931
use Zend\Mail\Header\From;
3032
use Zend\Mail\Header\GenericHeader;
3133
use Zend\Mail\Header\HeaderInterface;
@@ -512,33 +514,63 @@ protected function getSubject()
512514
}
513515

514516
/**
515-
* Set the message subject header value, return Subject object
517+
* Set the message subject header value
516518
*
517-
* @return Subject
519+
* @param string $subject
520+
* @return $this
518521
*/
519522
public function setSubject($subject)
520523
{
521-
return $this->getSubject()->setSubject($subject);
524+
$this->getSubject()->setSubject($subject);
525+
526+
return $this;
522527
}
523528

524529
/**
525530
* Set To: header
526531
*
527532
* @param string|AddressList $value
533+
* @return $this
528534
*/
529535
public function setTo($value)
530536
{
531537
$this->setAddressListHeader('To', $value);
538+
539+
return $this;
532540
}
533541

534542
/**
535543
* Set From: header
536544
*
537-
* @param string $value
545+
* @param string|AddressList $value
546+
* @return $this
538547
*/
539548
public function setFrom($value)
540549
{
541550
$this->setAddressListHeader('From', $value);
551+
552+
return $this;
553+
}
554+
555+
/**
556+
* Set Date: header
557+
*
558+
* @param string $value
559+
* @return $this
560+
*/
561+
public function setDate($value = null)
562+
{
563+
$value = $value ?: Date_Helper::getRFC822Date(time());
564+
565+
// can not update 'Date' header, so remove it
566+
if ($this->headers->has('Date')) {
567+
$this->headers->removeHeader('Date');
568+
}
569+
570+
$header = new Date($value);
571+
$this->headers->addHeader($header);
572+
573+
return $this;
542574
}
543575

544576
/**
@@ -697,6 +729,9 @@ public function stripHeaders()
697729
*
698730
* IMPORTANT: it should not contain any multipart changes,
699731
* as then everything will blow up as it is not parsed again.
732+
*
733+
* @param string|Mime\Message $content
734+
* @return $this
700735
*/
701736
public function setContent($content)
702737
{
@@ -734,10 +769,12 @@ public function setContent($content)
734769
}
735770
$this->content = $message->getBodyText();
736771

737-
return;
772+
return $this;
738773
}
739774

740775
$this->content = $content;
776+
777+
return $this;
741778
}
742779

743780
/**

tests/Mail/MailMessageTest.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -756,7 +756,9 @@ public function testParseHeaders()
756756
$value = '[#77675] New Issue:xxxxxxxxx xxxxxxx xxxxxxxx xxxxxxxxxxxxx xxxxxxxxxx xxxxxxxx, tähtaeg xx.xx, xxxx';
757757
try {
758758
// it fails with line length exactly 76, but suceeds with anything else, like 75 or 77
759-
$v = iconv_mime_encode('x-test', $value, ['scheme' => 'Q', 'line-length' => '76', 'line-break-chars' => ' ']);
759+
$v = iconv_mime_encode(
760+
'x-test', $value, ['scheme' => 'Q', 'line-length' => '76', 'line-break-chars' => ' ']
761+
);
760762
} catch (PHPUnit_Framework_Error_Notice $e) {
761763
error_log($e->getMessage());
762764
}

0 commit comments

Comments
 (0)