Pike v8.0 release 1738

Class Protocols.SMTP.Server

Description

The use of Protocols.SMTP.server is quite easy and allow you to design custom functions to process mail. This module does not handle mail storage nor relaying to other domains. So it is your job to provide mail storage and relay mails to other servers


Method create

Protocols.SMTP.Server Protocols.SMTP.Server(array(string) _domains, void|int port, void|string ip, function(:void) _cb_mailfrom, function(:void) _cb_rcptto, function(:void) _cb_data)

Description

Create a receiving SMTP server. It implements RFC 2821, 2822 and 1854.

Parameter domain

Domains name this server relay, you need to provide at least one domain (the first one will be used for MAILER-DAEMON address). if you want to relay everything you can put a '*' after this first domain.

Parameter port

Port this server listen on

Parameter listenip

IP on which server listen

Parameter cb_mailfrom

Mailfrom callback function, this function will be called when a client send a mail from command. This function must take a string as argument (corresponding to the sender's email) and return int corresponding to the SMTP code to output to the client. If you return an array the first element is the SMTP code and the second is the error string to display.

Parameter cb_rcptto

Same as cb_mailfrom but called when a client sends a rcpt to.

Parameter cb_data

This function is called each time a client send a data content. It must have the following synopsis: int cb_data(object mime, string sender, array(string) recipients, void|string rawdata) object mime : the mime data object string sender : sender of the mail (from the mailfrom command) array(string) recipients : one or more recipients given by the rcpt to command return : SMTP code to output to the client. If you return an array the first element is the SMTP code and the second is the error string to display.

Example

Here is an example of silly program that does nothing except outputing informations to stdout. int cb_mailfrom(string mail) { return 250; }

int cb_rcptto(string email) { // check the user's mailbox here return 250; }

int cb_data(object mime, string sender, array(string) recipients) { write(sprintf("smtpd: mailfrom=%s, to=%s, headers=%O\ndata=%s\n", sender, recipients * ", ", mime->headers, mime->getdata())); // check the data and deliver the mail here if(mime->body_parts) { foreach(mime->body_parts, object mpart) write("smtpd: mpart data = %O\n", mpart->getdata()); } return 250; }

int main(int argc, array(string) argv) { Protocols.SMTP.Server(({ "ece.fr" }), 2500, "127.0.0.1", cb_mailfrom, cb_rcptto, cb_data); return -1; }