PHPを使用して添付ファイルを含むEメールを送信する方法
12736 ワード
<?php
class Email {
//---
var $mailTo = ""; //
var $mailCC = ""; //
var $mailBCC = ""; //
var $mailFrom = ""; //
var $mailSubject = ""; //
var $mailText = ""; //
var $mailHTML = ""; // html
var $mailAttachments = ""; //
/*
setTo($inAddress) : $inAddress
,email ,
true
*/
function setTo($inAddress){
//-- explode() ”,”
$addressArray = explode( ",",$inAddress);
//--
for($i=0;$i<count($addressArray);$i++){ if($this->checkEmail($addressArray[$i])==false) return false; }
//-- email
$this->mailTo = implode($addressArray, ",");
return true;
}
/*
setCC($inAddress)
$inAddress ,email ,
true
*/
function setCC($inAddress){
//-- explode() ”,”
$addressArray = explode( ",",$inAddress);
//--
for($i=0;$i<count($addressArray);$i++){ if($this->checkEmail($addressArray[$i])==false) return false; }
//-- email
$this->mailCC = implode($addressArray, ",");
return true;
}
/*
setBCC($inAddress) $inAddress
,email ,
true
*/
function setBCC($inAddress){
//-- explode() ”,”
$addressArray = explode( ",",$inAddress);
//--
for($i=0;$i<count($addressArray);$i++)
{
if($this->checkEmail($addressArray[$i])==false)
return false;
}
//-- email
$this->mailBCC = implode($addressArray, ",");
return true;
}
/*
setFrom($inAddress): $inAddress
true
*/
function setFrom($inAddress){
if($this->checkEmail($inAddress)){
$this->mailFrom = $inAddress;
return true;
}
return false;
}
/*
setSubject($inSubject) $inSubject ,
true
*/
function setSubject($inSubject){
if(strlen(trim($inSubject)) > 0){
$this->mailSubject = preg_replace( "/n/", "",$inSubject);
return true;
}
return false;
}
/*
setText($inText) $inText
true
*/
function setText($inText){
if(strlen(trim($inText)) > 0){
$this->mailText = $inText;
return true;
}
return false;
}
/*
setHTML($inHTML) html $inHTML html ,
true
*/
function setHTML($inHTML){
if(strlen(trim($inHTML)) > 0){
$this->mailHTML = $inHTML;
return true;
}
return false;
}
/*
setAttachments($inAttachments) $inAttachments
, true
*/
function setAttachments($inAttachments){
if(strlen(trim($inAttachments)) > 0){
$this->mailAttachments = $inAttachments;
return true;
}
return false;
}
/*
checkEmail($inAddress) : ,
email
*/
function checkEmail($inAddress){
return (preg_match( "/^[^@ ]+@([a-zA-Z0-9-]+.)+([a-zA-Z0-9-]{2}|net|com|gov|mil|org|edu|int)$/",$inAddress));
}
/*
loadTemplate($inFileLocation,$inHash,$inFormat)
$inFileLocation
$inHash $inFormat
*/
function loadTemplate($inFileLocation,$inHash,$inFormat){
/* : Dear ~!UserName~,
Your address is ~!UserAddress~ */
//-- ”~!” ”~”
$templateDelim = "~";
$templateNameStart = "!";
//--
$templateLineOut = ""; //--
if($templateFile = fopen($inFileLocation, "r")){
while(!feof($templateFile)){
$templateLine = fgets($templateFile,1000);
$templateLineArray = explode($templateDelim,$templateLine);
for( $i=0; $i<count($templateLineArray);$i++){
//--
if(strcspn($templateLineArray[$i],$templateNameStart)==0){
//--
$hashName = substr($templateLineArray[$i],1);
//--
$templateLineArray[$i] = preg_match_replace($hashName,(string)$inHash[$hashName],$hashName);
}
}
//--
$templateLineOut .= implode($templateLineArray, "");
} //-- fclose($templateFile);
//-- ( html)
if( strtoupper($inFormat)== "TEXT" )
return($this->setText($templateLineOut));
else if( strtoupper($inFormat)== "HTML" )
return($this->setHTML($templateLineOut));
}
return false;
}
/*
getRandomBoundary($offset)
$offset – md5()
*/
function getRandomBoundary($offset = 0){
//--
srand(time()+$offset);
//-- md5 32
return ( "----".(md5(rand())));
}
/*
: getContentType($inFileName)
*/
function getContentType($inFileName){
//--
$inFileName = basename($inFileName);
//--
if(strrchr($inFileName, ".") == false){
return "application/octet-stream";
}
//--
$extension = strrchr($inFileName, ".");
switch($extension){
case ".gif": return "image/gif";
case ".gz": return "application/x-gzip";
case ".htm": return "text/html";
case ".html": return "text/html";
case ".jpg": return "image/jpeg";
case ".tar": return "application/x-tar";
case ".txt": return "text/plain";
case ".zip": return "application/zip";
default: return "application/octet-stream";
}
return "application/octet-stream";
}
/*
formatTextHeader text
*/
function formatTextHeader(){
$outTextHeader = "";
$outTextHeader .= "Content-Type: text/plain;charset=us-asciin";
$outTextHeader .= "Content-Transfer-Encoding: 7bitnn";
$outTextHeader .= $this->mailText. "n";
return $outTextHeader;
}
/*
formatHTMLHeader() html
*/
function formatHTMLHeader(){
$outHTMLHeader = "";
$outHTMLHeader .= "Content-Type: text/html;charset=us-asciin";
$outHTMLHeader .= "Content-Transfer-Encoding: 7bitnn";
$outHTMLHeader .= $this->mailHTML. "n";
return $outHTMLHeader;
}
/*
formatAttachmentHeader($inFileLocation)
*/
function formatAttachmentHeader($inFileLocation){
$outAttachmentHeader = "";
//-- getContentType($inFileLocation)
$contentType = $this->getContentType($inFileLocation);
//-- 7
if(preg_match( "/text/",$contentType)){
$outAttachmentHeader .= "Content-Type: ".$contentType. ";n";
$outAttachmentHeader .= ' name="'.basename($inFileLocation). '"'. "n";
$outAttachmentHeader .= "Content-Transfer-Encoding: 7bitn";
$outAttachmentHeader .= "Content-Disposition: attachment;n";
$outAttachmentHeader .= ' filename="'.basename($inFileLocation). '"'. "nn";
$textFile = fopen($inFileLocation, "r");
while(!feof($textFile)){
$outAttachmentHeader .= fgets($textFile,1000);
}
//-- fclose($textFile);
$outAttachmentHeader .= "n";
}
//-- 64
else{
$outAttachmentHeader .= "Content-Type: ".$contentType. ";n";
$outAttachmentHeader .= ' name="'.basename($inFileLocation). '"'. "n";
$outAttachmentHeader .= "Content-Transfer-Encoding: base64n";
$outAttachmentHeader .= "Content-Disposition: attachment;n";
$outAttachmentHeader .= ' filename="'.basename($inFileLocation). '"'. "nn";
//-- uuencode
exec( "uuencode -m $inFileLocation nothing_out",$returnArray);
for ($i = 1; $i<(count($returnArray)); $i++){
$outAttachmentHeader .= $returnArray[$i]. "n";
}
} return $outAttachmentHeader;
}
/*
send() , true
*/
function send(){
//--
$mailHeader = "";
//--
if($this->mailCC != "")
$mailHeader .= "CC: ".$this->mailCC. "n";
//--
if($this->mailBCC != "")
$mailHeader .= "BCC: ".$this->mailBCC. "n";
//--
if($this->mailFrom != "")
$mailHeader .= "FROM: ".$this->mailFrom. "n";
//--------------------------- ------------------------------
//--
if($this->mailText != "" && $this->mailHTML == "" && $this->mailAttachments == ""){
return mail($this->mailTo,$this->mailSubject,$this->mailText,$mailHeader);
}
//--html text
else if($this->mailText != "" && $this->mailHTML != "" && $this->mailAttachments == ""){
$bodyBoundary = $this->getRandomBoundary();
$textHeader = $this->formatTextHeader();
$htmlHeader = $this->formatHTMLHeader();
//-- MIME-
$mailHeader .= "MIME-Version: 1.0n";
$mailHeader .= "Content-Type: multipart/alternative;n";
$mailHeader .= ' boundary="'.$bodyBoundary. '"';
$mailHeader .= "nnn";
//--
$mailHeader .= "--".$bodyBoundary. "n";
$mailHeader .= $textHeader;
$mailHeader .= "--".$bodyBoundary. "n";
//-- html
$mailHeader .= $htmlHeader;
$mailHeader .= "n--".$bodyBoundary. "--";
//--
return mail($this->mailTo,$this->mailSubject, "",$mailHeader);
}
//-- html
else if($this->mailText != "" && $this->mailHTML != "" && $this->mailAttachments != ""){
$attachmentBoundary = $this->getRandomBoundary();
$mailHeader .= "Content-Type: multipart/mixed;n";
$mailHeader .= ' boundary="'.$attachmentBoundary. '"'. "nn";
$mailHeader .= "This is a multi-part message in MIME format.n";
$mailHeader .= "--".$attachmentBoundary. "n";
$bodyBoundary = $this->getRandomBoundary(1);
$textHeader = $this->formatTextHeader();
$htmlHeader = $this->formatHTMLHeader();
$mailHeader .= "MIME-Version: 1.0n";
$mailHeader .= "Content-Type: multipart/alternative;n";
$mailHeader .= ' boundary="'.$bodyBoundary. '"';
$mailHeader .= "nnn";
$mailHeader .= "--".$bodyBoundary. "n";
$mailHeader .= $textHeader;
$mailHeader .= "--".$bodyBoundary. "n";
$mailHeader .= $htmlHeader;
$mailHeader .= "n--".$bodyBoundary. "--";
//--
$attachmentArray = explode( ",",$this->mailAttachments);
//--
for($i=0;$i<count($attachmentArray);$i++){
//-- $mailHeader .= "n--".$attachmentBoundary. "n";
//--
$mailHeader .= $this->formatAttachmentHeader($attachmentArray[$i]);
}
$mailHeader .= "--".$attachmentBoundary. "--";
return mail($this->mailTo,$this->mailSubject, "",$mailHeader);
}
return false;
}
}
$mail = new Email();
$mail->setTo("[email protected]"); //
$mail->setCC("[email protected]"); //
$mail->setCC("[email protected]"); //
$mail->setFrom("[email protected]");//
$mail->setSubject(" ") ; //
$mail->setText(" ") ;//
$mail->setHTML("html ") ;// html
//$mail->setAttachments(“c:a.jpg”) ;// ,
$mail->send(); //
?>