4
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

rubyをcgiで動かす

Posted at

以下サンプルコードをapacheのcgiで動かすのにはまった。

sendmail.rb
# !/usr/bin/ruby
# encoding: utf-8

# sendmail.rb?body=本文 というように値を取得できる。
def get_body()
    require 'cgi'
    cgi = CGI.new
    body = cgi['body']
end

def send_mail(body)
    require "mail"
    mail = Mail.new do
      from    "from@from.com"
      to      "to@addr.com"
      subject "件名"
      body    body
    end

    mail.delivery_method :smtp, {
      enable_starttls_auto: false,
    }
    mail.charset = 'UTF-8'
    mail.deliver!
end

def print_html()
    print "Content-Type: text/html\n\n"
    print "mail sent."
end

body = get_body()
send_mail(body)
print_html()

.htaccess

.htaccess
Options +ExecCGI
AddHandler cgi-script .rb
  • /etc/apache2/mods-enabled/cgi.load <- 存在すること
  • /etc/apache2/mods-enabled/mime.load <- 存在すること
  • /etc/apache2/mods-enabled/mime.conf <- 一応中身確認

Premature end of script headers

  • \n\n¥n¥nになっていたのが原因。
    • cliから ./sendmail.rb で出力結果をみればもっと早く気がついた・・・
4
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?