postgarnet 1.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/lib/postgarnet.rb +175 -0
- metadata +59 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 271a04be208146d3bb3c4e705d049d02ccdf6fbd104d171ff83ad548379bfc18
|
4
|
+
data.tar.gz: 1b9b7ff07abe7f4314321409c8726cc433e11349c5727b424400ca48991c5988
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 1c89bef7166ffbd7ca165c08f1842944b62a8d5ea001d7865a568599f273108641ed35cfe571f3008b1221250723181a2be864daa9f225eaf490c784283b77b2
|
7
|
+
data.tar.gz: c1093b6c03e7d6303b5990d5a813e6e4278ca6e2946e2efb0e2a5dba48e759f006d3bdac77681f24acf993314d254b92628f679cae5cf76438370086aec70af5
|
data/lib/postgarnet.rb
ADDED
@@ -0,0 +1,175 @@
|
|
1
|
+
require 'net/smtp'
|
2
|
+
|
3
|
+
class FormatError < StandardError
|
4
|
+
def initialize(msg)
|
5
|
+
super
|
6
|
+
end
|
7
|
+
end
|
8
|
+
|
9
|
+
class TransactionError < StandardError
|
10
|
+
def initialize(msg)
|
11
|
+
super
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
class Postgarnet
|
16
|
+
@@version = "1.0.3"
|
17
|
+
|
18
|
+
def initialize(username:, password:)
|
19
|
+
@username, @password, @subject, @content, @recipient, @author = @username, @smtp = nil
|
20
|
+
login(username, password)
|
21
|
+
@headers = {
|
22
|
+
"mime-version" => "1",
|
23
|
+
"content-type" => "text/html"
|
24
|
+
}
|
25
|
+
end
|
26
|
+
|
27
|
+
def subject(text=nil, payload=nil)
|
28
|
+
unless text.nil? || text.is_a?(String)
|
29
|
+
raise FormatError, "Subject must be a string"
|
30
|
+
end
|
31
|
+
unless payload.nil? || payload.is_a?(Hash)
|
32
|
+
raise FormatError, "Payload must be a hash"
|
33
|
+
end
|
34
|
+
if text.nil?
|
35
|
+
return @subject
|
36
|
+
else
|
37
|
+
@subject = payload.nil? ? text : use_payload(payload, text)
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
def content(text=nil, payload=nil)
|
42
|
+
unless text.nil? || text.is_a?(String)
|
43
|
+
raise FormatError, "Subject must be a string"
|
44
|
+
end
|
45
|
+
unless payload.nil? || payload.is_a?(Hash)
|
46
|
+
raise FormatError, "Payload must be a hash"
|
47
|
+
end
|
48
|
+
if text.nil?
|
49
|
+
return @content
|
50
|
+
else
|
51
|
+
@content = payload.nil? ? text : use_payload(payload, text)
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
def to(recipient=nil)
|
56
|
+
unless recipient.nil? || recipient.is_a?(String)
|
57
|
+
raise FormatError, "Recipient (to) must be a string"
|
58
|
+
end
|
59
|
+
if recipient.nil?
|
60
|
+
return @recipient
|
61
|
+
else
|
62
|
+
@recipient = recipient
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
def from(author=nil)
|
67
|
+
unless author.nil? || author.is_a?(String)
|
68
|
+
raise FormatError, "Author (from) must be a string"
|
69
|
+
end
|
70
|
+
if author.nil?
|
71
|
+
return @author
|
72
|
+
else
|
73
|
+
@author = author
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
def header(key, value=nil)
|
78
|
+
unless key.is_a?(String) || key.is_a?(Symbol)
|
79
|
+
raise FormatError, "Header key must be a string or a symbol"
|
80
|
+
end
|
81
|
+
unless value.nil? || value.is_a?(String)
|
82
|
+
raise FormatError, "Header value must be a string"
|
83
|
+
end
|
84
|
+
if value.nil?
|
85
|
+
return @headers[key]
|
86
|
+
else
|
87
|
+
@headers[key] = value
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
91
|
+
def headers(value=nil)
|
92
|
+
unless value.nil? || value.is_a?(Hash)
|
93
|
+
raise FormatError, "Header value must be a hash"
|
94
|
+
end
|
95
|
+
if value.nil?
|
96
|
+
return @headers.clone
|
97
|
+
else
|
98
|
+
@headers = value.clone
|
99
|
+
end
|
100
|
+
end
|
101
|
+
|
102
|
+
def connect(host: "smtp.gmail.com", port: 587)
|
103
|
+
begin
|
104
|
+
@smtp = Net::SMTP.new(host, port)
|
105
|
+
@smtp.enable_starttls
|
106
|
+
rescue Exception => e
|
107
|
+
raise TransactionError, "Error when connecting: #{e}"
|
108
|
+
else
|
109
|
+
true
|
110
|
+
end
|
111
|
+
end
|
112
|
+
|
113
|
+
def login(username:, password:)
|
114
|
+
unless @smtp.nil?
|
115
|
+
raise TransactionError, "A connection already exists. Disconnect to the server first to login a new account."
|
116
|
+
end
|
117
|
+
if username.include?("@")
|
118
|
+
username = username.split("@")[0]
|
119
|
+
end
|
120
|
+
@username = "#{username}@gmail.com"
|
121
|
+
@password = password
|
122
|
+
return @username.clone
|
123
|
+
end
|
124
|
+
|
125
|
+
def disconnect
|
126
|
+
if @smtp.nil?
|
127
|
+
raise TransactionError, "No connection to disconnect"
|
128
|
+
end
|
129
|
+
@smtp.finish
|
130
|
+
@smtp = nil
|
131
|
+
end
|
132
|
+
|
133
|
+
def smtp
|
134
|
+
if @smtp.nil?
|
135
|
+
raise TransactionError, "Connect to the server first"
|
136
|
+
end
|
137
|
+
return @smtp
|
138
|
+
end
|
139
|
+
|
140
|
+
def send(client="localhost")
|
141
|
+
if [@username, @password, @author, @recipient, @subject, @content].any?(&:nil?)
|
142
|
+
raise FormatError, "Incomplete parameters"
|
143
|
+
end
|
144
|
+
if @smtp.nil?
|
145
|
+
raise TransactionError, "Connect to the server first"
|
146
|
+
end
|
147
|
+
begin
|
148
|
+
message = self.create_msg
|
149
|
+
@smtp.start(client, @username, @password, :plain) do
|
150
|
+
@smtp.send_message(message, @username, @recipient)
|
151
|
+
end
|
152
|
+
rescue Exception => e
|
153
|
+
raise TransactionError, "Error when sending: #{e}"
|
154
|
+
end
|
155
|
+
end
|
156
|
+
|
157
|
+
private
|
158
|
+
def use_payload(payload, content)
|
159
|
+
params = payload.clone
|
160
|
+
params.merge!({
|
161
|
+
from: @author,
|
162
|
+
to: @recipient,
|
163
|
+
author: @author,
|
164
|
+
recipient: @recipient
|
165
|
+
})
|
166
|
+
content.gsub(/\{(.+?)\}/) do
|
167
|
+
params[$1.to_s] || params[$1.to_sym] || "{#{$1}}"
|
168
|
+
end
|
169
|
+
end
|
170
|
+
|
171
|
+
def create_msg
|
172
|
+
headers = @headers.map { |key, value| "#{key}: #{value}" }.join("\n")
|
173
|
+
return "From: #{@author} <#{@username}>\nTo: #{@recipient} <#{@recipient}>\nSubject: #{@subject}\n#{headers}\n\n<html><body>#{@content}</body></html>"
|
174
|
+
end
|
175
|
+
end
|
metadata
ADDED
@@ -0,0 +1,59 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: postgarnet
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.3
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- trulyursdelv
|
8
|
+
bindir: bin
|
9
|
+
cert_chain: []
|
10
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
11
|
+
dependencies:
|
12
|
+
- !ruby/object:Gem::Dependency
|
13
|
+
name: net-smtp
|
14
|
+
requirement: !ruby/object:Gem::Requirement
|
15
|
+
requirements:
|
16
|
+
- - "~>"
|
17
|
+
- !ruby/object:Gem::Version
|
18
|
+
version: 0.5.1
|
19
|
+
type: :runtime
|
20
|
+
prerelease: false
|
21
|
+
version_requirements: !ruby/object:Gem::Requirement
|
22
|
+
requirements:
|
23
|
+
- - "~>"
|
24
|
+
- !ruby/object:Gem::Version
|
25
|
+
version: 0.5.1
|
26
|
+
description: 'Postgarnet is a lightweight SMTP client for sending emails through Gmail
|
27
|
+
using Ruby. It allows full control over server connection and message headers, supports
|
28
|
+
templates with dynamic payloads, and works out of the box with minimal setup. Designed
|
29
|
+
with simplicity in mind, it''s ideal for quick and flexible email sending in Ruby
|
30
|
+
scripts and applications.
|
31
|
+
|
32
|
+
'
|
33
|
+
executables: []
|
34
|
+
extensions: []
|
35
|
+
extra_rdoc_files: []
|
36
|
+
files:
|
37
|
+
- lib/postgarnet.rb
|
38
|
+
homepage: https://rubygems.org/gems/postgarnet
|
39
|
+
licenses:
|
40
|
+
- CC0-1.0
|
41
|
+
metadata: {}
|
42
|
+
rdoc_options: []
|
43
|
+
require_paths:
|
44
|
+
- lib
|
45
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
46
|
+
requirements:
|
47
|
+
- - ">="
|
48
|
+
- !ruby/object:Gem::Version
|
49
|
+
version: '3.0'
|
50
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
requirements: []
|
56
|
+
rubygems_version: 3.6.8
|
57
|
+
specification_version: 4
|
58
|
+
summary: Lightweight, on-the-go Gmail SMTP client for Ruby
|
59
|
+
test_files: []
|