gpt4 book ai didi

ruby-on-rails - 电子邮件未在 RoR 应用程序中发送

转载 作者:太空宇宙 更新时间:2023-11-03 18:17:02 25 4
gpt4 key购买 nike

我想从我的 ruby​​ on rails 应用程序发送邮件,我按照以下步骤操作:

首先,我使用以下命令创建邮件程序:

rails generate mailer UserMailer

我在 user_mailer.rb 页面中执行以下操作:

class UserMailer < ActionMailer::Base
default from: "xxxxx@xxxxx.com"

def schedule
@mail_to = 'abc@gmail.com' #params[:Schedule][:Appointments]
mail :to => @mail_to, :subject => "Hi"
end
end

schedule是我的 View 页面schedule.html.erb:

<%= form_for(:Schedule,:html => {:id => "Schedule"}) do |f| %>
<table class="table" width="100%">
<th><font size="+1">Interview Schedule</font></th>
</table>
<table>
<tr>
<td>
<label style="font-size:small; font-weight:bold;">Send Appointments to:</label>
</td>
<td>
<%= f.text_field :mail_to_address, :style => "font-size:small;" %>
</td>
</tr>
<tr>
<td style="vertical-align:top;">
<label style="font-size:small; font-weight:bold;">Message:</label>
</td>
<td>
<%= f.text_area :message, :style => "font-size:small; width:300px; height:100px;" %>
</td>
</tr>
<tr>
<td>
</td>
<td>
<%= f.submit "Send", { :style => "font-size:small" } %>
</td>
</tr>
</table>
<% end %>

在上面的代码中,mail_to_address text_field 我把电子邮件地址放在我想发送电子邮件的地方,并在 message text_field 中输入一些文本,然后点击 Send 按钮,但邮件未发送。为什么?

下面是environment.rb页面代码:

# Load the Rails application.
require File.expand_path('../application', __FILE__)

# Initialize the Rails application.
VideoResume::Application.initialize!
ActionMailer::Base.delivery_method = :smtp
ActionMailer::Base.smtp_settings = {
:address => "smtp.gmail.com",
:port => 587,
:domain => "gmail.com",
:authentication => :login,
:charset => 'utf-8',
:user_name => "abc@gmail.com",
:password => "password"
}

在我的 App/config/environments/development.rb 中我有:

VideoResume::Application.configure do
# Settings specified here will take precedence over those in config/application.rb.

# In the development environment your application's code is reloaded on
# every request. This slows down response time but is perfect for development
# since you don't have to restart the web server when you make code changes.
config.cache_classes = false

# Do not eager load code on boot.
config.eager_load = false

# Show full error reports and disable caching.
config.consider_all_requests_local = true
config.action_controller.perform_caching = false

# Don't care if the mailer can't send.
config.action_mailer.raise_delivery_errors = false

# Print deprecation notices to the Rails logger.
config.active_support.deprecation = :log

# Raise an error on page load if there are pending migrations.
config.active_record.migration_error = :page_load

# Debug mode disables concatenation and preprocessing of assets.
# This option may cause significant delays in view rendering with a large
# number of complex assets.
config.assets.debug = true

# Adds additional error checking when serving assets at runtime.
# Checks for improperly declared sprockets dependencies.
# Raises helpful error messages.
config.assets.raise_runtime_errors = true

config.action_mailer.perform_deliveries = true
config.action_mailer.raise_delivery_errors = true
# Raises error for missing translations
# config.action_view.raise_on_missing_translations = true
end

请建议我应该怎么做,等待回复。谢谢

最佳答案

从上面的评论来看,我认为问题是您没有调用邮件程序。正如 @mixian945@Sachin 所指出的,您需要调用您的邮件程序。

假设您想通过 Controller 中的create_schedule 方法 发送邮件,然后您可以通过

发送邮件
def create_schedule
# your method logic
UserMailer.schedule.deliver #this will call your schedule method in UserMailer class and hence send your mail
end

def schedule
mail to: "abc@gmail.com", subject: "Hi"
end

此外,如果您想将用户 ID 或其他内容发送到您的交付方法,那么您可以像这样调用您的调度方法

def create_schedule
@user = User.find(params[:id]) #your logic to find your user
# your method logic
UserMailer.schedule.deliver(@user.email)
end

这将允许您使用自己的方法

def schedule (email)
mail :to => email, :subject => "Hi"
end

详情请引用Action Mailer Basics

关于ruby-on-rails - 电子邮件未在 RoR 应用程序中发送,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24547000/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com