Skip to content

Sending test

Igor Balos edited this page Feb 10, 2020 · 8 revisions

To send a simple email, all you need to do is create a Postmark API client and send a mail message.

For these API requests you will need to use a server API token. Once you obtain it, you will need to use server API client.

For quick testing purposes though, Postmark lets you send test emails that won’t actually get delivered to the recipient. This method is typically used to verify that your data is valid. You can do this by passing POSTMARK_API_TEST as your server API token.

server_token = 'POSTMARK_API_TEST'
client = Postmark::ApiClient.new(server token, http_open_timeout: 15)

Sending a simple test email

Once you set the test server token, you can send a test email message which will not be sent, but it will return a proper sending response, just like it would if the email was sent.

The only difference between the real sending response and the test response is the message in the response. It would say OK in real response, and Test job accepted in test response.

client.deliver(from: '[email protected]',
               to: 'Leonard Hofstadter <[email protected]>',
               subject: 'Re: Come on, Sheldon. It will be fun.',
               text_body: 'That\'s what you said about the Green Lantern.')

# => {:to=>"Leonard Hofstadter <[email protected]>", :submitted_at=>"2020-02-10T11:51:52.098666-05:00", :message_id=>"252a11e9-06c4-42d8-a6f1-8b992504ba2e", :error_code=>0, :message=>"Test job accepted"}

For more detailed testing, we suggest integration testing article.