Skip to content

Instantly share code, notes, and snippets.

@ukyo
Created June 10, 2014 09:52
Show Gist options
  • Save ukyo/ff109153d495fb7b43b9 to your computer and use it in GitHub Desktop.
Save ukyo/ff109153d495fb7b43b9 to your computer and use it in GitHub Desktop.
添付メール送る奴 php 5.1.6で動作確認
<?php
require "path/to/mail.php";
echo send_mail("[email protected]", "[email protected]", "subject", "message", "path/to/file.format");
<?php
// 参考
// http://www.shotdev.com/php/php-mail/php-send-email-upload-form-attachment-file/
function send_mail($to, $from, $subject, $body, $path = "") {
$random_hash = md5(date('r', time()));
$rn = "\r\n";
$filename = basename($path);
// header
$headers = "From: $from <$from>$rn";
$headers .= "MIME-Version: 1.0$rn";
$headers .= "Content-Type: multipart/mixed; boundary=\"$random_hash\"$rn$rn";
$headers .= "This is a multi-part message in MIME format.$rn";
// body
$headers .= "--$random_hash$rn";
$headers .= "Content-Type: text/html; charset=utf-8$rn";
$headers .= "Content-Transfer-Encoding: 7bit$rn$rn";
$headers .= "$body$rn$rn";
// attachment
if (!empty($path)) {
$headers .= "--$random_hash$rn";
$headers .= "Content-Type: application/octet-stream; name=\"$filename\"$rn";
$headers .= "Content-Transfer-Encoding: base64$rn";
$headers .= "Content-Dispositon: attachment \"$filename\"$rn$rn";
$attachment = chunk_split(base64_encode(file_get_contents($path)));
$headers .= "$attachment$rn$rn";
}
return mail($to, $subject, null, $headers);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment