Skip to content

Instantly share code, notes, and snippets.

@kkurahar
Last active December 22, 2015 19:39
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 kkurahar/6521477 to your computer and use it in GitHub Desktop.
Save kkurahar/6521477 to your computer and use it in GitHub Desktop.
FuelPHP + Amazon SES マルチバイト対応 http://kkurahar.github.io/blog/2013/09/11/fuelphp-aws-ses/
<?php
// Include the SDK using the Composer autoloader
require_once(APPPATH . "vendor/aws-2.0/autoload.php");
// Include the Mail_MIME
require_once("Mail/mime.php");
use Aws\Common\Aws;
use Aws\Common\Enum\Region;
use Aws\Ses\Exception\SesException;
use Aws\Ses\Exception\MessageRejectedException;
class Aws_Ses_Client
{
protected $config = [];
protected $ses = null;
protected $mail_mime = null;
protected $to = [];
protected $cc = [];
protected $bcc = [];
protected $subject = '';
protected $body_text = '';
protected $body_html = '';
protected $attachments = [];
/**
* Driver constructor
*
* @param array $config driver config
*/
public function __construct(array $config)
{
$this->config = $config;
$this->ses = Aws::factory(array(
'key' => \Config::get('access_key'),
'secret' => \Config::get('secret_key'),
'region' => Region::US_EAST_1
))->get('ses');
$this->mail_mime = new Mail_mime($this->config['crlf']);
}
public function from($email, $name = false)
{
$this->config['from']['email'] = (string) $email;
return $this;
}
public function to($to)
{
$this->to = $to;
return $this;
}
public function cc($cc)
{
$this->cc = $cc;
return $this;
}
public function bcc($bcc)
{
$this->bcc = $bcc;
return $this;
}
public function subject($subject)
{
$this->subject = (string) $subject;
return $this;
}
public function body_text($body)
{
$this->body_text = (string) $body;
return $this;
}
public function body_html($body)
{
$this->body_html = (string) $body;
return $this;
}
public function attachment($attach = [])
{
$this->attachments = $attach;
return $this;
}
public function send()
{
if (empty($this->to) and empty($this->cc) and empty($this->bcc)) {
throw new \FuelException('Cannot send email without recipients.');
}
if (($from = $this->config['from']['email']) === false or empty($from)) {
throw new \FuelException('Cannot send without from address.');
}
$to = $this->to;
$cc = $this->cc;
$bcc = $this->bcc;
$subject = $this->subject;
$body_text = $this->body_text;
$body_html = $this->body_html;
$attachments = $this->attachments;
if($attachments) {
/* UTF-8 -> ISO-2022-JP */
// 内部エンコーディングを取得
$internalChar = mb_internal_encoding();
// mimeメッセージを作成
$subject = $this->encode_mimeheader($subject, $internalChar);
mb_convert_variables($this->config['charset'], $internalChar, $body_text);
mb_convert_variables($this->config['charset'], $internalChar, $body_html);
if ($body_text) $this->mail_mime->setTxtBody($body_text);
if ($body_html) $this->mail_mime->setHTMLBody($body_html);
// 添付ファイルを設定
$this->add_addAttachment($attachments, $internalChar);
// mimeフォーマットのヘッダーとボディを作成
$param['head_charset'] = $this->config['charset'];
$param['text_encoding'] = $this->config['encoding'];
$param['text_charset'] = $this->config['charset'];
$param['html_encoding'] = $this->config['charset'];
$param['html_charset'] = $this->config['charset'];
$mime_body = $this->mail_mime->get($param);
$mime_headers = $this->mail_mime->txtHeaders([
'From' => $from,
'To' => $to,
'Cc' => $cc,
'Bcc' => $bcc,
'Subject' => $subject
]);
// mimeメッセージを作成
$mime_message = $mime_headers . "\r\n" . $mime_body;
$result = $this->ses->sendRawEmail([
'RawMessage' => [
'Data' => base64_encode($mime_message)
]
]);
} else {
$arrBody = [];
if ($body_text) $arrBody += ['Text' => ['Data' => $body_text,'Charset' => $this->config['charset']]];
if ($body_html) $arrBody += ['Html' => ['Data' => $body_html,'Charset' => $this->config['charset']]];
$result = $this->ses->sendEmail([
'Source' => $from,
'Destination' => [
'ToAddresses' => $to,
'CcAddresses' => $cc,
'BccAddresses' => $bcc
],
'Message' => [
'Subject' => ['Data' => $subject,'Charset' => $this->config['charset']],
'Body' => $arrBody
]
]);
}
}
protected function encode_mimeheader($header, $char)
{
$header = mb_convert_encoding($header, $this->config['charset'], $char);
$transfer_encoding = ($this->config['encoding'] === 'quoted-printable') ? 'Q' : 'B' ;
return mb_encode_mimeheader($header, $this->config['charset'], $transfer_encoding, $this->config['newline']);
}
protected function add_addAttachment($attachments, $char)
{
foreach ($attachments as $file => $c_type) {
$f_name = basename($file);
mb_convert_variables($this->config['charset'], $char, $f_name);
$this->mail_mime->addAttachment(
$file,
$c_type,
$f_name,
true, // isfile
'base64',// encoding
'attachment', // disposition attachment
$this->config['charset'], // charset
'', // language
'', // location
'base64', // n_encoding
$this->config['charset'], // f_encoding Encoding of the attachment's filename in Content-Disposition header.
'', // description
$this->config['charset'] // h_charset
);
}
}
}
<?php
return array(
// Amazon Web Services Key. Found in the AWS Security Credentials. You can also pass
// this value as the first parameter to a service constructor.
'access_key' => 'your access key',
// Amazon Web Services Secret Key. Found in the AWS Security Credentials. You can also
// pass this value as the second parameter to a service constructor.
'secret_key' => 'your secret key',
//----------------------------------------------------------------------
// S3関連
//----------------------------------------------------------------------
's3' => array(
),
//----------------------------------------------------------------------
// AutoScaling関連
//----------------------------------------------------------------------
'as' => array(
),
//----------------------------------------------------------------------
// SES関連
//----------------------------------------------------------------------
'ses' => array(
'charset' => 'ISO-2022-JP',
'encode_headers' => false,
'encoding' => '7bit',
'newline' => "\n",
'crlf' => "\n",
'from' => array(
'email' => false,
'name' => false,
),
),
);
<?php
class Aws_Ses
{
/**
* Driver config defaults.
*/
protected static $_ses;
public static function forge()
{
$driver = new Aws_Ses_Client(static::$_ses);
return $driver;
}
/**
* Init, config loading.
*/
public static function _init()
{
static::$_ses = \Config::get('ses');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment