Rails 如何查看 SendCloud 返回的状态码

luckyyang · 2014年10月17日 · 最后由 luckyyang 回复于 2015年01月28日 · 4882 次阅读

sendcloud 给的官方例子是在下面。rails(4.1)下是通过 actionmailer 来发送邮件的,那如何在 rails 当中来查看 reponse 呢?

#!/usr/bin/env ruby
require 'rubygems'
require 'rest_client'

def send_attachment_mail
  response = RestClient.post "https://sendcloud.sohu.com/webapi/mail.send.xml",
  :api_user => "[email protected]",
  :api_key => "password",
  :from => "[email protected]",
  :fromname => "from名称",
  :to => "[email protected];[email protected]",
  :subject => "ruby 调用WebAPI测试主题",
  :html => '欢迎使用<a href="https://sendcloud.sohu.com">SendCloud</a>',
  :attachment1 => File.new("/path/to/doc/操作指南v2.6.pdf", 'rb'),
  :attachment2 => File.new("/path/to/doc/WEB API文档.pdf", 'rb')
  return response
end

response = send_attachment_mail 
puts response.code
puts response.to_str

UPDATE:大家在使用 sendcloud 的时候还是有很多问题啊,我把我的 demo 放上来做参考吧:https://github.com/luckyyang/sendcloud-demo

有需求的话我可以考虑录期视频。

发完了往 Log 文件里面写

这么写就可以:

response = mail(:to => @user.email, :subject => "#{@user.nick},毛豆网有新的学习目标发布")

puts response

#2 楼 @luckyyang 我最近野在用,基本使用是成功了,但是不知道怎么配置进 rails,本身可以单独使用,但我想结合 rails 的配置。

# gamil
  config.action_mailer.delivery_method = :smtp
  config.action_mailer.smtp_settings = {
    address:              'smtp.gmail.com',
    port:                 587,
    domain:               'example.com',
    user_name:            '<username>',
    password:             '<password>',
    authentication:       'plain',
    enable_starttls_auto: true
  }

怎么结合下面的对照写上面的?

#!/usr/bin/env ruby
require 'rubygems'
require 'rest_client'

def send_attachment_mail
  response = RestClient.post "https://sendcloud.sohu.com/webapi/mail.send.xml",
  :api_user => "[email protected]",
  :api_key => "password",
  :from => "[email protected]",
  :fromname => "from名称",
  :to => "[email protected];[email protected]",
  :subject => "ruby 调用WebAPI测试主题",
  :html => '欢迎使用<a href="https://sendcloud.sohu.com">SendCloud</a>',
  return response
end

response = send_attachment_mail 
puts response.code
puts response.to_str

能给一个例子最好了。谢谢

如果是使用 SMTP 服务,是拿不到返回状态码的,前几天用 SMTP 服务的时候,被坑惨了,跟 SendCloud 的工程师一起调试了个把小时。

SendCloud 用私有方式扩展了 SMTP 协议,如果邮件样本报备不合规,SMTP 返回状态码,但是这么做会被 Ruby 的 SMTP 库当作异常处理,抛出一个诡异的错误,然后无从查证,最后锁定问题到邮件样本匹配上,但是又不给你标准,只告诉你说邮件样本匹配判断很严格......

#4 楼 @lgn21st 邮件样本我记得是 553,我的通常匹配度都挺高的. 那意味着目前最好自己 post 方式发送吗?

#4 楼 @lgn21st 他们陪你调试,真是太优待你了。。。我就说拿不到状态码嘛,他们还说能拿到,因为他们对 rails 不懂。

他们的邮件样本太坑爹了,浪费了我很多小时去调试,有时候看程序就是没问题,最后才知道是样本的问题。

可以让 sendcloud 放开邮件样本的审核的,这样发邮件就和邮件样本没关系了,直接发。一般人我不告诉他。

#7 楼 @luckyyang 我也是没有办法,第一次联系对方,贴给对方错误日志,对方可能也不懂 Rails,随便搜了个 timeout 超时重试的方案给我,我一看居然是在 RubyChina 上搜到的...... 遂联系对方说我就是 RubyChina 的管理员,这个明显不是超时重试就能解决。大概 10 月 8 日联系 SendCloud,10 月 20 日才有回复,调试无果后,就是按照你说的办法,放开样本审核解决了。

#7 楼 @luckyyang 怎么让 sendcloud 放开审核?发邮件给官方团队卖萌么?

用 sendcloud authentication 用 plain 还是 login 呢?我这边老是出现 Net::SMTPAuthenticationError: 500 Unrecognized command

#9 楼 @cassiuschen 卖萌和卖身都可以

#10 楼 @steven_yue config/initializers/setup_mail.rb的所有代码

ActionMailer::Base.smtp_settings = {
  :address                        => "smtpcloud.sohu.com",
  :port                                => 25,
  :user_name                    => Setting.sendcloud.user_name,
  :password                      => Setting.sendcloud.password,
  :authentication       => "login"
}

ActionMailer::Base.default_url_options[:host] = "maodou.io"
13 楼 已删除
14 楼 已删除
需要 登录 后方可回复, 如果你还没有账号请 注册新账号