Simple examples to show the possibilities?

Created on 12 January 2022, almost 3 years ago
Updated 10 May 2023, over 1 year ago

Problem/Motivation

As former user of Swift Mailer, I'm stumbling into Symphony Mailer.

When using Swift Mailer I had a hard time setting up very basic things like a HTML theme on my mails, sending attachments, ect,... Symphony Mailer is yet another learning curve for me and it would be great to add some simple examples to the documentation to get started and show the features that ship with this module.

I'm thinking of:

  • Example: how to theme system e-mails
  • Example: Send attachment with e-mails

I think this are quite common features a lot of users are looking for.

✨ Feature request
Status

Active

Version

1.0

Component

Documentation

Created by

πŸ‡§πŸ‡ͺBelgium apoc1

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • πŸ‡©πŸ‡ͺGermany zevier

    If this module shall be used widely, it needs URGENT more documentation.

    The documentation https://www.drupal.org/docs/contributed-modules/symfony-mailer-0/feature... β†’ claims what could be possible, but don’t show and don’t explain.

    PLEASE provide some simple examples for basic functionality on level3.
    If it is easy as claimed, please show.

    • - Give a minimal hello_world example what is needed to send an email (on level3 inside of a new module).
    • - Give another example with attachment, using of templates etc.
  • πŸ‡³πŸ‡ΏNew Zealand john pitcairn

    More on question 6 from comment #7:

    For modules (not themes) previously using hook_preprocess_swiftmailer() to add css libraries for inlining, what is the equivalent procedure to add a module library and have its css inlined using symfony_mailer?

  • πŸ‡ͺπŸ‡¨Ecuador jwilson3

    Unofficial answer, and untested but a few ideas to try:

    1. Specify which theme you want to use in your policies, eg "mytheme".
    2. add the "email" library to mymodule.libraries.yml following the instructions in getting started β†’
    3. add an empty "email" library to mytheme.libraries.yml with a dependency on mymodule/email

    If that doesnt work (and I'm guessing might not)...

    1. remove the module library dependency from the theme library, add an empty "css/mail.css" in the theme library.
    2. alter the theme library using https://api.drupal.org/hook_library_info_alter in mymodule.module to swap out the path to the css file to point to the one from the module folder.
  • πŸ‡³πŸ‡ΏNew Zealand john pitcairn

    Argh, I hope not. The theme and module are completely independent of each other. It should be possible for a module to send mail and inline its own css without relying on any theme.

  • πŸ‡©πŸ‡ͺGermany dercheffe Sersheim, Germany

    What I need is, how to natively create an email with this module directly. Wi

    email_entity = $email_factory->newTypedEmail('MYMODULE', 'my_awsome_email_subtype');
    $email_entity->setParam('user_display_name', $user_recipient_name);
    $email_entity->setSender($email_from);
    $email_entity->setSubject($email_subject);
    $email_entity->setTextBody($email_body_plain);
    $email_entity->setTo($user_recipient_email);
    $operation_status = $email_entity->send();
    

    Unfortunately it doesn't work.
    I get "Call to a member function newTypedEmail() on null"πŸ™ˆ

    Or how can I do the "native implementation" in the docs it's called "Level 3" correctly?

  • πŸ‡¬πŸ‡§United Kingdom adamps

    what is the equivalent procedure to add a module library and have its css inlined using symfony_mailer?

    This seems to be a completely general Drupal question - how to alter a theme library in a module. Try hook_library_info_alter()

  • πŸ‡¬πŸ‡§United Kingdom adamps

    how to natively create an email with this module directly

    It's a good question- I wrote a page for it: https://www.drupal.org/docs/contributed-modules/symfony-mailer-0/develop... β†’

  • πŸ‡ΊπŸ‡ΈUnited States sah62 US

    Am I correct in assuming that some of these instructions are intended for module developers, and some are intended for site administrators? For example, "migrate to the new template" appears to be a developer instruction. There's no GUI for it that I can find so there's nothing I can do as a site administrator, but I understand how a module developer would need to switch if their module is sending email.

    I just did some testing with my existing webform configuration in which email with attachments is sent. It still works just fine, without changing anything, after configuring Symfony Mailer (with import of my Swift Mailer settings) and removing the legacy applications.

    One last question: at what point can I (or should I?) disable the "Emulate swiftmailer" option as a site administrator?

    Thanks again!

  • πŸ‡¬πŸ‡§United Kingdom adamps

    Am I correct in assuming that some of these instructions are intended for module developers, and some are intended for site administrators?

    Yes unfortunately it's a bit muddled up, πŸ“Œ Improve the documentation Active . I'm a developer not a writerπŸ˜ƒ.

    One last question: at what point can I (or should I?) disable the "Emulate swiftmailer" option as a site administrator?

    If you only use the GUI, then I guess you never wrote templates or CSS files. SO it makes no difference to you, you can do it immediately.

  • πŸ‡ΊπŸ‡ΈUnited States chipcleary

    Thanks @AdamPS for providing Symfony Mailer!

    In case it is useful for others, as I played with using Symfony Mailer to create my first custom email using it, I developed a "Hello World" email example module. See here: https://github.com/chipcleary/symfony_mailer_example/blob/main/README.md.

    This provides a basic example of how to programmatically define and send custom mail, including:

    • Defining a custom EmailBuilder
    • Providing and using twig variables
    • Providing a mailer policy
    • Providing CSS

    All of this is already covered in the existing documentation β†’ . But it took me some hunting-and-pecking to identify which parts I needed and how to apply it.

    Caveats:

    • I'm a novice developer and am unfamiliar with Symfony Mailer. I wrote this as I worked through how to create a first custom email. It's only meant to provide a minimal "starterkit" example. It is emphatically *not* authoritative.
    • It does not address either of the two examples used in the issue statement, which are a step more advanced (theming system emails, sending attachments).
  • πŸ‡¬πŸ‡§United Kingdom adamps

    @chipcleary Nice example thanks

  • πŸ‡¬πŸ‡§United Kingdom adamps

    @chipcleary Your example is similar to TestEmailBuilder from this module, but betterπŸ˜ƒ. I would like to bring the extra parts into here.

    1. libraries.yml
    2. CSS file
    3. A variable - e.g. the date (a bit more 'serious' than a lucky numberπŸ˜ƒ)

    I would welcome a patch.

  • πŸ‡ͺπŸ‡ΈSpain carlos espino Guatiza (Lanzarote)

    @chipcleary Thanks a lot

  • πŸ‡ΊπŸ‡ΈUnited States chipcleary

    @AdamPS, glad you like it and I'd be happy to provide a patch! I should be able to provide it by the end of the week.

  • πŸ‡ΊπŸ‡ΈUnited States chipcleary

    @AdamPS, here's a stab at the patch. Happy to make modifications.

    I extended the email to include:

    • A header and footer, which use custom CSS for formatting.
    • A "day" variable which captures day of the week.

    To implement this, I modified:

    • symfony_mailer.libraries.yml
    • TestEmailBuilder.php
    • symfony_mailer.mailer_policy.symfony_mailer.test.yml

    And I added test.email.css.

  • Open in Jenkins β†’ Open on Drupal.org β†’
    Core: 9.5.x + Environment: PHP 7.4 & MySQL 5.7
    last update over 1 year ago
    4 pass, 2 fail
  • Status changed to Needs review over 1 year ago
  • πŸ‡¬πŸ‡§United Kingdom adamps

    Great many thanks, I'll take a look soon

  • Open in Jenkins β†’ Open on Drupal.org β†’
    Core: 9.5.x + Environment: PHP 7.4 & MySQL 5.7
    last update over 1 year ago
    4 pass, 2 fail
  • Open in Jenkins β†’ Open on Drupal.org β†’
    Core: 9.5.x + Environment: PHP 7.4 & MySQL 5.7
    last update over 1 year ago
    5 pass
  • πŸ‡¬πŸ‡§United Kingdom adamps
  • Status changed to Needs work over 1 year ago
  • πŸ‡¬πŸ‡§United Kingdom adamps

    It's good thanks, please can you just tidy up some details:

    1) TestEmailBuilder
    I like that you added clear explanations to help anyone using this file as an example to copy. Please can you avoid splitting a line of code with comments, and instead put the comments first as separate bullet points, then $email->addLibrary('symfony_mailer/test'). Also check the coding standards, keep to max length 80 characters.

    2) symfony_mailer.mailer_policy.symfony_mailer.test.yml
    I have made the yml files easy for a human to read, to help people using it as an example. Please keep the syntax value: |- then use a separate line for each paragraph and, for consistency, keep with the single quotes around the subject.

    3) test.email.css
    The initial comment should contain a @file annotation and remove the 2 lines with only a star.

    4) Please add an extra line into TestEmailTest to check that one of the styles is present. Use a comment to say the style comes from test.email.css.

  • Status changed to Needs review over 1 year ago
  • Open in Jenkins β†’ Open on Drupal.org β†’
    Core: 9.5.x + Environment: PHP 7.4 & MySQL 5.7
    last update over 1 year ago
    4 pass, 1 fail
  • πŸ‡ΊπŸ‡ΈUnited States chipcleary

    Helpful feedback, thanks. Here's an updated patch. Would you be able to check in particular whether I've addressed #4 appropriately? I'm least confident about that one.

  • Open in Jenkins β†’ Open on Drupal.org β†’
    Core: 9.5.x + Environment: PHP 7.4 & MySQL 5.7
    last update over 1 year ago
    5 pass
  • πŸ‡¬πŸ‡§United Kingdom adamps

    Great it looks good. I changed it slightly, hopefully it fixes the tests.

  • Status changed to Active over 1 year ago
  • πŸ‡¬πŸ‡§United Kingdom adamps
  • Status changed to Needs work over 1 year ago
  • πŸ‡ΊπŸ‡ΈUnited States chipcleary

    Super, thanks @AdamPS. Let me know if there's anything else you'd like.

  • Status changed to Active over 1 year ago
  • πŸ‡¬πŸ‡§United Kingdom adamps

    It's good thanks. I've left the issue open because there's still more that could be done.

Production build 0.71.5 2024