|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require_relative "../lib/resend" |
| 4 | + |
| 5 | +raise if ENV["RESEND_API_KEY"].nil? |
| 6 | + |
| 7 | +Resend.api_key = ENV["RESEND_API_KEY"] |
| 8 | + |
| 9 | +puts "=== Listing Received Emails ===" |
| 10 | + |
| 11 | +puts "\nListing all received emails:" |
| 12 | +emails = Resend::Emails::Receiving.list |
| 13 | + |
| 14 | +puts "Total emails in response: #{emails[:data].length}" |
| 15 | +puts "Has more: #{emails[:has_more]}" |
| 16 | + |
| 17 | +emails[:data].each do |e| |
| 18 | + puts " - #{e["id"]}: #{e["subject"]} from #{e["from"]}" |
| 19 | + puts " Created: #{e["created_at"]}" |
| 20 | + if e["attachments"] && !e["attachments"].empty? |
| 21 | + puts " Attachments: #{e["attachments"].length}" |
| 22 | + end |
| 23 | +end |
| 24 | + |
| 25 | +puts "\n\nListing with limit of 5:" |
| 26 | +limited_emails = Resend::Emails::Receiving.list(limit: 5) |
| 27 | + |
| 28 | +puts "Retrieved #{limited_emails[:data].length} emails" |
| 29 | +puts "Has more: #{limited_emails[:has_more]}" |
| 30 | + |
| 31 | +# Example of pagination (if you have more emails) |
| 32 | +if limited_emails[:has_more] && limited_emails[:data].last |
| 33 | + last_id = limited_emails[:data].last["id"] |
| 34 | + puts "\n\nGetting next page after ID: #{last_id}" |
| 35 | + next_page = Resend::Emails::Receiving.list(limit: 5, after: last_id) |
| 36 | + puts "Next page has #{next_page[:data].length} emails" |
| 37 | +end |
| 38 | + |
| 39 | +puts "\n\n=== Retrieving Single Received Email ===" |
| 40 | + |
| 41 | +# Use the first email from the list, or specify a known ID |
| 42 | +if emails[:data] && emails[:data].first |
| 43 | + email_id = emails[:data].first["id"] |
| 44 | +else |
| 45 | + # Replace with an actual received email ID from your account |
| 46 | + email_id = "006e2796-ff6a-4436-91ad-0429e600bf8a" |
| 47 | +end |
| 48 | + |
| 49 | +email = Resend::Emails::Receiving.get(email_id) |
| 50 | + |
| 51 | +puts "\nEmail Details:" |
| 52 | +puts " ID: #{email[:id]}" |
| 53 | +puts " From: #{email[:from]}" |
| 54 | +puts " To: #{email[:to].join(', ')}" |
| 55 | +puts " Subject: #{email[:subject]}" |
| 56 | +puts " Created At: #{email[:created_at]}" |
| 57 | +puts " Message ID: #{email[:message_id]}" |
| 58 | + |
| 59 | +if email[:cc] && !email[:cc].empty? |
| 60 | + puts " CC: #{email[:cc].join(', ')}" |
| 61 | +end |
| 62 | + |
| 63 | +if email[:bcc] && !email[:bcc].empty? |
| 64 | + puts " BCC: #{email[:bcc].join(', ')}" |
| 65 | +end |
| 66 | + |
| 67 | +if email[:attachments] && !email[:attachments].empty? |
| 68 | + puts "\n Attachments:" |
| 69 | + email[:attachments].each do |attachment| |
| 70 | + puts " - #{attachment["filename"]} (#{attachment["content_type"]})" |
| 71 | + puts " ID: #{attachment["id"]}" |
| 72 | + puts " Size: #{attachment["size"]} bytes" if attachment["size"] |
| 73 | + puts " Content ID: #{attachment["content_id"]}" if attachment["content_id"] |
| 74 | + end |
| 75 | + |
| 76 | + puts "\n Listing all attachments for email: #{email[:id]}" |
| 77 | + attachments_list = Resend::Attachments::Receiving.list( |
| 78 | + email_id: email[:id] |
| 79 | + ) |
| 80 | + |
| 81 | + puts " Total attachments: #{attachments_list[:data].length}" |
| 82 | + puts " Has more: #{attachments_list[:has_more]}" |
| 83 | + |
| 84 | + # Retrieve full attachment details for the first attachment |
| 85 | + if email[:attachments].first |
| 86 | + first_attachment_id = email[:attachments].first["id"] |
| 87 | + puts "\n Retrieving full attachment details for: #{first_attachment_id}" |
| 88 | + |
| 89 | + attachment_details = Resend::Attachments::Receiving.get( |
| 90 | + id: first_attachment_id, |
| 91 | + email_id: email[:id] |
| 92 | + ) |
| 93 | + |
| 94 | + puts " Download URL: #{attachment_details[:download_url]}" |
| 95 | + puts " Expires At: #{attachment_details[:expires_at]}" |
| 96 | + end |
| 97 | +end |
| 98 | + |
| 99 | +puts "\n HTML Content:" |
| 100 | +puts " #{email[:html][0..100]}..." if email[:html] |
| 101 | + |
| 102 | +puts "\n Text Content:" |
| 103 | +puts " #{email[:text]}" if email[:text] |
0 commit comments