mailslurp-examples - ruby-minitest-netsmtp-example

https://github.com/mailslurp/examples

Table of Contents

ruby-minitest-netsmtp-example/test_example.rb

require 'minitest/autorun'
#<gen>ruby_minitest_configure
require 'mailslurp_client'

MailSlurpClient.configure do |config|
  config.api_key['x-api-key'] = ENV['API_KEY']
end
#</gen>
class SmtpExample < Minitest::Test
  def test_can_create_inbox_send_and_receive
    #<gen>ruby_minitest_create_controller
    inbox_controller = MailSlurpClient::InboxControllerApi.new
    #</gen>
    #<gen>ruby_minitest_create_inbox
    options = {
      name: "My test inbox",
      inboxType: "SMTP_INBOX"
    }
    inbox = inbox_controller.create_inbox_with_options(options)
    assert_match /@mailslurp/, inbox.email_address
    #</gen>
    #<gen>ruby_minitest_send_email
    inbox_controller.send_email(inbox.id, {
      to: [inbox.email_address],
      subject: "Hello",
      body: "Welcome. Your code is: 123456",
    })
    #</gen>
    #<gen>ruby_minitest_receive_email
    wait_for_controller = MailSlurpClient::WaitForControllerApi.new
    wait_options = {
      inbox_id: inbox.id,
      timeout: 120000,
      unread_only: true
    }
    email = wait_for_controller.wait_for_latest_email(wait_options)
    assert_match /Welcome/, email.body
    #</gen>
    #<gen>ruby_minitest_extract_code
    code = email.body.match(/Your code is: ([0-9]{6})/)[1]
    assert_equal code, '123456'
    #</gen>
    #<gen>ruby_minitest_smtp_send
    require 'net/smtp'
    access_details = inbox_controller.get_imap_smtp_access(inbox_id: inbox.id)
    Net::SMTP.start(
      address= access_details.secure_smtp_server_host,
      port= access_details.secure_smtp_server_port,
      helo= inbox.email_address.match(/@(.+)/)[1],
      user= access_details.secure_smtp_username,
      secret= access_details.secure_smtp_password,
      authtype= :plain
    ) do |smtp|
      message = <<EOF
Subject: SMTP test

This is my body
EOF
      smtp.send_message message, inbox.email_address, inbox.email_address
      smtp.finish
    end
    #</gen>
    smtp_email = wait_for_controller.wait_for_latest_email(inbox_id: inbox.id, timeout: 120_000, unread_only: true)
    assert_match /SMTP test/, smtp_email.subject
    assert_match /This is my body/, smtp_email.body
  end
end

ruby-minitest-netsmtp-example/Rakefile

# frozen_string_literal: true

require "bundler/gem_tasks"
require "rake/testtask"

Rake::TestTask.new(:test) do |t|
  t.libs << "test"
  t.libs << "lib"
  t.test_files = FileList["test/**/test_*.rb"]
end

require "rubocop/rake_task"

RuboCop::RakeTask.new

task default: %i[test rubocop]

ruby-minitest-netsmtp-example/Makefile

-include ../.env
.PHONY: test

test:
	 API_KEY=$(API_KEY) bundle exec ruby test_example.rb

ruby-minitest-netsmtp-example/LICENSE.txt

The MIT License (MIT)

Copyright (c) 2024 jack-mailslurp

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

ruby-minitest-netsmtp-example/Gemfile.lock

GEM
  remote: https://rubygems.org/
  specs:
    ethon (0.16.0)
      ffi (>= 1.15.0)
    ffi (1.16.3)
    mailslurp_client (15.17.40)
    minitest (5.22.2)
    typhoeus (1.4.1)
      ethon (>= 0.9.0)

PLATFORMS
  ruby

DEPENDENCIES
  mailslurp_client
  minitest
  typhoeus

BUNDLED WITH
   1.17.2

ruby-minitest-netsmtp-example/Gemfile

#<gen>ruby_minitest_gemspec
source "https://rubygems.org"

gem 'mailslurp_client'
gem 'typhoeus'
#</gen>
gem 'minitest'

ruby-minitest-netsmtp-example/.rubocop.yml

AllCops:
  TargetRubyVersion: 2.6

Style/StringLiterals:
  Enabled: true
  EnforcedStyle: double_quotes

Style/StringLiteralsInInterpolation:
  Enabled: true
  EnforcedStyle: double_quotes

Layout/LineLength:
  Max: 120

ruby-minitest-netsmtp-example/sig/ruby/minitest/netsmtp/example.rbs

module Ruby
  module Minitest
    module Netsmtp
      module Example
        VERSION: String
        # See the writing guide of rbs: https://github.com/ruby/rbs#guides
      end
    end
  end
end

ruby-minitest-netsmtp-example/lib/ruby/minitest/netsmtp/example.rb

# frozen_string_literal: true

require_relative "example/version"

module Ruby
  module Minitest
    module Netsmtp
      module Example
        class Error < StandardError; end
        # Your code goes here...
      end
    end
  end
end

ruby-minitest-netsmtp-example/lib/ruby/minitest/netsmtp/example/version.rb

# frozen_string_literal: true

module Ruby
  module Minitest
    module Netsmtp
      module Example
        VERSION = "0.1.0"
      end
    end
  end
end

ruby-minitest-netsmtp-example/bin/setup

#!/usr/bin/env bash
set -euo pipefail
IFS=$'\n\t'
set -vx

bundle install

# Do any other automated setup that you need to do here

ruby-minitest-netsmtp-example/bin/console

#!/usr/bin/env ruby
# frozen_string_literal: true

require "bundler/setup"
require "ruby/minitest/netsmtp/example"

# You can add fixtures and/or initialization code here to make experimenting
# with your gem easier. You can also use a different console, if you like.

require "irb"
IRB.start(__FILE__)

ruby-minitest-netsmtp-example/.bundle/config

---
BUNDLE_PATH: "vendor/bundle"