Skip to content

Commit

Permalink
Initial import.
Browse files Browse the repository at this point in the history
  • Loading branch information
oneiros committed Nov 2, 2018
0 parents commit 8b2c14a
Show file tree
Hide file tree
Showing 13 changed files with 227 additions and 0 deletions.
9 changes: 9 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
root = true

[*.cr]
charset = utf-8
end_of_line = lf
insert_final_newline = true
indent_style = space
indent_size = 2
trim_trailing_whitespace = true
9 changes: 9 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/docs/
/lib/
/bin/
/.shards/
*.dwarf

# Libraries don't need dependency lock
# Dependencies will be locked in application that uses them
/shard.lock
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
language: crystal
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2018 David Roetzel

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.
65 changes: 65 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
# carbon\_smtp\_adapter

A simple SMTP-Adapter for [carbon](https://github.com/luckyframework/carbon).

**Caveat**: This adapter uses [crystal-email](https://github.com/arcage/crystal-email) which does not currently allow to set arbitrary email headers. So this feature of
carbon will not work.

## Versioning

The current plan is to track carbon's major and minor numbers, so that
carbon\_smtp\_adapter `0.1.x` is compatible with carbon `0.1.x` and so on.

## Installation

Add this to your application's `shard.yml`:

```yaml
dependencies:
carbon_smtp_adapter:
github: your-github-user/carbon_smtp_adapter
```
## Usage
```crystal
require "carbon_smtp_adapter"

# configure your base email class to use the smtp adapter:
BaseEmail.configure do |setting|
settings.adapter = Carbon::SmtpAdapter.new
end
```

By default, carbon will try to deliver the email to an smtp server running on
`localhost` and listening on port `25`. If you need different settings, you can
configure the following (values shown are the defaults):

```crystal
Carbon::SmtpAdapter.configure do |settings|
settings.host = "localhost"
settings.port = 25
settings.helo_domain = nil
settings.use_tls = true
settings.username = nil
settings.password = nil
end
```

## Contributing

1. Fork it (<https://github.com/oneiros/carbon_smtp_adapter/fork>)
2. Create your feature branch (`git checkout -b my-new-feature`)
3. Commit your changes (`git commit -am 'Add some feature'`)
4. Push to the branch (`git push origin my-new-feature`)
5. Create a new Pull Request

## Contributors

- [oneiros](https://github.com/oneiros) David Roetzel - creator, maintainer

With many thanks to:

- [paulcsmith](https://github.com/paulcsmith) Paul Smith - creator of carbon
- [arcage](https://github.com/arcage) arcage - creator of crystal-email
- [tijn](https://github.com/tijn) Tijn Schuurmans - creator of devmail
20 changes: 20 additions & 0 deletions shard.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
name: carbon_smtp_adapter
version: 0.1.0

authors:
- David Roetzel <[email protected]>

crystal: 0.26.1

license: MIT

dependencies:
carbon:
github: luckyframework/carbon
version: "~> 0.1.0"
email:
github: arcage/crystal-email
version: "~> 0.3.0"
development_dependencies:
devmail:
github: tijn/devmail
41 changes: 41 additions & 0 deletions spec/carbon_smtp_adapter_spec.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
require "./spec_helper"

SMTP_PORT = 30025

email_store = Store.new
smtp_server = SMTPServer.new(email_store, SMTP_PORT)
smtp_server.run

Carbon::SmtpAdapter.configure do |settings|
settings.port = SMTP_PORT
end

abstract class BaseEmail < Carbon::Email
end

BaseEmail.configure do |settings|
settings.adapter = Carbon::SmtpAdapter.new
end

class TestEmail < BaseEmail
from Carbon::Address.new("My App Name", "[email protected]")
to "[email protected]"
subject "Test Subject"
templates text, html
end

describe CarbonSmtpAdapter do
it "works" do
email = TestEmail.new
p email.headers
Carbon::SmtpAdapter.new.deliver_now(email)

email_store.count.should eq(1)
received_email = email_store.messages.last
received_email.should match(/From: My App Name <support@myapp\.com>/)
received_email.should match(/To: fred@example\.org/)
received_email.should match(/Subject: Test Subject/)
received_email.should match(/Content-Type: text\/plain/)
received_email.should match(/Content-Type: text\/html/)
end
end
4 changes: 4 additions & 0 deletions spec/spec_helper.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
require "spec"
require "../src/carbon_smtp_adapter"
require "devmail/store"
require "devmail/smtp_server"
1 change: 1 addition & 0 deletions spec/templates/test_email/html.ecr
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<h1>Welcome!</h1>
1 change: 1 addition & 0 deletions spec/templates/test_email/text.ecr
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Welcome!
Binary file added src/carbon/adapters/.smtp_adapter.cr.swp
Binary file not shown.
46 changes: 46 additions & 0 deletions src/carbon/adapters/smtp_adapter.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
class Carbon::SmtpAdapter < Carbon::Adapter
Habitat.create do
setting host : String = "localhost"
setting port : Int32 = 25
setting helo_domain : String? = nil
setting use_tls : Bool = true
setting username : String? = nil
setting password : String? = nil
end

def deliver_now(email : Carbon::Email)
auth = get_auth_tuple

::EMail.send(settings.host, settings.port) do
subject email.subject
from(email.from.address, email.from.name)
email.to.each do |to_address|
to(to_address.address, to_address.name)
end
email.cc.each do |cc_address|
cc(cc_address.address, cc_address.name)
end
email.bcc.each do |bcc_address|
bcc(bcc_address.address, bcc_address.name)
end
message email.text_body
message_html email.html_body
end
end

private def get_auth_tuple : Tuple(String, String)?
username = settings.username
password = settings.password

if username && password.nil?
raise "You need to provide a password when setting a username"
end
if password && username.nil?
raise "You need to set a username when providing a password"
end

if username && password
{username, password}
end
end
end
9 changes: 9 additions & 0 deletions src/carbon_smtp_adapter.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
require "habitat"
require "carbon"
require "email"

require "./carbon/adapters/smtp_adapter"

module CarbonSmtpAdapter
VERSION = "0.1.0"
end

0 comments on commit 8b2c14a

Please sign in to comment.