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

Support headers for native_mailer #272

Closed
nalysius opened this issue Jun 25, 2018 · 4 comments
Closed

Support headers for native_mailer #272

nalysius opened this issue Jun 25, 2018 · 4 comments
Labels

Comments

@nalysius
Copy link
Contributor

Q A
Bug report? no
Feature request? yes
BC Break report? no
RFC? no
Symfony version 4.1
Monolog Bundle version 3.3

I am currently using Monolog to receive mails when I log errors. With the swift_mailer handler I don't receive any mails using sendmail transport. I am used to use NativeMailerHandler with Monolog so I would like to use it as handler. With native_handler I receive emails, but they are always in plain text.

The swift_mailer handler does support a content_type key that allows to get the mail as text/html.

The NativeMailerHandler class has a headers attribute that could contains a content-type. For now only swift_mailer is able to send email as html, native_mailer doesn't use any 'headers' or 'content_type' keys.

The following example would be fully compatible with swift_mailer.

monolog:
    handlers:
        main:
            type:       native_mailer
            from_email: 'myemail@example.com'
            to_email:   ['user@example.com']
            subject:    'An error occured'
            level:      error
            formatter:  monolog.formatter.html
            content-type: text/html

But this one would be more flexible with the usage of headers.

monolog:
    handlers:
        main:
            type:       native_mailer
            from_email: 'myemail@example.com'
            to_email:   ['user@example.com']
            subject:    'An error occured'
            level:      error
            formatter:  monolog.formatter.html
            headers:
                content-type: text/html
@lyrixx
Copy link
Member

lyrixx commented May 18, 2019

Hello @anthonybocci

Would you mind to submit a PR?

Thanks

@lyrixx lyrixx closed this as completed May 18, 2019
@lyrixx lyrixx reopened this May 18, 2019
nalysius added a commit to nalysius/monolog-bundle that referenced this issue May 18, 2019
The native mailer is able to have additional headers, but it
wasn't possible to give it any headers in the configuration.
Swift mailer may have a content_type key but not native mailer.

A new "headers" key is now allowed in the handler configuration, so a
list of headers may be given to the handler. Only native mailer supports
it.

Related to symfony#272
@nalysius
Copy link
Contributor Author

Hi @lyrixx,

I just submitted a PR.
It works, but since it's my first contribution please let me know if something is wrong or missing.

lyrixx pushed a commit to nalysius/monolog-bundle that referenced this issue Jun 11, 2019
The native mailer is able to have additional headers, but it
wasn't possible to give it any headers in the configuration.
Swift mailer may have a content_type key but not native mailer.

A new "headers" key is now allowed in the handler configuration, so a
list of headers may be given to the handler. Only native mailer supports
it.

Related to symfony#272
nalysius pushed a commit to nalysius/monolog-bundle that referenced this issue Jun 11, 2019
The headers type was defined as a choice, but it's not. It's a sequence
of strings.

See: symfony#272
nalysius pushed a commit to nalysius/monolog-bundle that referenced this issue Jun 11, 2019
The field "headers" was defined as an attribute but it's a complex type
so it's an element instead.

See: symfony#272
lyrixx added a commit that referenced this issue Jun 20, 2019
…lyrixx)

This PR was merged into the 3.x-dev branch.

Discussion
----------

Add support for headers in native mailer

The native mailer is able to have additional headers, but it
wasn't possible to give it any headers in the configuration.
Swift mailer may have a content_type key but not native mailer.

A new "headers" key is now allowed in the handler configuration, so a
list of headers may be given to the handler. Only native mailer supports
it.

Related to #272

Commits
-------

19eaa0f Fix 'Add support for headers in native mailer'
7ecbc9e Add support for headers in native mailer
@lyrixx lyrixx closed this as completed Jun 20, 2019
@ilya-realforce
Copy link

ilya-realforce commented Mar 12, 2020

@nalysius sorry for necro-posting, but were you able to override the Content-type header? I couldn't do that, because with config's headers part you only can add new headers (since NativeMailerHandler.php:96 is only appending new items to the headers array), and on the NativeMailerHandler.php:122 there is no checking of existing Content-type header. It just adds text/plain unconditionally. As a result, I'm getting these headers in the mail:

Content-type: text/html; charset: utf8\r\n
Content-type: text/plain; charset=utf-8

I think the possible solution is to add content_type config key support somewhere around monolog-bundle/DependencyInjection/MonologExtension.php:543 and call setContentType of NativeMailerHandler with it.

@lyrixx can you also look at it, please?

@nalysius
Copy link
Contributor Author

@ilya-realforce I am currently using the Content-Type header in several applications. In the mail's source I saw the same, the Content-Type header is present twice in the mail. It's not ideal.

Monolog handles the content-type in a special field, so even if we give it a header it will then use the content type and add it as a header. I guess it would be better in monolog-bundle to add a condition and check whether the header we are setting is the content type and if so call the method setContentType instead.

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

No branches or pull requests

4 participants