Created
September 16, 2011 08:04
-
-
Save kijtra/1221500 to your computer and use it in GitHub Desktop.
[PHP] fsockopenを使ってメールを送信。送信中のサーバーレスポンス確認用。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function fsock_mail($from,$to,$subject,$body){ | |
$server='localhost';//メールサーバ | |
$port=25;//ポート番号 | |
$timeout=30;//タイムアウト(秒) | |
//件名の文字コード変換 | |
$subject=mb_convert_encoding($subject,'iso-2022-jp',mb_internal_encoding()); | |
//本文の文字コード変換 | |
$body=mb_convert_encoding($body,'iso-2022-jp',mb_internal_encoding()); | |
//メールサーバに接続 | |
$sock=fsockopen($server,$port,$errno,$errstr,$timeout); | |
//接続失敗時エラー | |
if(!$sock){ | |
trigger_error(__FUNCTION__.'() "fsockopen" connect error.',E_USER_ERROR); | |
return false; | |
} | |
//タイムアウト設定 | |
socket_set_timeout($sock,2); | |
$result=array(); | |
//SMTP発行 | |
fputs($sock,"HELO ".$server."\r\n"); | |
$result['HELO']=fgets($sock,128); | |
//FROM | |
fputs($sock,"MAIL FROM:<".$from.">\r\n"); | |
$result['MAIL FROM']=fgets($sock,128); | |
//TO | |
if(is_array($to)){ | |
foreach($to as $val){ | |
fputs($sock,"RCPT TO:<".$val.">\r\n"); | |
} | |
}else if(strpos($to,',')!==false){ | |
foreach(explode(',',$to) as $val){ | |
fputs($sock,"RCPT TO:<".$val.">\r\n"); | |
} | |
}else{ | |
fputs($sock,"RCPT TO:<".$to.">\r\n"); | |
} | |
$result['TO']=fgets($sock,128); | |
//DATA(ピリオドのみの行を送るまでが本文) | |
fputs($sock,"DATA\r\n"); | |
$result['DATA']=fgets($sock,128); | |
//Fromヘッダ | |
fputs($sock,"From: <".$from.">\r\n"); | |
$result['From:']=fgets($sock,128); | |
//Toヘッダ | |
if(is_array($to)){ | |
foreach ($to as $val) { | |
fputs($sock,"To:<".$val.">\r\n"); | |
} | |
}else if(strpos($to,',')!==false){ | |
foreach(explode(',',$to) as $val){ | |
fputs($sock,"To:<".$val.">\r\n"); | |
} | |
}else{ | |
fputs($sock,"To:<".$to.">\r\n"); | |
} | |
$result['To:']=fgets($sock,128); | |
//Subjectヘッダ | |
fputs($sock,"Subject:".$subject."\r\n"); | |
$result['Subject:']=fgets($sock,128); | |
//本文 | |
fputs($sock,$body."\r\n"); | |
$result['Body:']=fgets($sock,128); | |
fputs($sock,"\r\n.\r\n"); | |
$result['end']=fgets($sock); | |
fclose($sock); | |
//送信確認 | |
$result['success']=(strpos($result['end'],'250')===0); | |
return $result; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment