我已经像这样设置了我的环境:
echo "export SENDGRID_API_KEY='xxxxxxxxx'" > sendgrid.env
echo "sendgrid.env" >> .gitignore
source ./sendgrid.env
Sendgrid gem 已安装。
我尝试运行的代码:
require 'sendgrid-ruby'
include SendGrid
require 'json'
def hello_world
from = Email.new(email: 'test@example.com')
to = Email.new(email: 'test@example.com')
subject = 'Sending with SendGrid is Fun'
content = Content.new(type: 'text/plain', value: 'and easy to do anywhere, even with Ruby')
mail = Mail.new(from, subject, to, content)
#previous version
#sg = SendGrid::API.new(api_key: ENV['xxxxxxx'])
#current version
sg = SendGrid::API.new(api_key: ENV['SENDGRID_API_KEY'])
response = sg.client.mail._('send').post(request_body: mail.to_json)
puts response.status_code
puts response.body
puts response.headers
end
hello_world
但我收到以下错误:
no implicit conversion of nil into String
在这个文件里面:
/ruby/gems/2.4.0/gems/sendgrid-ruby-5.2.0/lib/sendgrid/client.rb:24:in
我无法弄清楚这里出了什么问题......
完整错误:
/Users/pimzonneveld/.rbenv/versions/2.4.3/lib/ruby/gems/2.4.0/gems/sendgrid-ruby-5.2.0/lib/sendgrid/client.rb:24:in `+': no implicit conversion of nil into String (TypeError)
from /Users/pimzonneveld/.rbenv/versions/2.4.3/lib/ruby/gems/2.4.0/gems/sendgrid-ruby-5.2.0/lib/sendgrid/client.rb:24:in `initialize'
from app/helpers/mail.rb:12:in `new'
from app/helpers/mail.rb:12:in `hello_world'
from app/helpers/mail.rb:19:in `<main>'