Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issues integrating with Mailgun #35

Closed
jsthomas opened this issue Jul 23, 2021 · 4 comments · Fixed by mirage/colombe#41
Closed

Issues integrating with Mailgun #35

jsthomas opened this issue Jul 23, 2021 · 4 comments · Fixed by mirage/colombe#41

Comments

@jsthomas
Copy link

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 "my-personal-email@xyz.com"] 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.

@dinosaure
Copy link

I will take a look on your bug 👍.

@dinosaure
Copy link

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.

@jsthomas
Copy link
Author

jsthomas commented Jul 26, 2021

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.

@joseferben
Copy link
Contributor

Thanks both!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants