Skip to content
This repository has been archived by the owner on Feb 25, 2024. It is now read-only.

Commit

Permalink
Updated Mailinator to support Private Domain (#13)
Browse files Browse the repository at this point in the history
Support private domain and custom options
  • Loading branch information
mattrobbins authored and ainformatico committed Jul 3, 2018
1 parent 9877be3 commit e661b4d
Show file tree
Hide file tree
Showing 15 changed files with 283 additions and 69 deletions.
36 changes: 23 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,24 +40,27 @@ Access to an email
email = Mailinator::Email.get('email-abcd1234')
```

Access to a private email

```ruby
email = Mailinator::Email.get('email-abcd1234', private_domain: true)
```

This comment has been minimized.

Copy link
@kookeygam

kookeygam Jan 24, 2023

Hello


Now you have access to some methods:

* `email.id`
* `email.from_full`
* `email.date`
* `email.received`
* `email.from`
* `email.to`
* `email.subject`
* `email.request_id`
* `email.body`
* `email.body_html`
* `email.read?`
* `email.inbox_fetches_left`
* `email.email_fetches_left`
* `email.sender`
* `email.reply_to`
* `email.from`
* `email.ip`
* `email.received`
* `email.date`
* `email.body_base_64`
* `email.orig_from`
* `email.id`
* `email.time`
* `email.forwards_left`
* `email.original`, original request
* `email.seconds_ago`

### Inbox

Expand Down Expand Up @@ -88,6 +91,13 @@ To delete an `Email` you need:
inbox.messages.first.delete
```

Access to a private inbox
```ruby
inbox = Mailinator::Inbox.get('inbox-abcd1234', private_domain: true)
```

The 'download' and 'delete' work the same with private domain.

## Contributing

1. Fork it ( https://github.com/ainformatico/mailinator/fork )
Expand Down
14 changes: 7 additions & 7 deletions lib/mailinator/api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def generate_url(url)
end

def generate_params(params)
URI.encode_www_form(params.merge(token: token))
URI.encode_www_form(params.merge({token: token}))
end

def base_url
Expand All @@ -48,12 +48,12 @@ def base_url

def handle_response(response)
case response.code.to_i
when 200
JSON.parse(response.body)
when 404
raise NotFound
else
raise RequestError, status: response.message, code: response.code
when 200
JSON.parse(response.body)
when 404
fail NotFound
else
fail RequestError, {status: response.message, code: response.code}
end
end
end
Expand Down
5 changes: 3 additions & 2 deletions lib/mailinator/email.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
module Mailinator
class Email
class << self
def get(id)
def get(id, opts = {})
api = Api.new
data = api.get('email', msgid: id)
params = opts.merge(msgid: id)
data = api.get('email', params)

populate(data)
end
Expand Down
5 changes: 3 additions & 2 deletions lib/mailinator/inbox.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
module Mailinator
class Inbox
class << self
def get(to)
def get(to, opts = {})
api = Api.new
data = api.get('inbox', to: to)
params = opts.merge(to: to)
data = api.get('inbox', params)

populate(data)
end
Expand Down
30 changes: 10 additions & 20 deletions lib/mailinator/models/email.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,31 +7,21 @@ module Models
class Email < Base
def transform_data
{
id: @data['data']['id'],
from_full: @data['data']['fromfull'],
date: DateTime.parse(@data['data']['headers']['date']),
received: @data['data']['headers']['received'],
from: @data['data']['headers']['from'],
to: @data['data']['headers']['to'],
subject: @data['data']['subject'],
request_id: @data['data']['requestId'],
body: @data['data']['parts'].first['body'],
body_html: retrieve_body_html,
inbox_fetches_left: @data['apiInboxFetchesLeft'],
email_fetches_left: @data['apiEmailFetchesLeft'],
forwards_left: @data['forwardsLeft'],
sender: @data['data']['headers']['sender'],
from: @data['data']['from'],
date: DateTime.parse(@data['data']['headers']['date']),
body_base_64: @data['data']['parts'].last['body'],
orig_from: @data['data']['origfrom'],
id: @data['data']['id'],
time: @data['data']['time'],
ip: @data['data']['ip'],
to: @data['data']['headers']['to'],
reply_to: @data['data']['headers']['reply-to'],
received: @data['data']['headers']['received'],
read?: @data['data']['been_read']
seconds_ago: @data['data']['seconds_ago']
}
end

private

def retrieve_body_html
html = @data['data']['parts'][1] || {}
html['body']
end
end
end
end
4 changes: 2 additions & 2 deletions lib/mailinator/models/entry.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
module Mailinator
module Models
class Entry < Base
def download
email = Mailinator::Email.get(id)
def download(opts = {})
email = Mailinator::Email.get(id, opts)
if block_given?
yield email
else
Expand Down
41 changes: 39 additions & 2 deletions spec/fixtures/email-plain.response
Original file line number Diff line number Diff line change
@@ -1,5 +1,42 @@
HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
Date: Sun, 28 Dec 2014 13:39:51 GMT
Date: Mon, 14 May 2018 13:39:51 GMT

{"apiInboxFetchesLeft":696,"apiEmailFetchesLeft":10,"data":{"headers":{"sender":"example@example.net","content-type":"multipart\/alternative;\r\n\tboundary=\"MIMEBoundaryec1dc8828d9bbedf095f9e9d2ebdbdeb\"","to":"recipient@mailinator.com","x-connecting-ip":"127.0.0.1","subject":"This is a subject","mime-version":"1.0","x-received-time":"1419696967076","reply-to":"example@example.net","received":"from 127-0-0-1.dynamic-ip.example.net ()\r\n by mail.mailinator.com with SMTP (Postfix)\r\n for recipient@mailinator.com;\r\n Sat, 27 Dec 2014 16:16:07 +0000 (UTC)","from":"\"Sender\" <example@example.net>","date":"Sun, 28 Dec 2014 00:16:00 +0800"},"seconds_ago":77024,"id":"1419696967-44152505-recipient","to":"recipient@mailinator.com","time":1419696967076,"subject":"This is a subject","fromfull":"example@example.net","parts":[{"headers":{"content-type":"text\/plain;\r\n\tcharset=\"iso-8859-1\"","content-transfer-encoding":"quoted-printable"},"body":"This is a body"}],"been_read":false,"from":"Sender","ip":"127.0.0.1"},"forwardsLeft":10}
{
"data": {
"fromfull": "example@example.net",
"headers": {
"date": "Mon, 14 May 2018 00:47:55 -0600 (MDT)",
"mime-version": "1.0",
"x-virus-scanned": "ClamAV using ClamSMTP",
"subject": "This is a Subject",
"message-id": "<message.id@example.com>",
"received": ["from 127-0-0-1.dynamic-ip.example.net ()\r\n by mail.mailinator.com with SMTP (Postfix)\r\n for recipient@mailinator.com;\r\n Sat, 27 Dec 2014 16:16:07 +0000 (UTC)"],
"from": "\"Sender\" <example@example.net>",
"content-type": "multipart/mixed; boundary=\"----=_part_50750_324040880.1526280475585\"",
"to": "<recipient@mailinator.com>",
"x-mailinator-smtp": ["system: start;0", "s: 220 mail.mailinator.com esmtp postfix;0", "r: ehlo bmta1.somemail.com;102", "s: 250-mail.mailinator.com 250-8bitmime 250 ok;102", "r: mail from:<exampl@example.net>;194", "s: 250 ok;194", "r: rcpt to:<recipient@mailinator.com>;286", "s: 250 ok;286", "r: data;377", "s: 354 end data with <cr><lf>.<cr><lf>;377", "s: 250 ok;6137"]
},
"subject": "This is a Subject",
"parts": [{
"headers": {
"content-transfer-encoding": "7bit",
"content-type": "text/html; charset=\"utf-8\""
},
"body": "This is a body"
}, {
"headers": {
"content-transfer-encoding": "base64",
"content-disposition": "attachment; filename=some.pdf",
"content-type": "text\/html;\r\n\tcharset=\"iso-8859-1\""
},
"body": "This is a BASE64 body"
}],
"from": "Sender",
"origfrom": "Example <example@example.net>",
"to": "recipient",
"id": "1419696967-44152505-recipient",
"time": 1526072140000,
"seconds_ago": 55994
}
}
49 changes: 47 additions & 2 deletions spec/fixtures/email.response
Original file line number Diff line number Diff line change
@@ -1,5 +1,50 @@
HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
Date: Sun, 28 Dec 2014 13:39:51 GMT
Date: Fri, 11 May 2018 13:39:51 GMT

{"apiInboxFetchesLeft":696,"apiEmailFetchesLeft":10,"data":{"headers":{"sender":"example@example.net","content-type":"multipart\/alternative;\r\n\tboundary=\"MIMEBoundaryec1dc8828d9bbedf095f9e9d2ebdbdeb\"","to":"recipient@mailinator.com","x-connecting-ip":"127.0.0.1","subject":"This is a subject","mime-version":"1.0","x-received-time":"1419696967076","reply-to":"example@example.net","received":"from 127-0-0-1.dynamic-ip.example.net ()\r\n by mail.mailinator.com with SMTP (Postfix)\r\n for recipient@mailinator.com;\r\n Sat, 27 Dec 2014 16:16:07 +0000 (UTC)","from":"\"Sender\" <example@example.net>","date":"Sun, 28 Dec 2014 00:16:00 +0800"},"seconds_ago":77024,"id":"1419696967-44152505-recipient","to":"recipient@mailinator.com","time":1419696967076,"subject":"This is a subject","fromfull":"example@example.net","parts":[{"headers":{"content-type":"text\/plain;\r\n\tcharset=\"iso-8859-1\"","content-transfer-encoding":"quoted-printable"},"body":"This is a body"},{"headers":{"content-type":"text\/html;\r\n\tcharset=\"iso-8859-1\"","content-transfer-encoding":"quoted-printable"},"body":"<!DOCTYPE html PUBLIC \"-\/\/W3C\/\/DTD XHTML 1.0 Transitional\/\/EN\" \"http:\/\/www.w3.org\/TR\/xhtml1\/DTD\/xhtml1-transitional.dtd\">\r\n<html xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\r\n\r\n<head>\r\n<meta content=\"en-us\" http-equiv=\"Content-Language\" \/>\r\n<meta content=\"text\/html; charset=utf-8\" http-equiv=\"Content-Type\" \/>\r\n<title>Alert<\/title>\r\n<\/head>\r\n\r\n<body><p>This is a body<\/p><\/body>\r\n\r\n<\/html>"}],"been_read":false,"from":"Sender","ip":"127.0.0.1"},"forwardsLeft":10}
{
"data": {
"fromfull": "example@example.net",
"headers": {
"date": "Mon, 14 May 2018 00:47:55 -0600 (MDT)",
"mime-version": "1.0",
"x-virus-scanned": "ClamAV using ClamSMTP",
"subject": "This is a Subject",
"message-id": "<message.id@example.com>",
"received": [
"from bmta1.example.com([9.9.9.9]) by mail.mailinator.com with SMTP (Postfix) for receipient@example.com; Fri, 11 May 2018 20:55:36 +0000 (UTC)",
"from mail-2.example.com (mail.example.com [9.9.9.9]) by bmta1.example.com (Postfix) with ESMTP id 5B2118627 for <receipient@example.com>; Fri, 11 May 2018 14:55:39 -0600 (MDT)",
"from mail-2.example.com (localhost [127.0.0.1]) by mail.example.com (Postfix) with ESMTP id 4BF611FFAF for <receipient@example.com>; Fri, 11 May 2018 16:55:39 -0400 (EDT)",
"from mail-1.example.com (smtp.example.com [9.9.9.9]) by mail-prod-2.example.com (Postfix) with ESMTP id 3BB0E1FF38 for <receipient@example.com>; Fri, 11 May 2018 16:55:39 -0400 (EDT)",
"from app-4.example.net (app-4.example.net [9.9.9.9]) by mail.example.com (Postfix) with ESMTP id 3946F20A83 for <receipient@example.com>; Fri, 11 May 2018 16:55:39 -0400 (EDT)",
"from app-4.example.net (localhost [127.0.0.1]) by app-4.example.net (Postfix) with ESMTP id 3481E413E1 for <receipient@example.com>; Fri, 11 May 2018 14:55:39 -0600 (MDT)"
],
"from": "Sender <sender@example.com>",
"content-type": "multipart/mixed; boundary=\"-- -- = _part_124646_371774408 .1526072139209 \"",
"to": "<recipient@mailinator.com>",
"x-mailinator-smtp": ["system: start;0", "s: 220 mail.mailinator.com esmtp postfix;0", "r: ehlo bmta1.somemail.com;102", "s: 250-mail.mailinator.com 250-8bitmime 250 ok;102", "r: mail from:<exampl@example.net>;194", "s: 250 ok;194", "r: rcpt to:<recipient@mailinator.com>;286", "s: 250 ok;286", "r: data;377", "s: 354 end data with <cr><lf>.<cr><lf>;377", "s: 250 ok;6137"]
},
"subject": "This is a Subject",
"requestId": "638363",
"parts": [{
"headers": {
"content-transfer-encoding": "quoted-printable",
"content-type": "text/html; charset=\"utf-8\""
},
"body": "<p>This is a body</p>"
}, {
"headers": {
"content-transfer-encoding": "base64",
"content-disposition": "attachment; filename=some.pdf",
"content-type": "text\/html;\r\n\tcharset=\"iso-8859-1\""
},
"body": "This is a BASE64 body JVBERi0xLjUKJeLjz9MKMSAwIG9iag08PC9UeXBlL1BhZ2UvUGFyZW50IDYgMCBSIC9NZWRpYUJv"
}],
"from": "Sender",
"origfrom": "Sender <sender@example.com>",
"to": "Recipient",
"id": "1419696967-44152505-recipient",
"time": 1526072140000,
"seconds_ago": 15258
}
}
50 changes: 50 additions & 0 deletions spec/fixtures/email_private.response
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
Date: Mon, 4 14 2018 12:26:18 GMT

{
"data": {
"fromfull": "example@example.net",
"headers": {
"date": "Mon, 14 May 2018 00:47:55 -0600 (MDT)",
"mime-version": "1.0",
"x-virus-scanned": "ClamAV using ClamSMTP",
"subject": "This is a Subject",
"message-id": "<message.id@example.com>",
"received": [
"from bmta1.example.com([9.9.9.9]) by mail.mailinator.com with SMTP (Postfix) for receipient@example.com; Fri, 11 May 2018 20:55:36 +0000 (UTC)",
"from mail-2.example.com (mail.example.com [9.9.9.9]) by bmta1.example.com (Postfix) with ESMTP id 5B2118627 for <receipient@example.com>; Fri, 11 May 2018 14:55:39 -0600 (MDT)",
"from mail-2.example.com (localhost [127.0.0.1]) by mail.example.com (Postfix) with ESMTP id 4BF611FFAF for <receipient@example.com>; Fri, 11 May 2018 16:55:39 -0400 (EDT)",
"from mail-1.example.com (smtp.example.com [9.9.9.9]) by mail-prod-2.example.com (Postfix) with ESMTP id 3BB0E1FF38 for <receipient@example.com>; Fri, 11 May 2018 16:55:39 -0400 (EDT)",
"from app-4.example.net (app-4.example.net [9.9.9.9]) by mail.example.com (Postfix) with ESMTP id 3946F20A83 for <receipient@example.com>; Fri, 11 May 2018 16:55:39 -0400 (EDT)",
"from app-4.example.net (localhost [127.0.0.1]) by app-4.example.net (Postfix) with ESMTP id 3481E413E1 for <receipient@example.com>; Fri, 11 May 2018 14:55:39 -0600 (MDT)"
],
"from": "Sender <sender@example.com>",
"content-type": "multipart/mixed; boundary=\"-- -- = _part_124646_371774408 .1526072139209 \"",
"to": "<recipient@mailinator.com>",
"x-mailinator-smtp": ["system: start;0", "s: 220 mail.mailinator.com esmtp postfix;0", "r: ehlo bmta1.somemail.com;102", "s: 250-mail.mailinator.com 250-8bitmime 250 ok;102", "r: mail from:<exampl@example.net>;194", "s: 250 ok;194", "r: rcpt to:<recipient@mailinator.com>;286", "s: 250 ok;286", "r: data;377", "s: 354 end data with <cr><lf>.<cr><lf>;377", "s: 250 ok;6137"]
},
"subject": "This is a Subject",
"requestId": "638363",
"parts": [{
"headers": {
"content-transfer-encoding": "7bit",
"content-type": "text/html; charset=\"utf-8\""
},
"body": "<p>This is a body</p>"
}, {
"headers": {
"content-transfer-encoding": "base64",
"content-disposition": "attachment; filename=some.pdf",
"content-type": "text\/html;\r\n\tcharset=\"iso-8859-1\""
},
"body": "This is a BASE64 body JVBERi0xLjUKJeLjz9MKMSAwIG9iag08PC9UeXBlL1BhZ2UvUGFyZW50IDYgMCBSIC9NZWRpYUJv"
}],
"from": "Sender",
"origfrom": "Sender <sender@example.com>",
"to": "recipient",
"id": "1419696967-44152505-recipient",
"time": 1526072140000,
"seconds_ago": 15258
}
}
26 changes: 24 additions & 2 deletions spec/fixtures/inbox.response
Original file line number Diff line number Diff line change
@@ -1,5 +1,27 @@
HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
Date: Sun, 28 Dec 2014 16:26:18 GMT
Date: Mon, 14 May 2018 16:26:18 GMT

{"messages":[{"seconds_ago":87011,"id":"1419696967-44152505-recipient","to":"recipient@mailinator.com","time":1419696967076,"subject":"First subject","fromfull":"sender@example.org","been_read":false,"from":"Sender name","ip":"127.0.0.1"},{"seconds_ago":73130,"id":"1419710848-46252175-recipient","to":"recipient@mailinator.com","time":1419710848789,"subject":"Second subject","fromfull":"sender@example.org","been_read":false,"from":"Another sender name","ip":"127.0.0.2"}]}
{
"messages": [{
"seconds_ago": 87011,
"id": "1419696967-44152505-recipient",
"to": "recipient@mailinator.com",
"time": 1419696967076,
"subject": "First subject",
"fromfull": "sender@example.org",
"been_read": false,
"from": "Sender name",
"ip": "127.0.0.1"
}, {
"seconds_ago": 73130,
"id": "1419710848-46252175-recipient",
"to": "recipient@mailinator.com",
"time": 1419710848789,
"subject": "Second subject",
"fromfull": "sender@example.org",
"been_read": false,
"from": "Another sender name",
"ip": "127.0.0.2"
}]
}
19 changes: 19 additions & 0 deletions spec/fixtures/inbox_private.response
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
Date: Mon, 14 2018 12:26:18 GMT

{
"messages": [{
"subject": "First subject",
"from": "Sender name",
"id": "1419696967-44152505-recipient",
"to": "recipient@private_domain.mailinator.com",
"seconds_ago": 41616
}, {
"subject": "Second subject",
"from": "Another Sender name",
"id": "1556696970-44152699-recipient",
"to": "recipient@private_domain.mailinator.com",
"seconds_ago": 41506
}]
}
30 changes: 14 additions & 16 deletions spec/lib/mailinator/email_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,21 @@
let(:message) { subject.get('abcd1234') }

it 'gets the email' do
expect(message.id).to eq('1419696967-44152505-recipient')
expect(message.subject).to eq('This is a subject')
expect(message.body).to eq(body)
expect(message.body_html).to include("<p>#{body}</p>")
expect(message.read?).to_not be
expect(message.inbox_fetches_left).to eq(696)
expect(message.email_fetches_left).to eq(10)
expect(message.sender).to eq('example@example.net')
expect(message.reply_to).to eq('example@example.net')
expect(message.from).to eq('Sender')
expect(message.ip).to eq('127.0.0.1')
expect(message.received)
.to match(a_string_matching('from 127-0-0-1.dynamic-ip.example.net'))
message = Mailinator::Email.get('abcd1234')
expect(message.from_full).to eq('example@example.net')
expect(message.date).to be_a(DateTime)
expect(message.time).to eq(1_419_696_967_076)
expect(message.forwards_left).to eq(10)
expect(message.original).to be_a(Hash)
expect(message.received)
.to include('from bmta1.example.com([9.9.9.9]) by mail.mailinator.com with SMTP (Postfix) for receipient@example.com; Fri, 11 May 2018 20:55:36 +0000 (UTC)')
expect(message.from).to eq('Sender <sender@example.com>')
expect(message.to).to eq('<recipient@mailinator.com>')
expect(message.subject).to eq('This is a Subject')
expect(message.request_id).to eq('638363')
expect(message.body).to include('<p>This is a body</p>')
expect(message.body_base_64).to include('This is a BASE64 body JVBERi0xLjUKJe')
expect(message.orig_from).to eq('Sender <sender@example.com>')
expect(message.id).to eq('1419696967-44152505-recipient')
expect(message.time).to eq(1_526_072_140_000)
expect(message.seconds_ago).to eq(15_258)
end
end

Expand Down

0 comments on commit e661b4d

Please sign in to comment.