我正在尝试发送电子邮件,并按照本教程
这就是我的进展方式:
我的emailer.rb
class Emailer < ActionMailer::Base
default from: "r.karoui@gmail.com"
def emprunt_mail(user)
@user = user
@url = 'http://example.com/login'
mail(to: @user.email, subject: 'Emprunt de livre')
end
end
my emrunt_mail.erb.html
<!DOCTYPE html>
<html>
<head>
<meta content='text/html; charset=UTF-8' http-equiv='Content-Type' />
</head>
<body>
<h1>Bonjour, <%= current_user.f_name %></h1>
<p>
Vous venez d'emprunter un livre
</p>
<p>
vous avez une durée d'une semaine pour le rendre !
</p>
<p>Bonne journée!</p>
</body>
</html>
在我的books_controller.rb
中def emprunter
if (@book.state == false)
if @book.update(state: 1)
flash[:notice]='Vous aver un délai de 7 jours pour rendre le livre'
Emailer.create_emprunt_mail(current_user)
Emailer.deliver_emprunt_mail(current_user)
redirect_to books_list_path
else
redirect_to admin_manage_path
end
else
flash[:notice]='Ce livre est déja pris'
redirect_to books_list_path
end
end
我将此配置添加到mt environment / developpment.rb
config.assets.raise_runtime_errors = true
config.action_mailer.raise_delivery_errors =true
config.action_mailer.default_url_options ={ :host => 'localhost:3000'}
config.action_mailer.smtp_settings= {
adress: "smtp.gmail.com",
port: "587",
domain: "gmail.com",
authentication: "plain",
enable_startlls_auto: true,
user_name: 'USERNAME',
password: '*********'
}
我没有添加任何路线 strage bihavior是在emprunter方法中传递给第一个if的else,它应该进入块if。
更新
在我的服务器中,我现在有了这个
“呈现的电子邮件/ emprunt_mail.html.erb(0.0ms)
Emailer#emprunt_mail: processed outbound mail in 4.1ms
Sent mail to rawia.seller@gmail.com (3.3ms)
Date: Tue, 11 Nov 2014 11:40:31 +0100
From: karoui.rawia@gmail.com
To: rawia.seller@gmail.com
Message-ID: <5461e79fd9674_4b33ffe529d82a44d0@imac-de-imac.home.mail>
Subject: Emprunt de livre
Mime-Version: 1.0
Content-Type: text/html;
charset=UTF-8
Content-Transfer-Encoding: quoted-printable
<!DOCTYPE html>
<html>
<head>
<meta content=3D'text/html; charset=3DUTF-8' http-equiv=3D'Content-Type=
' />
</head>
<body>
<h1>Bonjour, </h1>
<p>
Vous venez d'emprunter un livre
</p>
<p>
vous avez une dur=C3=A9e d'une semaine pour le rendre !
</p>
<p>Bonne journ=C3=A9e!</p>
</body>
</html>=
Redirected to http://localhost:3000/books/show
Completed 302 Found in 14ms (ActiveRecord: 1.2ms)
但是当我检查邮件时我没有找到任何新邮件!请告诉我问题出在哪里
这是environment / developpment.rb中的新配置:
config.action_mailer.default_url_options = { :host => 'localhost:3000' }
# ActionMailer Config
# Setup for production - deliveries, no errors raised
config.action_mailer.delivery_method = :smtp
config.action_mailer.perform_deliveries = true
config.action_mailer.raise_delivery_errors = false
config.action_mailer.default :charset => "utf-8"
config.action_mailer.smtp_settings= {
adress: "smtp.gmail.com",
port: 587,
domain: "gmail.com",
authentication: "plain",
enable_starttls_auto: true,
user_name: 'username',
password: '*****'
}