std.smtp
Sends email through a server of SMTP: a message with a text, an HTML version and attachments, to the server of a provider or of a company, with TLS and a login.
import std.smtp
smtp.Client mail
mail.host = "smtp.example.com" // port 587 with STARTTLS, unless told otherwise
mail.user = "anna@example.com"
mail.password = "the password"
smtp.Message message
message.from = "Anna Rossi <anna@example.com>"
message.to = ["bruno@example.com"]
message.subject = "The report"
message.text = "Here it is."
message.attachFile("report.pdf", "application/pdf")
mail.send(message)The guide: Networking and HTTP.
Generata dalla documentazione nei sorgenti della libreria, che è in inglese.
#Tipi
#Security
enum SecurityHow the connection to the server is protected.
StartTls: it starts in clear and asks for TLS before anything else: port 587, and 25Tls: TLS from the first byte, the "SSL" of the mail programs: port 465None: in clear, for a server of the same computer or of a closed network
#Refused
struct Refused : ExceptionThe answer of a server that refuses: code is the one of SMTP, as 550 for an address that does not exist or 535 for a wrong password. Those from 400 to 499 are of the moment, and the message can be sent again later; those from 500 are final.
i32 code
#Attachment
struct AttachmentA file sent with a message.
string name: the name that who receives sees: "report.pdf"string type: "application/pdf"; "" is "application/octet-stream"list<u8> data: its bytes
#Message
struct MessageA message of email. An address is "anna@example.com" or, with the name that the mail programs show, "Anna Rossi \<anna@example.com>".
string from: who sends itlist<string> to: who receives itlist<string> cc: who receives a copy, named in the messagelist<string> bcc: who receives a copy without being namedstring replyTo: where the answers go, when it is notfromstring subjectstring text: the message as plain textstring html: the message as HTML: withtexttoo, a mail program shows the one it canlist<Attachment> attachments
#Message.attach
void attach(string name, string type, list<u8> data)Adds a file to the message, from its bytes.
#Message.attachFile
void attachFile(string path, string type)Adds a file to the message, read from path, with the name of the file. Throws as fs.readBytes when it cannot be read.
#Client
struct ClientA server of SMTP to send through. One declared without a value goes to port 587 with STARTTLS; host must be set. Its methods are const: the tasks of a server share one. Each send opens a connection of its own and closes it.
string host: "smtp.example.com"i32 port: 587 with StartTls, 465 with Tls, 25 between serversSecurity securitystring user: the name of the login, often the address; "" for no loginstring password: its password, or the password of an applicationstring token: A token of OAuth 2.0 in place of the password, for the providers that take no password, as Google and Microsoft 365: the access token ofuser, from std.oauth.string name: how the client calls itself, in its greetingi64 timeout: for the connection, and for each read and write, in milliseconds; -1 none
#Client.check
void check() constConnects, starts TLS and logs in, as send does, and leaves: to know that the server, the port, the name and the password are right before the first message. Throws what send throws.
#Client.send
void send(Message message) constSends a message to all its receivers, those of to, cc and bcc. Throws Refused, with the code of SMTP, when the server says no, as to a wrong password or to an address it does not take: then nobody gets the message. An Error for a message that cannot be written, for a server that does not offer what was asked, as STARTTLS, and for a failure of the network or a timeout.
#Funzioni
#encode
string encode(Message message)The message as the text that is sent, with its headers and the parts of MIME, the lines ended by "\r\n". send does it by itself: a program calls it to keep a copy of what it sent, or to send it in another way. Throws an Error for a message without a sender or a receiver, for an address that is not one, and for an end of line in the subject, in an address or in a name.