pepogmail_send

pepogmail_sendとはdomain内よりsendmailなどのMTAを使用してメールが送信できない場合、サブミッションポート(587)が空いていれば自分のgmailアカントからメールを送信できるちょびっとなrubyスクリプト(CGI)です。

変更履歴

2011.11.28

o ファーストリリース

o 2012.7.21

o 添付ファイルを送信出来るよう機能UP

  1. #!/usr/bin/ruby -Ku
  2. # pepogmail_image_send.cgi ; mail send test & file
  3. # licence GPLv2 ; this scripts designed by IZAMUKARERA 2011.11.28 update 2012.7.21
  4. def error_cgi
  5. print "Content-Type:text/html;charset=UTF-8\n\n"
  6. print "*** CGI Error List ***<br />"
  7. print "#{CGI.escapeHTML($!.inspect)}<br />"
  8. $@.each {|x| print CGI.escapeHTML(x), "<br />"}
  9. end
  10. class Gmail
  11. def initialize()
  12. @mail = TMail::Mail.new
  13. @mail.mime_version = '1.0'
  14. end
  15. def set_account(user)
  16. @account = user
  17. end
  18. def set_mailfrom(from_address)
  19. @mail.from = from_address
  20. @mail.reply_to = from_address
  21. end
  22. def set_password(password)
  23. @password = password
  24. end
  25. def set_to(to_addresses)
  26. @mail.to = to_addresses
  27. end
  28. def set_subject(subject)
  29. work = Kconv.tojis(subject).split(//,1).pack('m').chomp
  30. @mail.subject = "=?ISO-2022-JP?B?"+work.gsub('\n', '')+"?="
  31. end
  32. def set_text(text)
  33. main_text = TMail::Mail.new
  34. main_text.set_content_type 'text', 'plain', {'charset'=>'iso-2022-jp'}
  35. main_text.body = Kconv.tojis(text)
  36. @mail.parts.push main_text
  37. end
  38. def send()
  39. @mail.date = Time.now
  40. @mail.write_back
  41. smtpserver = Net::SMTP.new('smtp.gmail.com', 587)
  42. smtpserver.enable_tls(OpenSSL::SSL::VERIFY_NONE)
  43. smtpserver.start('gmail.com', @account, @password, :login) do |smtp|
  44. smtp.sendmail(@mail.encoded, @mail.from, @mail.reply_to, @mail.to)
  45. end
  46. end
  47. def set_attach(file_dir, file_name)
  48. attach = TMail::Mail.new
  49. file_dir_name = file_dir + file_name
  50. attach.body = Base64.encode64 File.read(file_dir_name)
  51. attach.set_content_type 'application','mp4','name'=>file_name
  52. attach.set_content_disposition 'attachment','filename'=>file_name
  53. attach.transfer_encoding = 'base64'
  54. @mail.parts.push attach
  55. end
  56. end
  57. begin
  58. require 'net/smtp'
  59. require 'rubygems'
  60. require 'tmail'
  61. require 'tlsmail'
  62. require 'kconv'
  63. require 'openssl'
  64. require 'base64'
  65. require 'cgi'
  66. cgi = CGI.new()
  67. cgi_mail_from = cgi['mail_from']
  68. cgi_password = cgi['password']
  69. cgi_mail_to = cgi['mail_to']
  70. cgi_subject = cgi['subject']
  71. cgi_message = cgi['msg']
  72. cgi_server = cgi['server']
  73. cgi_file_name = cgi['image_file']
  74. cgi_file_dir = '/var/www/html/remote-hand/tmp/'
  75. user = 'gmail_account@gmail.com'
  76. user_password = 'gmail_password'
  77. s_password = 'this_server_password'
  78. print "Content-Type:text/html;charset=UTF-8\n\n"
  79. print '<BODY BGCOLOR="#E0FFFF">'
  80. print "<HEAD>"
  81. print "<TITLE>Mail Send Process</TITLE>"
  82. print "</HEAD>"
  83. print "<BODY>"
  84. if cgi_password == s_password then
  85. gmail = Gmail.new()
  86. gmail.set_account(user)
  87. gmail.set_password(user_password)
  88. gmail.set_mailfrom(cgi_mail_from)
  89. gmail.set_to(cgi_mail_to)
  90. gmail.set_subject(cgi_subject)
  91. gmail.set_text(cgi_message)
  92. if cgi_file_name != ""
  93. gmail.set_attach(cgi_file_dir, cgi_file_name)
  94. end
  95. gmail.send
  96. print "Send gmail -Successful completion<BR>"
  97. elsif cgi_password != s_password then
  98. print "Wrong password -Abend<BR>"
  99. end
  100. print "</BODY>"
  101. print "</HTML>"
  102. rescue
  103. error_cgi
  104. end

CGI確認用のちょびっとなmail formです。

<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>