诗歌rails 之使用Rails Action Mailer发送SMTP邮件 基础教程
链接如果,你是在找
553 You are not authorized to send mail, authentication is required 这个问题的原因,请跳这里,Rails smtp 邮件出错时
Action Mailer 是Rails的一个组件用来发送接收邮件,下面将演示,如何使用它创建SMTP邮件。从命令创建Rails工程开始:
Action Mailer 配置
smtp邮件发送,首先需要配置,在config的environment.rb最后
添加
这个配置是指定使用smtp,下面具体的使用参数:
也要在environment.rb中配置
以上配需要根据实际情况写,端口验证方式和发送类型。同样,配置也可以是
接着是生成mailer命令如下:
生成文件:
我们需要在添加方法:
其中, recipient, subject, message 和 sent_at是关于发送的参数,同时我们还有六个标准参数
*@subject主题.
* @body 是Ruby的hash结构。你可以创建三个值对title, email, message
* @recipients接受邮件的地址列表
* @from发件人地址列表.
* @sent_on发送时间.
* @headers是另外的hash结构标识header信息如,配置MIME typeplain text 或 HTML
然后,创建发送模板
修改如下文件app/views/contact.rhtml
创建对应controller处理逻辑
在 emailer_controller.rb中调用上面创建的Model实现发送逻辑:
发送邮件,需要添加deliver_到发送的对应方法前。
request.xhr?是用阿里处理Rails Java Script(RJS)当浏览器不支持JavaScript时可以发送text信息。
下面是发送内容的输入界面,首先在emailer_controller.rb添加相应方法
在app\views\emails\index.rhtml文件中设计输入界面
然后,http://127.0.0.1:3000/Emailer/inde页面测试
如下:
点击发送,就应该看到成功提示
553 You are not authorized to send mail, authentication is required 这个问题的原因,请跳这里,Rails smtp 邮件出错时
Action Mailer 是Rails的一个组件用来发送接收邮件,下面将演示,如何使用它创建SMTP邮件。从命令创建Rails工程开始:
- C:\ruby\> rails emails
Action Mailer 配置
smtp邮件发送,首先需要配置,在config的environment.rb最后
添加
- ActionMailer::Base.delivery_method = :smtp
这个配置是指定使用smtp,下面具体的使用参数:
也要在environment.rb中配置
- ActionMailer::Base.server_settings = {
- :address => "smtp.tutorialspoint.com",
- :port => 25,
- :domain => "tutorialspoint.com",
- :authentication => :login,
- :user_name => "username",
- :password => "password",
- }
以上配需要根据实际情况写,端口验证方式和发送类型。同样,配置也可以是
- ActionMailer::Base.default_content_type = "text/html"#其中type可以是"text/plain", "text/html", 和 "text/enriched"."text/plain"是默认
接着是生成mailer命令如下:
- C:\ruby\> cd emails
- C:\ruby\emails> ruby script/generate mailer Emailer
生成文件:
- class Emailer < ActionMailer::Base
- end
我们需要在添加方法:
- class Emailer < ActionMailer::Base
- def contact(recipient, subject, message, sent_at = Time.now)
- @subject = subject
- @recipients = recipient
- @from = 'no-reply@yourdomain.com'
- @sent_on = sent_at
- @body["title"] = 'This is title'
- @body["email"] = 'sender@yourdomain.com'
- @body["message"] = message
- @headers = {}
- end
- end
其中, recipient, subject, message 和 sent_at是关于发送的参数,同时我们还有六个标准参数
引用
*@subject主题.
* @body 是Ruby的hash结构。你可以创建三个值对title, email, message
* @recipients接受邮件的地址列表
* @from发件人地址列表.
* @sent_on发送时间.
* @headers是另外的hash结构标识header信息如,配置MIME typeplain text 或 HTML
然后,创建发送模板
修改如下文件app/views/contact.rhtml
- Hi!
- You are having one email message from <%= @email %> with a tilte
- <%= @title %>
- and following is the message:
- <%= @message %>
- Thanks
创建对应controller处理逻辑
- C:\ruby\emails> ruby script/generate controller Emailer
在 emailer_controller.rb中调用上面创建的Model实现发送逻辑:
- class EmailerController < ApplicationController
- def sendmail
- email = @params["email"]
- recipient = email["recipient"]
- subject = email["subject"]
- message = email["message"]
- Emailer.deliver_contact(recipient, subject, message)
- return if request.xhr?
- render :text => 'Message sent successfully'
- end
- end
发送邮件,需要添加deliver_到发送的对应方法前。
request.xhr?是用阿里处理Rails Java Script(RJS)当浏览器不支持JavaScript时可以发送text信息。
下面是发送内容的输入界面,首先在emailer_controller.rb添加相应方法
- def index
- render :file => 'app\views\emailer\index.rhtml'
- end
在app\views\emails\index.rhtml文件中设计输入界面
- <h1>Send Email</h1>
- <%= start_form_tag :action => 'sendmail' %>
- <p><label for="email_subject">Subject</label>:
- <%= text_field 'email', 'subject' %></p>
- <p><label for="email_recipient">Recipient</label>:
- <%= text_field 'email', 'recipient' %></p>
- <p><label for="email_message">Message</label><br/>
- <%= text_area 'email', 'message' %></p>
- <%= submit_tag "Send" %>
- <%= end_form_tag %>
然后,http://127.0.0.1:3000/Emailer/inde页面测试
如下:
点击发送,就应该看到成功提示
浙公网安备 33010602011771号