andrewtimberlake / mailroom

Mail handling for Elixir
MIT License
21 stars 18 forks source link

[SMTP] Structured email creation #4

Closed srivathsanmurali closed 5 years ago

srivathsanmurali commented 5 years ago

This PR goes after #3. A new Email module is implemented that makes it easier to create new emails in a structured manner and send via SMTP.

This also makes it easier to add some add check email fields.

srivathsanmurali commented 5 years ago

This is still a WIP. I thougth I would send in the PR to see what you think about this approach to the problem as I continue to work on this. I am currently building a elixir service that needs a SMTP client and this works really well.

I hope I contribute to your project :smile:

I am working to update the tests to use the new SMTP/Email methods I implemetented in this PR.

Cheers

srivathsanmurali commented 5 years ago

This PR is ready to be reviewed. This branch is based on the branch used in #3, I will rebase this PR based on how #3 goes.

I am willing to help further with the SMTP Module specifically if you require more help.

andrewtimberlake commented 5 years ago

Rather than handling this within Mailroom, I’ve been working extensively with the Dockyard/elixir-mail project which already does structured mail parsing and encoding. It is already included as a dependency (as :mail)

srivathsanmurali commented 5 years ago

Sweet, didn't notice that. Alright. I think I will close this PR.

srivathsanmurali commented 5 years ago

@andrewtimberlake I wonder if handling multiple recipients be inline with the objectives of the :mailroom package.

Would it make sense for me to rework this PR to expose an function that takes an %Mail.Message{} as an argument which would help in sending RCPT commands for each of the recipients and use Mail.Renders for encoding the message.

andrewtimberlake commented 5 years ago

Which recipients would you send? When sending a message with multiple recipients (to, cc, bcc), each of them might need to go to a different SMTP server.

srivathsanmurali commented 5 years ago

I am new to the mail processing and I may have misunderstood but this is my understanding.

  1. The SMTP client sends a RCPT command for each recipient to the Mail Submission Agent (the SMTP server we connect to) as part of the SMTP session.
  2. The MSA delivers the message to the Mail Transfer Agent which handles that delivery to the respective exchange servers of recipients with different hosts.

Which would mean, this module that acts the SMTP client doesn't need to worry sending to different SMTP servers.

andrewtimberlake commented 5 years ago

When you send an email to: bob@google.com and cc: john@facebook.com, both of those domains will have a different receiving SMTP server.

The process would be

  1. Create an RFC2822 message with both the to and cc headers
  2. Query DNS for the MX records for google.com
  3. Send the message created in (1) to the google.com SMTP server with RCPT TO of bob@google.com
  4. Query DNS for the MX records for facebook.com
  5. Send the same message created in(2) to the facebook.com SMTP server with RCPT TO of john@facebook.com

Often you will use a relay SMTP server (such as from your ISP). This server will accept any RCPT TO (usually after AUTH) and it will perform the above steps on your behalf.

I’m happy to work on a send command that takes a %Mail.Message{} and sends to all recipients but we need to make sure we handle situations where the SMTP server rejects some or all of the recipients.

srivathsanmurali commented 5 years ago

Thanks for clarification.

I had implemented the send method just as you described just a bit earlier. https://github.com/srivathsanmurali/mailroom/blob/multiple_recipients_email/lib/mailroom/smtp.ex#L242

I will work on the error handling for the case when SMTP servers reject on some/all recipients.