From 8b2c14ab4cdbee86b7388604f5f4f8e6bab56eef Mon Sep 17 00:00:00 2001 From: David Roetzel Date: Fri, 2 Nov 2018 12:20:52 +0100 Subject: [PATCH] Initial import. --- .editorconfig | 9 ++++ .gitignore | 9 ++++ .travis.yml | 1 + LICENSE | 21 ++++++++ README.md | 65 +++++++++++++++++++++++ shard.yml | 20 +++++++ spec/carbon_smtp_adapter_spec.cr | 41 ++++++++++++++ spec/spec_helper.cr | 4 ++ spec/templates/test_email/html.ecr | 1 + spec/templates/test_email/text.ecr | 1 + src/carbon/adapters/.smtp_adapter.cr.swp | Bin 0 -> 12288 bytes src/carbon/adapters/smtp_adapter.cr | 46 ++++++++++++++++ src/carbon_smtp_adapter.cr | 9 ++++ 13 files changed, 227 insertions(+) create mode 100644 .editorconfig create mode 100644 .gitignore create mode 100644 .travis.yml create mode 100644 LICENSE create mode 100644 README.md create mode 100644 shard.yml create mode 100644 spec/carbon_smtp_adapter_spec.cr create mode 100644 spec/spec_helper.cr create mode 100644 spec/templates/test_email/html.ecr create mode 100644 spec/templates/test_email/text.ecr create mode 100644 src/carbon/adapters/.smtp_adapter.cr.swp create mode 100644 src/carbon/adapters/smtp_adapter.cr create mode 100644 src/carbon_smtp_adapter.cr diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..163eb75 --- /dev/null +++ b/.editorconfig @@ -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 diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..e29dae7 --- /dev/null +++ b/.gitignore @@ -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 diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..ffc7b6a --- /dev/null +++ b/.travis.yml @@ -0,0 +1 @@ +language: crystal diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..658c0e2 --- /dev/null +++ b/LICENSE @@ -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. diff --git a/README.md b/README.md new file mode 100644 index 0000000..3b9f96e --- /dev/null +++ b/README.md @@ -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 () +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 diff --git a/shard.yml b/shard.yml new file mode 100644 index 0000000..146d80b --- /dev/null +++ b/shard.yml @@ -0,0 +1,20 @@ +name: carbon_smtp_adapter +version: 0.1.0 + +authors: + - David Roetzel + +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 diff --git a/spec/carbon_smtp_adapter_spec.cr b/spec/carbon_smtp_adapter_spec.cr new file mode 100644 index 0000000..0125747 --- /dev/null +++ b/spec/carbon_smtp_adapter_spec.cr @@ -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", "support@myapp.com") + to "fred@example.org" + 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 /) + 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 diff --git a/spec/spec_helper.cr b/spec/spec_helper.cr new file mode 100644 index 0000000..eab0a25 --- /dev/null +++ b/spec/spec_helper.cr @@ -0,0 +1,4 @@ +require "spec" +require "../src/carbon_smtp_adapter" +require "devmail/store" +require "devmail/smtp_server" diff --git a/spec/templates/test_email/html.ecr b/spec/templates/test_email/html.ecr new file mode 100644 index 0000000..dbf4582 --- /dev/null +++ b/spec/templates/test_email/html.ecr @@ -0,0 +1 @@ +

Welcome!

diff --git a/spec/templates/test_email/text.ecr b/spec/templates/test_email/text.ecr new file mode 100644 index 0000000..d8e9d74 --- /dev/null +++ b/spec/templates/test_email/text.ecr @@ -0,0 +1 @@ +Welcome! diff --git a/src/carbon/adapters/.smtp_adapter.cr.swp b/src/carbon/adapters/.smtp_adapter.cr.swp new file mode 100644 index 0000000000000000000000000000000000000000..a11f8590eeed921451ffbc44362c945c6152b873 GIT binary patch literal 12288 zcmeI2y=xRf7{(`7;`e7I!fOP%K)8z$MWQjN2wJ4^6SY|8cIR@ddppa{ToPl{e?!pP zQY-C5JF(Hm#!4&y2Mfh#c6atxBgR&F7oMBDciw&8ncuw|2shoSs~6_!cza6V+9AZZ zcOP#~_g2PE_JoM!qH^K&uI0?Emr~5%cZQuT*SAz?O(&FjSEqp)*esBd%&f{gCJ(E4 zrwW-)Goc;ke^rW;m;e)4Ltv{oGJUvs)DMmypxwK!tjXX26JP>NfC(@GCcp%k025#W z|1SZXjfn@?-iB(6?P?tRWvo8(zyz286JP>NfC(@GCcp%k025#WOn?deg9KzG#Ox*^ zc5cSv@%z93|Np~wAznidp%v&NGzD#jzHAfXGxQ010X>EuK`}HB?T5Z@72+-Q3VHdM}CS99jYfq zG3kAXav2*%j9^RZ2J=yBJ4tx_}v9XWZBE0awS@Y^QNDw|=eC zS2bLzsE0M!vYI!ntNB24S7o)LT)miCw-IMz+CEk!)b}?Md<+R$9xqC(C{jIIP&SZ5 z+Yjt8OBBt}WjD4i*&Mc$RE`sKj;SZ=N1}l;MlPtJZwCpffsB*3dmD6hbf;KIol?zO zwQQTSBP*hA7`7lBNRD#&wySZ{P1Hfx1{0NaSrD^rCBr^NnpPXBt3IpfDk?5iXn55H z8nLzhP5V*e@7%3!SzWKRc5lSix?4S3{3y}8_i{aGmD_Z0M@Sm6rl%Qp-2v|kcJ-;? zFf(&*-hDj=#9MXK^tG{*jULjuodC!30dieIv%fS5%v{$*Dv1|W9;AAyTpt?eY;mX@ zg_CR3Vo9A)Af=9n;7Jr(K>;P&4=nIo4!hP5NWI>{i9Qc zJtN^lYL86AXZq;RE&x>Dj_L74hca>4+_QRKc4I3oPR6U^and48@CT|UaS7+^PnXB* K>0}+PBH{