sendmailが使えなかったときに作った関数です。
さすがに今どき、役に立つ人は居ないかなぁ。
| 
					 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101  | 
						<?php /**  * @param string $sv		メールサーバーのIPアドレス  * @param int 	 $port		ポート番号  * @param string $mail_from	送信元メールアドレス  * @param string $send_to	相手先メールアドレス  * @param string $smtp_hello_str	接続元を特定するための文字列。ヘッダに使われる  * @param string $subject	メールの表題  * @param string $message	メール本文  * @param string $user_id	ESMTPの認証ユーザー。送信元メールアドレスそのものだったりする  * @param string $user_pw	ESMTPの認証パスワード。メールサーバーによってはPOPのパスワードと同じだったりする  * @return bool		送ったらTrue  */ function directsendmail($sv, $port=587, $mail_from, $send_to, $smtp_hello_str, $subject, $message, $user_id, $user_pw) { 	$go_ahead=false; 	if($soc=fsockopen($sv, $port)){ 		// 		$tmp_fgets_str=fgets($soc, 1024); 		if(substr($tmp_fgets_str, 0, 3)=='220'){ 			// 			if(strstr($tmp_fgets_str, "ESMTP")){ 				// 				fputs($soc, "EHLO $smtp_hello_str\r\n"); 				$auth_sw=""; 				$auth_check_loop=true; 				while($auth_check_loop){ 					if($tmp_fgets_str=fgets($soc, 1024)){ 						if(substr($tmp_fgets_str, 0, 3)=='250'){ 							if($auth_sw==""){ 								$auth_sw="NO_AUTH"; 							} 							if(substr($tmp_fgets_str, 4, 4)=='AUTH'){ 								if(strstr($tmp_fgets_str, "PLAIN")){ 									$auth_sw="PLAIN"; 								} 							} 							if(substr($tmp_fgets_str, 3, 1)==' '){ 								$auth_check_loop=false; 							} 						} 					}else{ 						$auth_check_loop=false; 					} 				} 				// 				switch($auth_sw){ 				case "PLAIN": 					fputs($soc, "AUTH PLAIN ".base64_encode($user_id.chr(0).$user_id.chr(0).$user_pw)."\r\n"); 					if(substr(fgets($soc, 1024), 0, 3)=='235'){ 						$go_ahead=true; 					} 					break; 				case "NO_AUTH": 					$go_ahead=true; 					break; 				default: 					break; 				} 			}else{ 				// 			    fputs($soc, "HELO $smtp_hello_str\r\n"); 				if(substr(fgets($soc, 1024), 0, 3)=='250'){ 					$go_ahead=true; 				} 			} 			// 			if($go_ahead){ 				$go_ahead=false; 			    $mbsubject = mb_encode_mimeheader( mb_convert_encoding($subject, "JIS") ); 				$message = str_replace("\r\n","\n",$message); 				$message = str_replace("\r","\n",$message); 				$message = str_replace("\n","\r\n",$message); 			    $mbmessage = mb_convert_encoding($message, "JIS"); 			    fputs($soc, "MAIL FROM:<$mail_from>\r\n"); 				if(substr(fgets($soc, 1024), 0, 3)=='250'){ 			    	fputs($soc, "RCPT TO:<$send_to>\r\n"); 					if(substr(fgets($soc, 1024), 0, 3)=='250'){ 				    	fputs($soc, "DATA\r\n"); 						if(substr(fgets($soc, 1024), 0, 3)=='354'){ 						    fputs($soc, "From: $mail_from\r\n"); 						    fputs($soc, "To: $send_to\r\n"); 						    fputs($soc, "Subject: $mbsubject\r\n"); 						    fputs($soc, "MIME-Version: 1.0\r\n"); 						    fputs($soc, "Content-Type: Text/Plain; charset=iso-2022-jp\r\n"); 						    fputs($soc, "Content-Transfer-Encoding: 7bit\r\n"); 						    fputs($soc, "\r\n"); 						    fputs($soc, "$mbmessage\r\n"); 						    fputs($soc, ".\r\n"); 							if(substr(fgets($soc, 1024), 0, 3)=='250'){ 						    	fputs($soc, "QUIT\r\n"); 								$go_ahead=true; 							} 						} 					} 				} 			} 		}     	fclose($soc); 	} 	return $go_ahead; }  |