pepogmail_sendとはdomain内よりsendmailなどのMTAを使用してメールが送信できない場合、サブミッションポート(587)が空いていれば自分のgmailアカントからメールを送信できるちょびっとなrubyスクリプト(CGI)です。
変更履歴
2011.11.28
o ファーストリリース
o 2012.7.21
o 添付ファイルを送信出来るよう機能UP
- #!/usr/bin/ruby -Ku
- # pepogmail_image_send.cgi ; mail send test & file
- # licence GPLv2 ; this scripts designed by IZAMUKARERA 2011.11.28 update 2012.7.21
- def error_cgi
- print "Content-Type:text/html;charset=UTF-8\n\n"
- print "*** CGI Error List ***<br />"
- print "#{CGI.escapeHTML($!.inspect)}<br />"
- $@.each {|x| print CGI.escapeHTML(x), "<br />"}
- end
- class Gmail
- def initialize()
- @mail = TMail::Mail.new
- @mail.mime_version = '1.0'
- end
- def set_account(user)
- @account = user
- end
- def set_mailfrom(from_address)
- @mail.from = from_address
- @mail.reply_to = from_address
- end
- def set_password(password)
- @password = password
- end
- def set_to(to_addresses)
- @mail.to = to_addresses
- end
- def set_subject(subject)
- work = Kconv.tojis(subject).split(//,1).pack('m').chomp
- @mail.subject = "=?ISO-2022-JP?B?"+work.gsub('\n', '')+"?="
- end
- def set_text(text)
- main_text = TMail::Mail.new
- main_text.set_content_type 'text', 'plain', {'charset'=>'iso-2022-jp'}
- main_text.body = Kconv.tojis(text)
- @mail.parts.push main_text
- end
- def send()
- @mail.date = Time.now
- @mail.write_back
- smtpserver = Net::SMTP.new('smtp.gmail.com', 587)
- smtpserver.enable_tls(OpenSSL::SSL::VERIFY_NONE)
- smtpserver.start('gmail.com', @account, @password, :login) do |smtp|
- smtp.sendmail(@mail.encoded, @mail.from, @mail.reply_to, @mail.to)
- end
- end
- def set_attach(file_dir, file_name)
- attach = TMail::Mail.new
- file_dir_name = file_dir + file_name
- attach.body = Base64.encode64 File.read(file_dir_name)
- attach.set_content_type 'application','mp4','name'=>file_name
- attach.set_content_disposition 'attachment','filename'=>file_name
- attach.transfer_encoding = 'base64'
- @mail.parts.push attach
- end
- end
- begin
- require 'net/smtp'
- require 'rubygems'
- require 'tmail'
- require 'tlsmail'
- require 'kconv'
- require 'openssl'
- require 'base64'
- require 'cgi'
- cgi = CGI.new()
- cgi_mail_from = cgi['mail_from']
- cgi_password = cgi['password']
- cgi_mail_to = cgi['mail_to']
- cgi_subject = cgi['subject']
- cgi_message = cgi['msg']
- cgi_server = cgi['server']
- cgi_file_name = cgi['image_file']
- cgi_file_dir = '/var/www/html/remote-hand/tmp/'
- user = 'gmail_account@gmail.com'
- user_password = 'gmail_password'
- s_password = 'this_server_password'
- print "Content-Type:text/html;charset=UTF-8\n\n"
- print '<BODY BGCOLOR="#E0FFFF">'
- print "<HEAD>"
- print "<TITLE>Mail Send Process</TITLE>"
- print "</HEAD>"
- print "<BODY>"
- if cgi_password == s_password then
- gmail = Gmail.new()
- gmail.set_account(user)
- gmail.set_password(user_password)
- gmail.set_mailfrom(cgi_mail_from)
- gmail.set_to(cgi_mail_to)
- gmail.set_subject(cgi_subject)
- gmail.set_text(cgi_message)
- if cgi_file_name != ""
- gmail.set_attach(cgi_file_dir, cgi_file_name)
- end
- gmail.send
- print "Send gmail -Successful completion<BR>"
- elsif cgi_password != s_password then
- print "Wrong password -Abend<BR>"
- end
- print "</BODY>"
- print "</HTML>"
- rescue
- error_cgi
- end
<HTML> <HEAD> <META http-equiv="Content-Type" content="text/HTML; charset=UTF-8"> <META NAME="Auther" content="yamauchi.isamu"> <META NAME="Copyright" content="pepolinux.local"> <META NAME="Build" content="2012.7.9"> <META NAME="reply-to" content="pepo1@pepolinux.local"> <script type="text/javascript"> function mail_ck(str) { /* mailアドレスチェック */ var ck_pattern = /[!#-9A-~]+@+[0-9A-Za-z.-]+.+[^.]$/; if( ! str.match(ck_pattern)) { alert(str + "←" + "メールアドレスに誤りがあります。"); return -1; } return 1; } function num_alpha_ck(str) { /* 半角英数字チェック */ if( str.match( /[^A-Za-z0-9\s.\-_]/ ) ) { alert(str + "←" + "半角英数字でのみ入力してください。"); return -1; } return 1; } function msg_ck(str) { /* メッセージ長さチェック */ var ck_size = str.length if( ck_size > 255 ) { alert("メッセージサイズオーバーです"); return -1; } return 1; } function sendmail_ck() { var check = 0; var error_ct = 0; var ck_str = ""; ck_str = document.menu.mail_to.value; if ( ck_str != "" ) { check++; if ( mail_ck(ck_str) == -1 ) { error_ct++; } } ck_str = document.menu.mail_from.value; if ( ck_str != "" ) { check++; if ( mail_ck(ck_str) == -1 ) { error_ct++; } } ck_str = document.menu.msg.value; if ( ck_str != "" ) { check++; if ( msg_ck(ck_str) == -1 ) { error_ct++; } } ck_str = document.menu.subject.value; if ( ck_str != "") { check++; } ck_str = document.menu.password.value; if ( ck_str != "") check++; ck_str = document.menu.server.value; if ( ck_str != "") { check++; if (num_alpha_ck(ck_str) == -1 ) { error_ct++; } } ck_str = document.menu.image_file; if ( ck_str != "") check++; if( error_ct == 0 && check == 6 || check == 7) { document.menu.submit(ck_str); } if ( check < 6) { alert("入力されていない項目があります") } return false; } function keypress() { /* Enter キー無効 */ if(window.event.keyCode == 13){ return false; } return true; } window.document.onkeydown= keypress; </script> <TITLE>メール送信</TITLE> </HEAD> <BODY BGCOLOR="#E0FFFF"> <!--Remote-Hand--> <DT><FONT SIZE="+1"><B>メールを送信します</B></FONT></DT> <FORM name="menu" ACTION="/cgi-bin/pepogmail_send.cgi" METHOD="POST"> <FONT SIZE="-1">Mail to:<INPUT TYPE="text" style="width:200px;text-align:left;" VALUE="" NAME="mail_to"> <BR> <FONT SIZE="-1">Mail From:<INPUT TYPE="text" style="width:200px;text-align:left;" VALUE="pepo1@pepolinux.local" NAME="mail_from"> <FONT> <BR> <FONT SIZE="-1">Subject:<INPUT TYPE="text" style="width:240px;text-align:left;" VALUE="メールの送信テスト" NAME="subject"> <BR> <textarea NAME="msg" rows="4" cols="40" >ここにメッセージを書いて送信してください</textarea><BR> <HR> <FONT SIZE="-1">パスワード:<INPUT SIZE="10" TYPE="password" NAME="password"> <BR> <FONT SIZE="-1">メールサーバ:<INPUT TYPE="text" style="width:200px;text-align:left;" VALUE="" NAME="server"> <BR> <FONT SIZE="-1">イメージファイル:<INPUT TYPE="text" style="width:100px;text-align:left;" VALUE="" NAME="image_file"> <BR> <INPUT style="text-align:center" TYPE="button" VALUE="実行" onClick="return sendmail_ck()" ;> <INPUT style="text-align:center" TYPE="reset" VALUE="クリア"> </FORM> </BODY> </HTML>
[PageInfo]
LastUpdate: 2012-10-11 04:44:08, ModifiedBy: pepolinux
[License]
GNU Free Documentation License
[Permissions]
view:all, edit:doc editors, delete/config:doc editors