Skip to content

Instantly share code, notes, and snippets.

Created June 19, 2011 15:56
Show Gist options
  • Save anonymous/1034433 to your computer and use it in GitHub Desktop.
Save anonymous/1034433 to your computer and use it in GitHub Desktop.
smtp protocol
/**
Basic SMTP protocol support
*/
struct SMTP {
mixin Protocol;
private bool ssl = false;
private string _message;
/**
Sets to the url of the SMTP server
*/
this(string url) {
curl = Curl(true);
if (url.startsWith("smtps://"))
ssl = true;
else
enforce(url.startsWith("smtp://"), "The url must be for the smtp protocol.");
curl.set(CurlOption.url, url);
if (ssl) {
curl.set(CurlOption.use_ssl, CurlUseSSL.all);
curl.set(CurlOption.ssl_verifypeer, false);
curl.set(CurlOption.ssl_verifyhost, 2);
}
}
/**
Setter for the sender's email address
*/
@property void mailFrom(string sender) {
// The sender address should be encapsulated with < and >
if (!(sender[0] == '<' && sender[$ - 1] == '>'))
sender = '<' ~ sender ~ '>';
curl.set(CurlOption.mail_from, sender);
}
/**
Setter for the recipient email addresses
*/
@property void mailTo(string[] recipients) {
curl_slist* recipients_list = null;
foreach(recipient; recipients) {
if (!(recipient[0] == '<' && recipient[$-1] == '>'))
recipient = '<' ~ recipient ~ '>';
recipients_list = curl_slist_append(recipients_list, cast(char*)toStringz(recipient));
}
curl.set(CurlOption.mail_rcpt, cast(void*)recipients_list);
}
/**
Sets the message body text
*/
@property void message(string msg) {
_message = msg;
/**
This delegate reads the message text and copies it.
*/
curl.onSend = delegate size_t(void[] data) {
if (!msg.length) return 0;
auto m = cast(void[])msg;
size_t to_copy = min(data.length, _message.length);
data[0..to_copy] = _message[0..to_copy];
_message = _message[to_copy..$];
return to_copy;
};
}
/**
Performs the request as configured
*/
void perform() {
curl.perform;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment