-
Notifications
You must be signed in to change notification settings - Fork 323
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Mail Helper Refactor - Call for Feedback #169
Comments
@awwa, @pjurewicz, @skalee, @tricknotes, @Gwash3189, @maxcodes, @tkbky, @swifthand, @mootpointer, @brian4d, @alikhan-io, @safetymonkey If you are tagged on this message, it means we are particularly interested in your feedback :) If you don't have the time, no worries and my apologies for the disturbance. If you do have the time, please take a look at the proposed helper upgrade above and let us know what you think. Any and all feedback is greatly appreciated. Thanks in advance! |
Sendgrid docs in Overview section says: In my opinion is a good ideia shows a example to a use case where send email to more than 1K at once. I'm currently solving this with the follow approach: def send_emails(user_type)
# Get all emails that needs to send. Could be more than 1K, so we need to make a
# little bit more configuration
# 'all_recipients' is a array of Personalization objects with their respective
# substitution tags
all_recipients = recipients
sg = SendGrid::API.new(api_key: ENV['SENDGRID_API_KEY'])
all_recipients.each_slice(1000) do |thousand_recipients|
# This is the way to send a Mail object with 1000 recipients and follow Sendgrid recommendations
mail = prepare_email(thousand_recipients)
begin
response = sg.client.mail._('send').post(request_body: mail.to_json)
rescue Exception => e
puts e.message
end
puts '-------------------'
puts "Status: #{response.status_code}"
puts "Body: #{response.body}"
puts "Headers: #{response.headers}"
puts '------------------'
end
puts "#{all_recipients.size} emails sent!"
end
def recipients
recipients = []
User.all.find_each do |user|
personalization = Personalization.new
personalization.add_to(Email.new(email: user.email))
personalization.add_substitution(Substitution.new(key: '-name-', value: user.name))
recipients << personalization
end
recipients
end
def prepare_email(thousand_recipients)
mail = Mail.new
mail.from = Email.new(email: '[email protected]', name: 'Example User')
mail.subject = 'Sending with SendGrid is Fun'
mail.template_id = '12345-6789'
# Add 1000 recipients inside mail object as recommended by Sendgrid
thousand_recipients.each do |recipient|
mail.add_personalization(recipient)
end
mail
end Perhaps this is not the better way, but I will leave my example here, maybe could be useful for some other users |
Thanks @gurgelrenan, My hope is that with the updated design this will be easier, for example, using the Kitchen Sink example, you should not have to manipulate the Personalization object directly. For this release, we are limiting the scope to the use cases described above; however, we will add this follow up use case to the next iteration. Thanks again! With Best Regards, Elmer |
Hi @thinkingserious , I will show my opinion. I would like to hear any comments from other Rubyist about No.3.
require 'base64'
bytes = File.read('/path/to/the/attachment.pdf')
encoded = Base64.encode64(bytes)
msg.add_attachment('attachment.pdf',
encoded,
'application/pdf',
'attachment',
'Balance Sheet')
These three methods are clear and easy to understand. SendGrid::Mail.create_single_email()
SendGrid::Mail.create_single_email_to_multiple_recipients()
SendGrid::Mail.create_multiple_emails_to_multiple_recipients() But these methods can be implemented in one method too. For example: def create_email(from, to, subject, plain_text_content, html_content, substitutions = [])
|
I agree with @awwa. Is there any reason to have all those separate methods? Also, I have some comments regarding naming:
And lastly: My 2c |
Would it be possible to use an options hash (Ruby 1.x) or keyword arguments (Ruby 2.x) instead of positional arguments? For example:
This makes my code self-documenting and much less prone to errors caused by my arguments being out of order. It also lets you incorporate optional parameters:
Also, a lot of Ruby interfaces allow some parameters to optionally be an array. The advantage is that your API syntax is easier to read and remember. @awwa mentioned this in #3 of his comments. Just wanted to add +1 to his suggestion. For example, the "Single Email to Multiple Recipients" could just be:
One last small suggestion. Most Ruby code seems to follow the convention of writing "set_" methods as "attr_name=". For example:
|
Fantastic feedback @brian4d! I'll have a link to some swag for you shortly, thanks! Also, I'll be spending some deeper time later to properly address all the great feedback here and make adjustments to the proposal. |
@gurgelrenan please take a moment to fill out this form so we can send you some swag. Thanks! |
Hi, @thinkingserious asked me to post my comments from #175 here, so here they are, in a new and improved version.
John |
Thanks @jjb! |
Review this blog post with regards to error handling: https://betta.io/blog/2018/03/30/graceful-errors-in-ruby-sdks |
Since there has been no activity on this issue since March 1, 2020, we are closing this issue. Please feel free to reopen or create a new issue if you still require assistance. Thank you! |
Hello!
It is now time to implement the final piece of our v2 to v3 migration. Before we dig into writing the code, we would love to get feedback on the proposed interface.
The text was updated successfully, but these errors were encountered: