Class: Submail::MailXSend

Inherits:
Object
  • Object
show all
Includes:
Helper
Defined in:
lib/submail/mail_x_send.rb

Instance Method Summary collapse

Methods included from Helper

#create_signatrue, #get_timestamp, #http_get, #http_post, #http_request

Constructor Details

#initialize(config) ⇒ MailXSend

Returns a new instance of MailXSend.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/submail/mail_x_send.rb', line 5

def initialize(config)
  @to = []
  @addressbook = []
  @from = ""
  @fromname = ""
  @reply = ""
  @cc = []
  @bcc = []
  @subject = ""
  @project = ""
  @vars ={}
  @links = {}
  @headers = {}
  @config = config
end

Instance Method Details

#add_addressbook(addressbook) ⇒ Object



28
29
30
31
# File 'lib/submail/mail_x_send.rb', line 28

def add_addressbook(addressbook)
  @addressbook = []
  @addressbook << addressbook
end

#add_bcc(address, name) ⇒ Object



49
50
51
52
53
54
# File 'lib/submail/mail_x_send.rb', line 49

def add_bcc(address, name)
  bcc = {}
  bcc["address"] = address
  bcc["name"] = name
  @bcc << bcc
end

#add_cc(address, name) ⇒ Object



42
43
44
45
46
47
# File 'lib/submail/mail_x_send.rb', line 42

def add_cc(address, name)
  cc = {}
  cc["address"] = address
  cc["name"] = name
  @cc << cc
end

#add_header(key, value) ⇒ Object



72
73
74
# File 'lib/submail/mail_x_send.rb', line 72

def add_header(key, value)
  @headers[key] = value
end


68
69
70
# File 'lib/submail/mail_x_send.rb', line 68

def add_link(key, value)
  @links[key] = value
end

#add_to(address, name) ⇒ Object



21
22
23
24
25
26
# File 'lib/submail/mail_x_send.rb', line 21

def add_to(address, name)
  to = {}
  to["address"] = address
  to["name"] = name
  @to << to
end

#add_var(key, value) ⇒ Object



64
65
66
# File 'lib/submail/mail_x_send.rb', line 64

def add_var(key, value)
  @vars[key] = value
end

#build_requestObject



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/submail/mail_x_send.rb', line 76

def build_request
  request = {}
  if @to.length != 0
    to = []
    @to.each do |k| 
      to << "%s<%s>" %[k["name"], k["address"]]
    end
    request["to"] = to.join(",")
  end
  if @addressbook.length != 0
    request["addressbook"] = @addressbook.join(",")
  end
  if @from != ""
    request["from"] = @from
  end
  if @fromname != ""
    request["from_name"] = @fromname
  end
  if @reply != ""
    request["reply"] = @reply
  end
  if @cc.length != 0
    cc = []
    @cc.each do |k| 
      cc << "%s<%s>" %[k["name"], k["address"]]
    end
    request["cc"] = cc.join(",")
  end
  if @bcc.length != 0
    bcc = []
    @bcc.each do |k| 
      bcc << "%s<%s>" %[k["name"], k["address"]]
    end
    request["bcc"] = bcc.join(",")
  end
  if @subject != ""
    request["subject"] = @subject
  end
  if @project != ""
    request["project"] = @project
  end
  if @vars.length != 0
    request["vars"] = JSON.generate @vars
  end
  if @links.length != 0
    request["links"] = JSON.generate @links
  end
  
  if @headers.length != 0
    request["headers"] = JSON.generate @headers
  end
  request
end

#mail_xsendObject



130
131
132
133
134
135
136
137
# File 'lib/submail/mail_x_send.rb', line 130

def mail_xsend
  request = self.build_request()
  url = "https://api.submail.cn/mail/xsend.json"
  request["appid"] = @config["appid"]
  request["timestamp"] = get_timestamp()
  request["signature"] = create_signatrue(request, @config)
  JSON.parse http_post(url, request)
end

#set_project(project) ⇒ Object



60
61
62
# File 'lib/submail/mail_x_send.rb', line 60

def set_project(project)
  @project = project
end

#set_reply(reply) ⇒ Object



38
39
40
# File 'lib/submail/mail_x_send.rb', line 38

def set_reply(reply)
  @reply = reply
end

#set_sender(from, fromname) ⇒ Object



33
34
35
36
# File 'lib/submail/mail_x_send.rb', line 33

def set_sender(from, fromname)
  @from = from
  @fromname = fromname
end

#set_subject(subject) ⇒ Object



56
57
58
# File 'lib/submail/mail_x_send.rb', line 56

def set_subject(subject)
  @subject = subject
end