以下サンプルコードを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 で出力結果をみればもっと早く気がついた・・・