Using an SMTP library to build a mail sender. The problem is it typically doesn't expose a simple interface to pass subject, content, and attachments as parameters -- so had to handle the structure manually.
Format of Internet Message Bodies
According to RFC2045, the MIME (Multipurpose Internet Mail Extensions) format is used.
From: ocfox <i@ocfox.me>
To: Test <test@cyans.dev>
Subject: Test Email
MIME-Version: 1.0
Content-Type: multipart/mixed;
boundary=_kuroneko_
This is a simple email header, using a custom boundary _kuroneko_ to separate different parts.
Header ...
--_kuroneko_
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 7bit
Hello, this is the text part.
--_kuroneko_
Attachment
An attachment is a part of the email and can be a file, an image, or anything else you want to send.
If you want to send an EPUB file, you need to add a part to the email like this:
Header ...
--_kuroneko_
Content-Type: application/epub+zip; name="example.epub"
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename="example.epub"
UEsDBAoAAA...
--_kuroneko_--
Use any Base64 encoding library to encode the file, combine the pieces, then send via SMTP.
Pretty straightforward.