Mailer Interface
Basic Code Sample
Howto
use mailer class in your own programs
To use the mailer class:-
mailer mail("myspiffyfriend@spiffy.com",
"me@somewhere.net",
"There is always room for FooBar",
"Foo\nBar",
"ns.somewhere.net");
mail.operator( )( );
The reason for this curious function call is to facilitate the
class being used with
Boost::Threads http://www.Boost.org.
e.g.
#include <boost\thread\thread.hpp>
boost::thread thrd(mail);
If your not using boost::thread then just call
operator( )( ) directly as above
or use the
send( ) function.
That's it. If there is an error however. The class members
will not throw an exception or report failure. To find if
the mail was delivered correctly use the
response
method
of class mailer, e.g.
if(mail.response( ).substr(0,3) != "250") {
}
Alternatively you can use the second constructor for mailer, passing a vector
as the message body. The example below also shows how to send directly
to an SMTP server rather than querying dns for MX records.
std::vector<char> vec;
std::string mess("Foo\nBar");
for(std::string::size_type i = 0; i < mess.length( ); ++i)
vec.push_back(mess[i]);
jwsmtp::mailer mail("myspiffyfriend@spiffy.com",
"me@somewhere.net",
"There is always room for FooBar",
vec,
"mail.somewhere.net",
jwsmtp::mailer::SMTP_PORT,
To send to mutiple recipients (to multiple email addresses), construct an
object of the mailer class as above. Then call the
addrecipient method
(before calling
mailer::operator( )( ) ) to add email addresses to
send the mail to.
e.g.
mail.addrecipient("someoneElse@somewhere.net");
mail.addrecipient("whooton@somewhere.net", mailer::Bcc);
The mail will now be sent to to these two addresses aswell as the address
passed in the constructor. To remove a recipient from the recipients list call
the removerecipient method with the
exact address
passed into the
constructor or the
addrecipient methods.
e.g.
mail.removerecipient("someoneElse@somewhere.net");
To remove all recipients from the recipients list call
the
clearrecipients method. e.g.
mail.clearrecipients( );
After construction the body of the mail & the subject of
the mail can be changed using the setmessage & setsubject
functions respectively. e.g.
mail.setmessage("The new message that will be received");
mail.setsubject("Always room for improvement");
To send a message in HTML format call the setmessage HTML functions:
mail.setmessageHTML(const std::string& newmessage);
mail.setmessageHTML(const std::vector<char>& newmessage);
mail.setmessageHTMLfile(const std::string& filename);
e.g.
string html("<html>
"<body>"
"html message<br><br>"
"<b>bold</b><br>"
"<i>italic</i><br>"
... etc
"</body>"
"</html>");
mail.setmessageHTML(html);
To add an attachment use the
attach
function e.g.
mail.attach("attachment.jpg");
mail.attach("D:\\adir\\attachment.jpg")
mail.attach("/home/me/myprogram");
When the mail is sent these files will automatically be sent
as part of the message also. Similarly to remove an attachment
call the removeattachment function. e.g.
mail.removeattachment("/home/me/myprogram");
To reset the mailer object (clearing all recipients, message & errors if any)
call the reset function. e.g.
mail.reset( );
To use the mail object again, add recipients and a new message and
optionally a new subject also, then call operator( )( ) to have the new
message mailed to the new recipients. The nameserver/smtpserver
can be changed using the
setserver function, simararily the sender
can be changed using the
setsender function. reset does not
change these values, so sending to a different server or using a
different nameserver will need setserver being called and
likewise to change who the mail is from will need a call to setsender.
If the server to which you are connecting requires authentication set
the username/password pair before calling send. e.g.
mail.username("loginname");
mail.password("secret");
To stop using authentication call the username function with the empty string.
mail.username("");
Currently only LOGIN and PLAIN authentication are supported, LOGIN by default,
to set to PLAIN call the authtype function
mail.authtype("mailer::PLAIN");
mailer public interface:
construction
mailer(const char* TOaddress, const char* FROMaddress,
const char* Subject, const std::vector<char>& Message,
const char* server = "127.0.0.1",
unsigned short Port = SMTP_PORT,
bool MXLookup = true);
mailer(const char* TOaddress, const char* FROMaddress,
const char* Subject, const char* Message,
const char* server = "127.0.0.1",
unsigned short Port = SMTP_PORT,
bool MXLookup = true);
mailer(bool MXLookup = false, unsigned short Port = SMTP_PORT);
~mailer( );
bool addrecipient(const std::string& newrecipient, short recipient_type = mailer::TO );
bool removerecipient(const std::string& recipient);
void clearrecipients( );
bool attach(const std::string& filename);
bool removeattachment(const std::string& filename);
void clearattachments( );
bool setmessage(const std::string& newmessage);
bool setmessage(const std::vector<char>& newmessage);
bool setsubject(const std::string& newSubject);
bool setmessageHTML(const std::string& newmessage);
bool setmessageHTML(const std::vector<char>& newmessage);
bool setmessageHTMLfile(const std::string& filename);
void reset( );
bool setserver(const std::string& nameserver_or_smtpserver);
bool setsender(const std::string& newsender);
void operator( )( );
void send( );
const std::string& response( ) const;
void username(const std::string& User);
void password(const std::string& Pass);
void authtype(const enum authtype Type);
JohnWiggins.net
bugs, questions email