Skip to content

Instantly share code, notes, and snippets.

@bto
Created January 29, 2015 02:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bto/51175706642cb2ac1f3c to your computer and use it in GitHub Desktop.
Save bto/51175706642cb2ac1f3c to your computer and use it in GitHub Desktop.
<?php
class jpSimpleMail {
public $mailer;
public $message;
public static function create($mailer) {
return new static();
}
public function __construct() {
}
public function getMailer() {
if ($this->mailer !== null) {
return $this->mailer;
}
$this->mailer = sfContext::getInstance()->getMailer();
return $this->mailer;
}
public function getMessage() {
if ($this->message !== null) {
return $this->message;
}
$this->message = Swift_Message::newInstance()
->setCharset('iso-2022-jp')
->setEncoder(new Swift_Mime_ContentEncoder_PlainContentEncoder('7bit'));
return $this->message;
}
public function addTo($to) {
$this->getMessage()->setTo($this->createArrayAddress($to));
return $this;
}
public function setBody($body, $contentType = null) {
$this->getMessage()->setBody($body, $contentType);
return $this;
}
public function setFrom($from) {
$this->getMessage()->setFrom($this->createArrayAddress($from));
return $this;
}
public function setSender($sender) {
$this->getMessage()->setSender($sender);
return $this;
}
public function setSubject($subject) {
$subject = mb_convert_encoding($subject, 'iso-2022-jp', 'utf-8');
$this->getMessage()->setSubject($subject);
return $this;
}
public function send() {
return $this->getMailer()->send($this->getMessage());
}
protected function createArrayAddress($addr) {
if (preg_match('/(.*)<(.*)>/', $addr, $matches)) {
$address = trim($matches[2]);
$title = mb_encode_mimeheader(
trim($matches[1]),
'iso-2022-jp',
'B',
"\r\n"
);
return array($address => $title);
} else {
return $addr;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment