将 sendgrid-ruby 库作为 ruby 脚本获取到 运行 的问题
Issues with getting sendgrid-ruby library to run as ruby script
我正在尝试让 sendgrid-ruby 库发送电子邮件,我已经设法编写了一个脚本,我可以 运行 逐行开始工作。但是,当我尝试 运行 整个脚本时,我得到
/Users/andrewaccuardi/.rvm/gems/ruby-2.4.1/gems/sendgrid-ruby-5.1.0/lib/sendgrid/client.rb:24:in `+': no implicit conversion of nil into String (TypeError)
这是我用于调用的代码:
require 'sendgrid-ruby'
include SendGrid
sg = SendGrid::API.new(api_key: ENV['SENDGRID_API_KEY'])
email = SendGrid::Mail.new
email.from = SendGrid::Email.new(email: "example@email.com", name: "Example User")
email.subject = "Your Email's Subject"
p = SendGrid::Personalization.new
p.add_to(SendGrid::Email.new(email: "example_email", name: "example_name"))
p.add_substitution(Substitution.new(key: '-companyName-', value: 'Example_company'))
p.add_substitution(Substitution.new(key: '-eventName-', value: 'DINOSAUR PARTY'))
p.add_substitution(Substitution.new(key: '-eventDays-', value: '35'))
p.add_substitution(Substitution.new(key: '-eventStartDate-', value: '08/15/1993'))
p.add_substitution(Substitution.new(key: '-eventEndDate-', value: '12/22/1992'))
p.add_substitution(Substitution.new(key: '-eventStartTime-', value: '12:30pm'))
p.add_substitution(Substitution.new(key: '-eventEndTime-', value: '12:30am'))
p.add_substitution(Substitution.new(key: '-eventAddress-', value: '1234 SE Main St.'))
p.add_substitution(Substitution.new(key: '-eventYears-', value: '3'))
p.add_substitution(Substitution.new(key: '-eventWebsite-', value: 'www.example.com'))
p.add_substitution(Substitution.new(key: '-eventFacebook-', value: 'facebook.com/example'))
p.add_substitution(Substitution.new(key: '-eventTwitter-', value: 'twitter.com/example'))
p.add_substitution(Substitution.new(key: '-eventOtherMedia-', value: 'othermedia.com/example'))
p.add_substitution(Substitution.new(key: '-eventDescription-', value: "Vape shoreditch fixie man bun vexillologist VHS. You probably haven't heard of them stumptown edison bulb, echo park narwhal +1 skateboard portland poutine pok pok bitters activated charcoal pinterest. Mlkshk 90's jean shorts kale chips four dollar toast glossier. Ennui semiotics brunch twee man braid lumbersexual chia plaid. Crucifix kogi tofu cardigan. Roof party sustainable jean shorts umami asymmetrical dreamcatcher. Offal biodiesel edison bulb tofu raw denim cred. Kogi banh mi shoreditch."))
p.add_substitution(Substitution.new(key: '-eventAttendance-', value: '50+'))
p.add_substitution(Substitution.new(key: '-eventCategory-', value: 'Entertainment'))
p.add_substitution(Substitution.new(key: '-eventGender-', value: 'Female'))
p.add_substitution(Substitution.new(key: '-eventAge-', value: '21+'))
p.add_substitution(Substitution.new(key: '-eventRequest-', value: 'Beer'))
p.add_substitution(Substitution.new(key: '-sellingRequest-', value: 'Email Blast'))
p.add_substitution(Substitution.new(key: '-repsNeeded-', value: '5'))
p.add_substitution(Substitution.new(key: '-tapsNeeded-', value: '10'))
p.add_substitution(Substitution.new(key: '-cupsNeeded-', value: '15'))
p.add_substitution(Substitution.new(key: '-alcoholPermit-', value: 'Yeah, buddy'))
p.add_substitution(Substitution.new(key: '-food-', value: 'Pizza'))
p.add_substitution(Substitution.new(key: '-promoOpps-', value: 'Lots'))
p.add_substitution(Substitution.new(key: '-nonProfName-', value: 'Non-profit example name'))
p.add_substitution(Substitution.new(key: '-nonProfWebsite-', value: 'www.nonprofitexample.com'))
p.add_substitution(Substitution.new(key: '-nonProfEmail-', value: 'examplenonprofit@email.com'))
p.add_substitution(Substitution.new(key: '-nonProfPhone-', value: '555-555-3331'))
email.add_personalization(p)
email.template_id = "684f302c-5669-4114-a953-c3e15d6de44d"
response = sg.client.mail._('send').post(request_body: email.to_json)
puts response.status_code
puts response.body
puts response.headers
任何关于如何前进的见解都会很棒,谢谢!
Rails 控制台加载环境变量,因此当从那里执行代码时,ENV['SENDGRID_API_KEY'] 具有值。
当你 运行 它作为脚本而不加载环境时,它的结果为 nil。
根据此处的文档:
https://github.com/sendgrid/sendgrid-ruby#setup-environment-variables
你必须导出这个。
我正在尝试让 sendgrid-ruby 库发送电子邮件,我已经设法编写了一个脚本,我可以 运行 逐行开始工作。但是,当我尝试 运行 整个脚本时,我得到
/Users/andrewaccuardi/.rvm/gems/ruby-2.4.1/gems/sendgrid-ruby-5.1.0/lib/sendgrid/client.rb:24:in `+': no implicit conversion of nil into String (TypeError)
这是我用于调用的代码:
require 'sendgrid-ruby'
include SendGrid
sg = SendGrid::API.new(api_key: ENV['SENDGRID_API_KEY'])
email = SendGrid::Mail.new
email.from = SendGrid::Email.new(email: "example@email.com", name: "Example User")
email.subject = "Your Email's Subject"
p = SendGrid::Personalization.new
p.add_to(SendGrid::Email.new(email: "example_email", name: "example_name"))
p.add_substitution(Substitution.new(key: '-companyName-', value: 'Example_company'))
p.add_substitution(Substitution.new(key: '-eventName-', value: 'DINOSAUR PARTY'))
p.add_substitution(Substitution.new(key: '-eventDays-', value: '35'))
p.add_substitution(Substitution.new(key: '-eventStartDate-', value: '08/15/1993'))
p.add_substitution(Substitution.new(key: '-eventEndDate-', value: '12/22/1992'))
p.add_substitution(Substitution.new(key: '-eventStartTime-', value: '12:30pm'))
p.add_substitution(Substitution.new(key: '-eventEndTime-', value: '12:30am'))
p.add_substitution(Substitution.new(key: '-eventAddress-', value: '1234 SE Main St.'))
p.add_substitution(Substitution.new(key: '-eventYears-', value: '3'))
p.add_substitution(Substitution.new(key: '-eventWebsite-', value: 'www.example.com'))
p.add_substitution(Substitution.new(key: '-eventFacebook-', value: 'facebook.com/example'))
p.add_substitution(Substitution.new(key: '-eventTwitter-', value: 'twitter.com/example'))
p.add_substitution(Substitution.new(key: '-eventOtherMedia-', value: 'othermedia.com/example'))
p.add_substitution(Substitution.new(key: '-eventDescription-', value: "Vape shoreditch fixie man bun vexillologist VHS. You probably haven't heard of them stumptown edison bulb, echo park narwhal +1 skateboard portland poutine pok pok bitters activated charcoal pinterest. Mlkshk 90's jean shorts kale chips four dollar toast glossier. Ennui semiotics brunch twee man braid lumbersexual chia plaid. Crucifix kogi tofu cardigan. Roof party sustainable jean shorts umami asymmetrical dreamcatcher. Offal biodiesel edison bulb tofu raw denim cred. Kogi banh mi shoreditch."))
p.add_substitution(Substitution.new(key: '-eventAttendance-', value: '50+'))
p.add_substitution(Substitution.new(key: '-eventCategory-', value: 'Entertainment'))
p.add_substitution(Substitution.new(key: '-eventGender-', value: 'Female'))
p.add_substitution(Substitution.new(key: '-eventAge-', value: '21+'))
p.add_substitution(Substitution.new(key: '-eventRequest-', value: 'Beer'))
p.add_substitution(Substitution.new(key: '-sellingRequest-', value: 'Email Blast'))
p.add_substitution(Substitution.new(key: '-repsNeeded-', value: '5'))
p.add_substitution(Substitution.new(key: '-tapsNeeded-', value: '10'))
p.add_substitution(Substitution.new(key: '-cupsNeeded-', value: '15'))
p.add_substitution(Substitution.new(key: '-alcoholPermit-', value: 'Yeah, buddy'))
p.add_substitution(Substitution.new(key: '-food-', value: 'Pizza'))
p.add_substitution(Substitution.new(key: '-promoOpps-', value: 'Lots'))
p.add_substitution(Substitution.new(key: '-nonProfName-', value: 'Non-profit example name'))
p.add_substitution(Substitution.new(key: '-nonProfWebsite-', value: 'www.nonprofitexample.com'))
p.add_substitution(Substitution.new(key: '-nonProfEmail-', value: 'examplenonprofit@email.com'))
p.add_substitution(Substitution.new(key: '-nonProfPhone-', value: '555-555-3331'))
email.add_personalization(p)
email.template_id = "684f302c-5669-4114-a953-c3e15d6de44d"
response = sg.client.mail._('send').post(request_body: email.to_json)
puts response.status_code
puts response.body
puts response.headers
任何关于如何前进的见解都会很棒,谢谢!
Rails 控制台加载环境变量,因此当从那里执行代码时,ENV['SENDGRID_API_KEY'] 具有值。
当你 运行 它作为脚本而不加载环境时,它的结果为 nil。
根据此处的文档:
https://github.com/sendgrid/sendgrid-ruby#setup-environment-variables
你必须导出这个。