Matthew Weier O'Phinney
1 function shop_mail($to, $subject, $body)
2 {
3 $headers = "From: shop@example.com\r\n"
4 .= "Bcc: shop-sent@example.com\r\n";
5 mail($to, $subject, $body, $headers);
6 }
$headers
manually each call$headers
manually each callmail()
1 <?php
2 function shop_mail($to, $subject, $body, $shop = 'original') {
3 switch ($shop) {
4 case: 'subdomain':
5 $from = 'shop@subdomain.example.com';
6 $bcc = 'shop-sent@subdomain.example.com';
7 break;
8 case: 'original':
9 default:
10 $from = 'shop@example.com';
11 $bcc = 'shop-sent@example.com';
12 break;
13 }
14
15 $headers = "From: $from\r\n"
16 .= "Bcc: $bcc\r\n";
17 mail($to, $subject, $body, $headers);
18 }
mail()
1 <?php
2 class ShopMail
3 {
4 protected static $from = 'shop@example.com';
5 protected static $bcc = 'shop-sent@example.com';
6
7 public static function send($to, $subject, $body)
8 {
9 $headers = "From: " . static::$from . "\r\n"
10 .= "Bcc: " . static::$bcc . "\r\n";
11 mail($to, $subject, $body, $headers);
12 }
13 }
1 <?php
2 class SubdomainMail extends ShopMail
3 {
4 protected static $from = 'shop@subdomain.example.com';
5 protected static $bcc = 'shop-sent@subdomain.example.com';
6 }
1 <?php
2 define('MYENV', 'Subdomain');
3
4 $mailer = MYENV . 'Mail::send';
5 call_user_func($mailer, $to, $subject, $body);
$mailer($to, $subect,
$body)
1 <?php
2 $config = new ArrayObject(array(), ArrayObject::ARRAY_AS_PROPS);
3 $config->env = "Subdomain';
4
5 $mailer = $config->env . 'Mail::send';
6 call_user_func($mailer, $to, $subject, $body);
1 <?php
2 class Mailer
3 {
4 protected $from = 'shop@example.com';
5 protected $bcc = 'shop-sent@example.com';
6 protected $contentType = 'text/plain';
7
8 public function setFrom($from) { ... }
9 public function setBcc($bcc) { ... }
10 public function setContentType($type) { ... }
11 public function send($to, $subject, $body)
12 {
13 $headers = "From: " . $this->from . "\r\n"
14 .= "Bcc: " . $this->bcc . "\r\n"
15 .= "Content-Type: " . $this->contentType . "\r\n";
16 mail($to, $subject, $body, $headers);
17 }
18 }
1 <?php
2 $mailer = new Mailer();
3 $mailer->setFrom($config->from)
4 ->setBcc($config->bcc)
5 ->setContentType('text/html');
6 $mailer->send($to, $subject, $body);
mail()
? 1 <?php
2 interface MailTransport
3 {
4 public function send($to, $subject, $body, $headers);
5 }
6
7 class MailHeaders extends ArrayObject
8 {
9 public function toString()
10 {
11 $headers = '';
12 foreach ($this as $header => $value) {
13 $headers .= $header . ': ' . $value . "\r\n";
14 }
15 return $headers;
16 }
17 }
1 <?php
2 class Mailer
3 {
4 protected $headers, $transport;
5
6 public function setHeaders(MailHeaders $headers) {
7 $this->headers = $headers;
8 return $this;
9 }
10
11 public function getHeaders() {
12 return $this->headers;
13 }
14
15 public function setTransport(MailTransport $transport) {
16 $this->transport = $transport;
17 return $this;
18 }
19
20 /* ... */
21 }
1 <?php
2 class Mailer
3 {
4 protected $headers, $transport;
5
6 public function \__construct(MailTransport $transport)
7 {
8 $this->setTransport($transport);
9 $this->setHeaders(new MailHeaders());
10 }
11 public function send($to, $subject, $body)
12 {
13 $this->transport->send(
14 $to, $subject, $body,
15 $this->headers->toString()
16 );
17 }
18 }
1 <?php
2 $mailer = new Mailer(new SmtpTransport);
3 $headers = new MailHeaders();
4 // or $headers = $mailer->getHeaders();
5
6 $headers['From'] = $config->from;
7 $headers['Bcc'] = $config->bcc;
8 $headers['Content-Type'] = 'text/html';
9 $mailer->setHeaders($headers) // if instantiated separately
10 ->send($to, $subject, $body);
1 <?php
2 interface MailMessage
3 {
4 public function setTo($to);
5 public function setSubject($subject);
6 public function setBody($body);
7 public function setHeaders(MailHeaders $headers);
8
9 public function getTo();
10 public function getSubject();
11 public function getBody();
12 public function getHeaders();
13 }
1 <?php
2 interface MailHeaders
3 {
4 public function addHeader($header, $value);
5 public function toString();
6 }
7
8 interface MailTransport
9 {
10 public function send(MailMessage $message);
11 }
1 <?php
2 $message = new Message();
3 $headers = new MessageHeaders();
4 $headers->addHeader('From', $from)
5 ->addHeader('Content-Type', 'text/html');
6 $message->setTo($to)
7 ->setSubject($subject)
8 ->setBody($body)
9 ->setHeaders($headers);
10 $transport = new SmtpTransport($config->transport);
11 $transport->send($message);
You can always write shorter code. The trade off is re-use.
You can always write shorter code. The trade off is re-use.
Writing robust code usually requires some verbosity.
Writing robust code usually requires some verbosity.
Writing robust code usually requires some verbosity.
Configuration can be either inline, or from a container.
Configuration can be either inline, or from a container.
Table of Contents | t |
---|---|
Exposé | ESC |
Full screen slides | e |
Presenter View | p |
Source Files | s |
Slide Numbers | n |
Toggle screen blanking | b |
Show/hide slide context | c |
Notes | 2 |
Help | h |