Skip to content

richwalm/ssmtp

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Simple SMTP Mailer
Written by Richard Walmsley <richwalm@gmail.com>
WWW: http://walmsley.gen.nz/

Description
===========
A simple API used to send out e-mails. It supports attachments but lacking authentication as it wasn't required for my use.
It's Windows only due to the use of its DNS lookup functions, although outside of those, it should be easy to port, although there are far superior tools out there.
Not tested but should be thread-safe.

Included an example.

API Usage
=========
All functions return SMTP_ERR_SUCCESS (0) on success.

The error codes are as follows;
* SMTP_ERR_INVALID_STATE	- Function called without a successful call to a prerequisite function.
* SMTP_ERR_FAILURE		- Server returned a non-success code or unable to connect to server.
* SMTP_ERR_BUFFER		- Out of memory or static buffer too small. The latter shouldn't happen with a valid e-mail address.
* SMTP_ERR_PROTOCOL		- Protocol error. Server not following the spec or there's a transfer error.
* SMTP_ERR_DATA			- The data passed to the function is invalid.

The socket used has a receive timeout of 15 seconds. This can be adjusted by defining SMTP_BLOCKING_TIME before including ssmtp.h. The value is in milliseconds.

int SMTPConnect(SMTPConn *Conn, const char *Domain, const char *HeloLine);
--------------------------------------------------------------------------
Attempts to connects to the most suitable mail server.

Before calling this function for the first use of a SMTPConn, ensure that SMTPConn->State is set to 0 otherwise the function may fail with SMTP_ERR_INVALID_STATE. It only needs to be set once per SMTPConn as it is then handled internally.

This looks up the MX records for the domain passed and then attempts to connect to them sorted by their preference. If one fails, it'll continue to the next one. If none of them work, it'll try to connect to the server's A records as per the spec.

Once connected, it'll send through the HELO line using the passed string. If the server returns an unsuccessful code, it'll disconnect and continue through the list.

int SMTPAddress(SMTPConn *Conn, int Type, const char *Address);
---------------------------------------------------------------
Adds a single e-mail address to the buffer.

The types are as follows;
* SMTP_ADDRESS_FROM
* SMTP_ADDRESS_TO
* SMTP_ADDRESS_CC
* SMTP_ADDRESS_BCC

There's no limit other then the available memory. BCC addresses aren't included into its buffer.
The senders' address always needs to be sent first otherwise the function will fail with SMTP_ERR_INVALID_STATE.

The address can be in either format; 'test@example.org' or '"Testing Account" <test@example.org>'.
There is no checking to see if an address has been added twice.

int SMTPData(SMTPConn *Conn, const char *Subject, const char *Body, SMTPAttach *Attachments);
---------------------------------------------------------------------------------------------
Sends off the e-mail.

Before calling this, we must have called SMTPAddress() at least twice returning successful. First with a sender and then again with a receiver.

The subject line is optional. If Attachments is NULL, the e-mail will be a standard e-mail, without MIME.
See the included example for further infomation on attachments.

int SMTPReset(SMTPConn *Conn);
------------------------------
Sends the SMTP RSET command. This clears any addresses that may have been added to the buffer. As if starting with a fresh connection.

int SMTPDisconnect(SMTPConn *Conn);
-----------------------------------
Disconnects from the mail server.

This will free any memory used by the address buffer. The SMTPConn can then be reused.

License
=======
Distributed under the MIT License. See the included LICENSE for details.