Skip to content

Issues integrating with Mailgun #35

Closed
mirage/colombe
#41

Description

@jsthomas

I'm trying to use letters to send email via Mailgun (specifically, a sandbox domain that's available on their free tier). I created a simple test program based on the examples in the readme to send a single email to myself:

let body = Letters.Plain {|
Hi there,

This is a test email from https://github.com/oxidizing/letters

Regards,
The Letters team
|}


let send_email () =
  let config = (Letters.Config.make
      ~username:"REDACTED"
      ~password:"REDACTED"
      ~hostname:"smtp.mailgun.org"
      ~with_starttls:true)
  in
  let sender = "REDACTED - Sandbox Domain" in
  let recipients = [Letters.To "[email protected]"] in
  let subject = "Test email from mailgun." in
  let mail = Letters.build_email ~from:sender ~recipients ~subject ~body in

  Printexc.record_backtrace true;
  match mail with
  | Ok message ->
    (try%lwt
       Letters.send ~config ~sender ~recipients ~message
    with
      e ->
      Printf.printf "Error. %s\n" (Printexc.to_string e);
      Printexc.print_backtrace stdout;
      Lwt.return_unit
   )
  | Error _ -> Printf.printf "Message synthesis failed."; flush stdout; Lwt.return_unit


let () =
  Lwt_main.run @@ send_email ()

When I tried to run this program, I see:

Error. (Invalid_argument "Malformed input")
Raised at Stdlib__string.index_rec in file "string.ml", line 115, characters 19-34
Called from Sexplib0__Sexp.Printing.index_of_newline in file "src/sexp.ml", line 113, characters 13-47

Is there some way for me to get a clearer picture of what's causing the sending process to fail?

Details:

  • I'm using version 4.11.1 of the compiler, version 0.2.1 of letters, and version 5.4.0 of lwt.
  • I'm using a Debian instance for development (Linux 4.19.0-5-amd64 #1 SMP Debian 4.19.37-5+deb10u2 (2019-08-08) x86_64 GNU/Linux)

Things I've tried:

  • Changing with_starttls.
  • Adding CA certs to the configuration (specifically these).
  • Confirming the credentials I received from Mailgun work (their docs provide a python stub one can use for this).
  • Running the test program under strace. I'm not an strace expert but it looks like my program was able to successfully connect to smtp.mailgun.org, send EHLO and STARTTLS, and get back a 220 Go Ahead before things went wrong.

Please let me know if I can help provide more diagnostic information.

Activity

  1. dinosaure commented on Jul 26, 2021

    @dinosaure

    I will take a look on your bug 👍.

  2. dinosaure commented on Jul 26, 2021

    @dinosaure

    The bug is about mailgun. Indeed, it does not strictly follow the RFC 4954 which describe the 334 response as is: https://datatracker.ietf.org/doc/html/rfc4954#section-8 (see continue-req)

    Indeed, 334 expects optionally a base64 value but mailgun send to us: 334 Go ahead\r\n (and "Go ahead" is not a valid base64 value). So ocaml-base64 raises an exception Malformed input (and, I believe that the stack trace disappear due to lwt).

    The solution is to be more resilient in colombe and just ignore invalid base64 values. I will propose a patch on colombe.

  3. jsthomas commented on Jul 26, 2021

    @jsthomas
    Author

    Thanks for taking a look, @dinosaure, I updated the version of colombe on my system and can confirm that it's working for me now. It's too bad that mailgun doesn't respect RFC 4954, one would think they have plenty of engineering resources to be able to do that.

    I'm not sure if the convention is for the maintainer or the person who opened the issue to close, but as far as I'm concerned this can be closed now.

  4. joseferben commented on Jul 26, 2021

    @joseferben
    Contributor

    Thanks both!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions