Mail

Writing emails, with attachments, and sending them through an SMTP server.

x has its own SMTP client, the same on Windows and Linux. A mail goes out compatibly with PureBasic 6.50, byte for byte apart from the boundary between its parts:

  • the same headers, in the same order;
  • the text as quoted-printable, in UTF-8 or ISO-8859-1;
  • attachments as base64, in a multipart/mixed message;
  • recipients given in the same order;
  • the same AUTH (PLAIN, otherwise LOGIN).

Where it stands:

  • TLS (#PB_Mail_UseSSL: STARTTLS; #PB_Mail_UseSMTPS: TLS from the start, usually port 465): on Windows, through Windows' own TLS (SChannel), checking the server's certificate unless #PB_Mail_NoSSLCheck. On Linux, not yet: sending fails.
  • #PB_Mail_WeakSSL and #PB_Mail_Debug do nothing.
  • On Linux, PureBasic needs libcurl for mail; x needs nothing.

Writing a mail

CreateMail ✅, FreeMail ✅, IsMail ✅

Result = CreateMail(#Mail, From$, Subject$ [, Encoding])
FreeMail(#Mail)
Result = IsMail(#Mail)

From$ is an address in one of the forms joe@domain.com, <joe@domain.com> or Joe Doe <joe@domain.com>. Encoding: #PB_UTF8 (the default) or #PB_Ascii (ISO-8859-1). FreeMail(#PB_All) frees all of them.

SetMailBody ✅, GetMailBody ✅

SetMailBody(#Mail, Body$)
Body$ = GetMailBody(#Mail)

The text. Lines should end with #CRLF$ (#LF$ works too).

SetMailAttribute ✅, GetMailAttribute ✅

SetMailAttribute(#Mail, Attribute, Value$)
Value$ = GetMailAttribute(#Mail, Attribute)
Attribute
#PB_Mail_From (0) the sender, as given to CreateMail
#PB_Mail_Subject (1) the subject
#PB_Mail_XMailer (2) an X-Mailer: header (none unless set)
#PB_Mail_Date (3) the Date: header. Unless set, it's the time of sending (and GetMailAttribute returns "")
#PB_Mail_Custom (4) headers of your own, one per line: "X-One: 1" + #CRLF$ + "X-Two: 2"
OpenConsole()
If CreateMail(0, "Joe Doe <joe@example.com>", "Hello")
  SetMailBody(0, "First line" + #CRLF$ + "Second line")
  SetMailAttribute(0, #PB_Mail_XMailer, "MyMailer")
  PrintN(GetMailAttribute(0, #PB_Mail_From))
  PrintN(GetMailAttribute(0, #PB_Mail_Subject) + " / " + GetMailAttribute(0, #PB_Mail_XMailer) + " / [" + GetMailAttribute(0, #PB_Mail_Date) + "]")
  PrintN(Str(CountString(GetMailBody(0), #CRLF$) + 1) + " lines")
  FreeMail(0)
EndIf

Output:

Joe Doe <joe@example.com>
Hello / MyMailer / []
2 lines

AddMailRecipient ✅, RemoveMailRecipient ✅

AddMailRecipient(#Mail, Address$, Flags)
RemoveMailRecipient(#Mail [, Address$ [, Flags]])

Flags: any of #PB_Mail_To (1), #PB_Mail_Cc (2) and #PB_Mail_Bcc (4). Bcc recipients get the mail but appear in no header. RemoveMailRecipient with no address removes everyone; with an address and flags, it takes the address off just those lists.

AddMailAttachment ✅, AddMailAttachmentData ✅

Result = AddMailAttachment(#Mail, Description$, Filename$ [, MimeType$])
Result = AddMailAttachmentData(#Mail, Description$, *Buffer, Length [, MimeType$])

Attaches a file or bytes, copied at once (up to 100 MB each). The description is the name the attachment gets in the mail. Returns nonzero on success, or 0 if the file can't be read.

  • MIME type, for a file: taken from its extension, as in PureBasic's table (png is image/png, pdf is application/pdf, and so on). Anything else is application/octet-stream.
  • MIME type, for data: application/octet-stream, unless one is given.

Sending

SendMail ✅, MailProgress ✅

Result = SendMail(#Mail, Smtp$ [, Port [, Flags [, User$, Password$]]])
Result = MailProgress(#Mail)

Sends the mail through the server Smtp$ (port 25 unless given), signing in first if a user is given. Returns nonzero once the server has taken the mail.

As in PureBasic, it fails (0) when:

  • there are no recipients;
  • the server refuses any one recipient, even if it accepted the others;
  • the server can't be reached.
Flags
#PB_Mail_Asynchronous (1) don't wait: the mail goes as MailProgress is called
#PB_Mail_UseSSL (2) TLS after connecting (STARTTLS), usually port 587
#PB_Mail_UseSMTPS (4) TLS from the start, usually port 465 (with #PB_Mail_UseSSL)
#PB_Mail_NoSSLCheck (16) don't check the server's certificate

MailProgress returns:

  • the bytes sent so far;
  • #PB_Mail_Connected (-1) before sending;
  • #PB_Mail_Finished (-3) once sent;
  • #PB_Mail_Error (-2) after a failure.
If CreateMail(0, "me@example.com", "Report")
  AddMailRecipient(0, "boss@example.com", #PB_Mail_To)
  SetMailBody(0, "Attached.")
  AddMailAttachment(0, "report.pdf", "C:\Reports\report.pdf")
  If SendMail(0, "smtp.example.com", 587, #PB_Mail_UseSSL, "me@example.com", Password$)
    PrintN("sent")
  EndIf
EndIf

eXperimental is an independent implementation: no proprietary PureBasic source code or implementation code is incorporated into it. PureBasic is a product of Fantaisie Software. eXperimental is neither affiliated with nor endorsed by Fantaisie Software.