cromo La guida di Cromo
Scarica PDF EN

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.

cromo
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

cromo
enum Security

How the connection to the server is protected.

  • StartTls: it starts in clear and asks for TLS before anything else: port 587, and 25
  • Tls: TLS from the first byte, the "SSL" of the mail programs: port 465
  • None: in clear, for a server of the same computer or of a closed network

#Refused

cromo
struct Refused : Exception

The 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

cromo
struct Attachment

A 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

cromo
struct Message

A 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 it
  • list<string> to: who receives it
  • list<string> cc: who receives a copy, named in the message
  • list<string> bcc: who receives a copy without being named
  • string replyTo: where the answers go, when it is not from
  • string subject
  • string text: the message as plain text
  • string html: the message as HTML: with text too, a mail program shows the one it can
  • list<Attachment> attachments

#Message.attach

cromo
void attach(string name, string type, list<u8> data)

Adds a file to the message, from its bytes.

#Message.attachFile

cromo
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

cromo
struct Client

A 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 servers
  • Security security
  • string user: the name of the login, often the address; "" for no login
  • string password: its password, or the password of an application
  • string 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 of user, from std.oauth.
  • string name: how the client calls itself, in its greeting
  • i64 timeout: for the connection, and for each read and write, in milliseconds; -1 none

#Client.check

cromo
void check() const

Connects, 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

cromo
void send(Message message) const

Sends 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

cromo
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.