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

Add eml parser to generate Msg from .eml files #145

Closed
wneessen opened this issue Sep 13, 2023 · 6 comments · Fixed by #249
Closed

Add eml parser to generate Msg from .eml files #145

wneessen opened this issue Sep 13, 2023 · 6 comments · Fixed by #249
Assignees
Labels
enhancement New feature or request hacktoberfest PRs are requested to be eligible to participate in Hacktoberfest hacktoberfest-accepted PR has been accepted to participate in Hacktoberfest WIP Work is in progress

Comments

@wneessen
Copy link
Owner

Is your feature request related to a problem? Please describe.

As requested on discord, it would be nice to generate a new Msg from a given .eml file.

Describe the solution you'd like

Ideally the parser would read all headers supported by go-mail and add them to the Msg automatically and then fill the body with the mail contents of the .eml. Parsing attachments could be tricky though.

Describe alternatives you've considered

No response

Additional context

No response

@wneessen wneessen added the enhancement New feature or request label Sep 13, 2023
@wneessen wneessen self-assigned this Sep 13, 2023
wneessen added a commit that referenced this issue Oct 13, 2023
Added support for quoted-printable encoding in email parser to increase its functionality. The change includes a case handling feature for 'EncodingQP' and related conversions to allow for proper message body reading and encoding setting. This improves the robustness and the scope of email content types that the parser can handle."
@wneessen wneessen added hacktoberfest PRs are requested to be eligible to participate in Hacktoberfest hacktoberfest-accepted PR has been accepted to participate in Hacktoberfest WIP Work is in progress labels Oct 13, 2023
@jameshueston
Copy link

Hi @wneessen, thanks for all your hard work on this library!
Any update on this issue, or recommended work-arounds?

I have a scenario where the network may be unavailable at the time of sending, so
I initially used your msg.WriteToFile(filepath) to write EMLs to disk in a retry folder, then
I have another service that checks for any files there and calls the following,
passing in from the caller a new msg object like so msg := mail.NewMsg():

func readMailFromDisk(msg *mail.Msg, filepath string) (err error) {
	conLib.Log.Debugf("Entered readMailFromDisk")
	defer conLib.Log.Debugf("Exited readMailFromDisk")

	// read the mail message from disk into a bytes buffer
	messageBytes, err := os.ReadFile(filepath)
	if err != nil {
		err = fmt.Errorf("failed to read file %s: %v", filepath, err)
		return
	}

	// load the bytes buffer into the mail message object by reference
	length, err := msg.NewReader().Read(messageBytes)
	if err != nil {
		err = fmt.Errorf("failed to read message: %v", err)
		return
	}

	if length == 0 {
		err = fmt.Errorf("message is empty")
		return
	}

	return
}

Today I learned this issue is open :)
Thanks in advance for your help and time.

@wneessen
Copy link
Owner Author

Hi @jameshueston,

unfortunately I haven't had much time to work on this lately. The branch https://github.com/wneessen/go-mail/tree/feature/145_add-eml-parser-to-generate-msg-from-eml-files has what I would call a poorly tested, yet working PoC for simple mails. The big issue currently are mutlipart mails, which are not that easy to "decode". If the mails you are sending are simple mails with maybe just a single attachment, the code might be working for you already, if they are multi-part, it will most likely do nothing yet.

I'll check if I can dedicate some time for this in the next few weeks, to get this finished.

Sorry I don't have a more satisfying response right now.

@jameshueston
Copy link

jameshueston commented Apr 26, 2024

Hi @wneessen, thanks for your fast reply!
I understand not having time lately. Much respect for the good you've already done :)
I dug deep into your code changes today to understand them, then
tested your branch in my use case successfully parsing headers and body with 0 attachments.
Monday, I plan to test with 1 and 2 attachments - very small XLSX and JSON files.
My Content Type is "text/plain".

If your code doesn't work, I thought about trying k3a/parsemail to read message parts into your mail.Msg.
Off the cuff, do you think that could work?

No pressure over the weekend. If all else fails, I could save raw data to disk and rebuild mail for retries, which in my case happens often with mobile devices on intermittent cellular service.

Thank you again, kindly.

@wneessen
Copy link
Owner Author

That sounds good. Crossing fingers that the branch works. Otherwise I'm positive that k3a/parsemail would work as well. From what I can see it extracts all the important parts that would be needed to create a fresh mail.Msg.

@wneessen
Copy link
Owner Author

wneessen commented Jun 19, 2024

It took a lot of time (due to other priorities and time constraints on my end), but https://github.com/wneessen/go-mail/tree/feature/145_add-eml-parser-to-generate-msg-from-eml-files is now at a state that is usable. It's not complete, but most functionality is given (from what I can see and from what I was able to test).

Currently missing:

  • Lots of unit test cases/test coverage
  • Combined multipart/related mails (meaning inline/embeded attachment and normal attachments) are currently not supported yet
  • Some edge cases that I haven't come up with yet, might cause issues

Please feel free to give the branch a try and provide some feedback.

@wneessen
Copy link
Owner Author

Update:

  • multipart/related should now be working as expected
  • More test coverage (almost 90%)
  • Minor API changes have been made

Again, the branch should be ready to test and feedback is appreciated.

@wneessen wneessen linked a pull request Jun 28, 2024 that will close this issue
wneessen added a commit that referenced this issue Jun 28, 2024
…enerate-msg-from-eml-files

This PR introduces an EML parser to go-mail. It allows to read generic EML data from a file, a string or a reader into a go-mail Msg struct. It supports all types of message parts and encodings. It should be able to recognize Mulitpart messages as well as attachments and embeds (inline attachments).

This PR closes #145
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request hacktoberfest PRs are requested to be eligible to participate in Hacktoberfest hacktoberfest-accepted PR has been accepted to participate in Hacktoberfest WIP Work is in progress
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants