Skip to content

Instantly share code, notes, and snippets.

@bugcloud
Created September 7, 2012 02:15
Show Gist options
  • Select an option

  • Save bugcloud/3662596 to your computer and use it in GitHub Desktop.

Select an option

Save bugcloud/3662596 to your computer and use it in GitHub Desktop.
Send a Email for Jap
<?php
class Email {
/*
* This method send a mail for Japanese.
* (charaset of the email is 'ISO-2022-JP')
*
* Parameters
* to: sending email's TO
* cc: sending email's CC
* bcc: sending email's BCC
* subject: sending email's subject
* body: sending email's body
* from_email: sending email's From and Reply-To
* from_name: sending email's From and Reply-To
* attach_files: sending email's attach files.
* This parameter is Array type, so you can set multiple files.
*
* Return
* The result of sending as TRUE or FALSE.
*/
public static function send_jp_mail( $params = array() ) {
$to = ( array_key_exists('to', $params) ? $params['to'] : null);
$cc = ( array_key_exists('cc', $params) ? $params['cc'] : null);
$bcc = ( array_key_exists('bcc', $params) ? $params['bcc'] : null);
$subject = ( array_key_exists('subject', $params) ? $params['subject'] : null);
$body = ( array_key_exists('body', $params) ? $params['body'] : null);
$from_email = ( array_key_exists('from_email', $params) ? $params['from_email'] : null);
$from_name = ( array_key_exists('from_name', $params) ? $params['from_name'] : null);
$attach_files = ( array_key_exists('attach_files', $params) ? $params['attach_files'] : array());
$boundary = null;
$has_attachment = is_array($attach_files) && count($attach_files) > 0;
if ($has_attachment) {
$boundary = md5(uniqid(rand(), true));
}
$headers = "MIME-Version: 1.0 \n" ;
$headers .= "From: " .
"".mb_encode_mimeheader (mb_convert_encoding($from_name,"ISO-2022-JP","AUTO")) ."" .
"<".$from_email."> \n";
$headers .= "Reply-To: " .
"".mb_encode_mimeheader (mb_convert_encoding($from_name,"ISO-2022-JP","AUTO")) ."" .
"<".$from_email."> \n";
$headers .= "Cc: ".$cc."\n";
$headers .= "Bcc: ".$bcc."\n";
if ($has_attachment) {
$headers .= "Content-Type: multipart/mixed; boundary=\"$boundary\"\n";
} else {
$headers .= "Content-Type: text/plain;charset=ISO-2022-JP \n";
}
$headers .= "Content-Transfer-Encoding: 7bit";
/* Convert body to same encoding as stated
in Content-Type header above */
if ($has_attachment) {
$r_body = $body;
$body = "--$boundary\n";
$body .= "Content-Type: text/plain; charset=\"iso-2022-jp\"\n";
$body .= "Content-Transfer-Encoding: 7bit\n";
$body .= "\n";
$body .= mb_convert_encoding($r_body, "ISO-2022-JP","AUTO")."\n";
foreach ($attach_files as $file) {
if (!file_exists($file)) {
continue;
}
$filename = basename($file);
$body .= "\n";
$body .= "--$boundary\n";
$body .= "Content-Type: application/octet-stream; name=\"$filename\"\n";
$body .= "Content-Disposition: attachment; filename=\"$filename\"\n";
$body .= "Content-Transfer-Encoding: base64\n";
$body .= "\n";
$body .= chunk_split(base64_encode(file_get_contents($file)))."\n";
}
$body .= '--'.$boundary.'--';
} else {
$body = mb_convert_encoding($body, "ISO-2022-JP","AUTO");
}
/* Mail, optional paramiters. */
$sendmail_params = "-f$from_email";
mb_language("ja");
$subject = mb_convert_encoding($subject, "ISO-2022-JP","AUTO");
$subject = mb_encode_mimeheader($subject);
$result = mail($to, $subject, $body, $headers, $sendmail_params);
return $result;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment