1 // Note that the only valid version of the GPL as far as jwSMTP
2 // is concerned is v2 of the license (ie v2, not v2.2 or v3.x or whatever),
3 // unless explicitly otherwise stated.
5 // This file is part of the jwSMTP library.
7 // jwSMTP library is free software; you can redistribute it and/or modify
8 // it under the terms of the GNU General Public License as published by
9 // the Free Software Foundation; version 2 of the License.
11 // jwSMTP library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with jwSMTP library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 // http://johnwiggins.net
22 // smtplib@johnwiggins.net
24 // Modified by Tom Cort <tom.cort@state.vt.us 30-Oct-2008.
25 // Renamed headers to .hxx and removed WIN32 specific code.
27 #ifndef __MAILER_HXX__
28 #define __MAILER_HXX__
39 // if MXLookup is true:
40 // 'server' is a nameserver to lookup an MX record by.
41 // if MXLookup is false.
42 // 'server' is an SMTP server which will be attempted directly for mailing
43 // if an IP address is not found, either MX record or direct to SMTP server,
44 // an attempt will be made to send mail directly to the server in the mail address.
45 // e.g. mail to fred@somewhere.com will have a connection attempt made directly to:
46 // somewhere.com (which is probably wrong and therefore will still fail)
47 mailer(const char* TOaddress
, const char* FROMaddress
,
48 const char* Subject
, const std::vector
<char>& Message
,
49 const char* server
= "127.0.0.1"/*default to localhost*/,
50 unsigned short Port
= SMTP_PORT
, // default SMTP port
51 bool MXLookup
= true);
53 mailer(const char* TOaddress
, const char* FROMaddress
,
54 const char* Subject
, const char* Message
,
55 const char* server
= "127.0.0.1"/*default to localhost*/,
56 unsigned short Port
= SMTP_PORT
, // default SMTP port
57 bool MXLookup
= true);
59 // defaults to SMTP_PORT & no MX lookup.
61 // mailer m; // mail an smtp server direct.
62 // mailer m2(true); // use MX lookup.
63 // mailer m2(false, weirdportnumber); // SMTP to a non standard port.
64 mailer(bool MXLookup
= false, unsigned short Port
= SMTP_PORT
);
68 // call this operator to have the mail mailed.
69 // This is to facilitate using multiple threads
70 // i.e. using boost::thread. (see http://www.boost.org)
73 // mailer mail(args...);
74 // boost::thread thrd(mail); // operator()() implicitly called.
75 // thrd.join(); // if needed.
78 // mailer mail(args...);
83 // attach a file to the mail. (MIME 1.0)
84 // returns false if !filename.length() or
85 // the file could not be opened for reading...etc.
86 bool attach(const std::string
& filename
);
88 // remove an attachment from the list of attachments.
89 // returns false if !filename.length() or
90 // the file is not attached or there are no attachments.
91 bool removeattachment(const std::string
& filename
);
93 // Set a new message (replacing the old)
94 // will return false and not change the message if newmessage is empty.
95 bool setmessage(const std::string
& newmessage
);
96 bool setmessage(const std::vector
<char>& newmessage
);
98 // Set a new HTML message (replacing the old)
99 // will return false and not change the message if newmessage is empty.
100 bool setmessageHTML(const std::string
& newmessage
);
101 bool setmessageHTML(const std::vector
<char>& newmessage
);
102 // use a file for the data
103 bool setmessageHTMLfile(const std::string
& filename
);
105 // Set a new Subject for the mail (replacing the old)
106 // will return false if newSubject is empty.
107 bool setsubject(const std::string
& newSubject
);
109 // sets the nameserver or smtp server to connect to
110 // dependant on the constructor call, i.e. whether
111 // 'lookupMXRecord' was set to false or true.
112 // (see constructor comment for details)
113 bool setserver(const std::string
& nameserver_or_smtpserver
);
115 // sets the senders address (fromAddress variable)
116 bool setsender(const std::string
& newsender
);
118 // add a recipient to the recipient list. (maximum allowed recipients 100).
119 // returns true if the address could be added to the
120 // recipient list, otherwise false.
121 // recipient_type must be in the range mailer::TO -> mailer::BCC if
122 // not recipient_type defaults to BCC (blind copy), see const enum below.
123 bool addrecipient(const std::string
& newrecipient
, short recipient_type
= TO
/*CC, BCC*/);
125 // remove a recipient from the recipient list.
126 // returns true if the address could be removed from the
127 // recipient list, otherwise false.
128 bool removerecipient(const std::string
& recipient
);
130 // clear all recipients from the recipient list.
131 void clearrecipients();
133 // clear all attachments from the mail.
134 void clearattachments();
136 // clear all recipients, message, attachments, errors.
137 // does not reset the name/smtp server (use setserver for this)
138 // does not set the senders address (use setsender for this)
141 // returns the return code sent by the smtp server or a local error.
142 // this is the only way to find if there is an error in processing.
143 // if the mail is sent correctly this string will begin with 250
144 // see smtp RFC 821 section 4.2.2 for response codes.
145 const std::string
& response() const;
148 // in unix we have to have a named object.
149 const static enum {TO
, Cc
, Bcc
, SMTP_PORT
= 25, DNS_PORT
= 53} consts
;
151 // what type of authentication are we using.
152 // (if using authentication that is).
153 enum authtype
{LOGIN
= 1, PLAIN
} type
;
155 // set the authentication type
156 // currently LOGIN or PLAIN only.
157 // The default login type is LOGIN, set in the constructor
158 void authtype(const enum authtype Type
);
160 // set the username for authentication.
161 // If this function is called with a non empty string
162 // jwSMTP will try to use authentication.
163 // To not use authentication after this, call again
164 // with the empty string e.g.
165 // mailerobject.username("");
166 void username(const std::string
& User
);
167 // set the password for authentication
168 void password(const std::string
& Pass
);
171 // create a header with current message and attachments.
172 std::vector
<char> makesmtpmessage() const;
174 // this breaks a message line up to be less than 1000 chars per line.
175 // keeps words intact also --- rfc821
176 // Check line returns are in the form "\r\n"
177 // (qmail balks otherwise, i.e. LAME server)
178 // also if a period is on a line by itself add a period
179 // stops prematurely ending the mail before whole message is sent.
180 void checkRFCcompat();
183 // returns the part of the string toaddress after the @ symbol.
184 // i.e. the 'toaddress' is an email address eg. someone@somewhere.com
185 // this function returns 'somewhere.com'
186 std::string
getserveraddress(const std::string
& toaddress
) const;
188 // Does the work of getting MX records for the server returned by 'getserveraddress'
189 // will use the dns server passed to this's constructor in 'nameserver'
190 // or if MXlookup is false in the constuctor, will return an address
191 // for the server that 'getserveraddress' returns.
192 // returns false on failure, true on success
193 bool gethostaddresses(std::vector
<SOCKADDR_IN
>& adds
);
195 // Parses a dns Resource Record (see TCP/IP illustrated, STEVENS, page 194)
196 bool parseRR(int& pos
, const unsigned char dns
[], std::string
& name
, in_addr
& address
);
198 // Parses a dns name returned in a dns query (see TCP/IP illustrated, STEVENS, page 192)
199 void parsename(int& pos
, const unsigned char dns
[], std::string
& name
);
201 // email address wrapper struct
203 std::string name
; // e.g. freddy foobar
204 std::string address
; // e.g. someone@mail.com
207 // authenticate against a server.
208 bool authenticate(const std::string
& servergreeting
, const SOCKET
& s
);
210 // less typing later, these are definately abominations!
211 typedef std::vector
<std::pair
<std::vector
<char>, std::string
> >::const_iterator vec_pair_char_str_const_iter
;
212 typedef std::vector
<std::pair
<Address
, short> >::const_iterator recipient_const_iter
;
213 typedef std::vector
<std::pair
<Address
, short> >::iterator recipient_iter
;
214 typedef std::vector
<std::string
>::const_iterator vec_str_const_iter
;
216 // split an address into its relevant parts i.e.
217 // name and actual address and return it in Address.
218 // this may be usefull out of the class maybe
219 // it should be a static function or a global? thinking about it.
220 Address
parseaddress(const std::string
& addresstoparse
);
222 // The addresses to send the mail to
223 std::vector
<std::pair
<Address
, short> > recipients
;
224 // The address the mail is from.
226 // Subject of the mail
228 // The contents of the mail message
229 std::vector
<char> message
;
230 // The contents of the mail message in html format.
231 std::vector
<char> messageHTML
;
232 // attachments: the file as a stream of char's and the name of the file.
233 std::vector
<std::pair
<std::vector
<char>, std::string
> > attachments
;
234 // This will be filled in from the toAddress by getserveraddress
236 // Name of a nameserver to query
237 std::string nameserver
;
238 // The port to mail to on the smtp server.
239 const unsigned short port
;
240 // use dns to query for MX records
241 const bool lookupMXRecord
;
242 // using authentication
244 // username for authenticated smtp
246 // password for authenticated smtp
248 // filled in with server return strings
249 std::string returnstring
;
252 } // end namespace jwsmtp
254 #endif // !ifndef __MAILER_HXX__