Contao-Camp 2024
Ergebnis 1 bis 15 von 15

Thema: 4.9.x best pratice to use swift mailer in own extension/plugin

  1. #1
    Marcell
    Gast

    Standard 4.9.x best pratice to use swift mailer in own extension/plugin

    Hello Community,

    I am wondering about not existing tutorial for beginners for using contao build in symfony componets. After hours research of code snippets and examples I didn't found the right use - argument for using the swiftmailer classes in my plugin.

    PHP-Code:
    namespace Vendor\Pluginname\Classes;

    use 
    Symfony\Component\SwiftMailer;

    class 
    MyClass
    {
           public function 
    sendMyMessage($myDataArr, \Swift_Mailer $mailer)
           {
    ....
    ...
    ..
    .

            }


    Any ideas whats the mistake or any other recommendations for working with swiftmailer in contao?

    Thanks for posting tips here.

  2. #2
    Community-Moderator
    Wandelndes Contao-Lexikon
    Avatar von Spooky
    Registriert seit
    12.04.2012.
    Ort
    Scotland
    Beiträge
    33.882
    Partner-ID
    10107

    Standard

    The example you (probably) used only shows it for Controllers, where you can inject services in each action. As with anything else, just use regular dependency injection:
    PHP-Code:
    // src/Foo/MyFoo.php
    namespace App\Foo;

    class 
    MyFoo
    {
        private 
    $mailer;

        public function 
    __construct(\Swift_Mailer $mailer)
        {
            
    $this->mailer $mailer;
        }


  3. #3
    Marcell
    Gast

    Standard

    Thanks Spooky for the code snippet of regular dependency injection. I thought simelar, but this didn't working here. I expect an syntax error, unexpected '$mailer' (T_VARIABLE), expecting ')'
    If I believe in this message my code part of $message has an mistake/error and it seems $mailer is working/loaded...smells like a typically beginner fail...I have to search for an exception as send($message) throw an error..

    e.g.

    PHP-Code:
    ...
    ..

    $message = (new \Swift_Message('This is a testmail with Swiftmailer and Contao'))
        ->
    setFrom('myEMailAdress')
        ->
    setTo($customerEMail)
        ->
    setBody(
               
    '<h1>HTML email</h1><p>Email with HTML tags if the client supports it.</p>',
               
    'text/html'
               
    )
        ->
    addPart(
               
    'Plain text email',
               
    'text/plain'
               
    );

    $mailer->send($message);

    ..


  4. #4
    Community-Moderator
    Wandelndes Contao-Lexikon
    Avatar von Spooky
    Registriert seit
    12.04.2012.
    Ort
    Scotland
    Beiträge
    33.882
    Partner-ID
    10107

    Standard

    What error do you get now?

  5. #5
    Marcell
    Gast

    Standard

    syntax error, unexpected '$mailer' (T_VARIABLE), expecting ')'

  6. #6
    Community-Moderator
    Wandelndes Contao-Lexikon
    Avatar von Spooky
    Registriert seit
    12.04.2012.
    Ort
    Scotland
    Beiträge
    33.882
    Partner-ID
    10107

    Standard

    You have a Syntax Error in your PHP code. Post your PHP code.

  7. #7
    Marcell
    Gast

    Standard

    Here is the codeline which expected:

    PHP-Code:
    ...
    ..

    // send message via Swiftmailer - call function

    $resultSendingMessage $this->sendMyMessage($myDataArr, \Swift_Mailer $mailer);

    ..
    ...

    public function 
    sendMyMessage($myDataArr, \Swift_Mailer $mailer)
    {

    $message = (new \Swift_Message('This is a testmail with Swiftmailer and Contao'))
        ->
    setFrom('myEMailAdress')
        ->
    setTo($customerEMail)
        ->
    setBody(
               
    '<h1>HTML email</h1><p>Email with HTML tags if the client supports it.</p>',
               
    'text/html'
               
    )
        ->
    addPart(
               
    'Plain text email',
               
    'text/plain'
               
    );

    $mailer->send($message);

    die();
    return 
    "";



  8. #8
    Community-Moderator
    Wandelndes Contao-Lexikon
    Avatar von Spooky
    Registriert seit
    12.04.2012.
    Ort
    Scotland
    Beiträge
    33.882
    Partner-ID
    10107

    Standard

    You need to post your full PHP code.

  9. #9
    Contao-Urgestein Avatar von fiedsch
    Registriert seit
    09.07.2009.
    Ort
    München
    Beiträge
    2.935

    Standard

    In

    PHP-Code:
    $resultSendingMessage $this->sendMyMessage($myDataArr, \Swift_Mailer $mailer); 
    hast Du einen Fehler: der Type-Hint \Swift_Mailer wird bei der Deklaration der Methode angegeben, aber nicht bei deren Aufruf.
    Contao-Community-Treff Bayern: http://www.contao-bayern.de

  10. #10
    Marcell
    Gast

    Standard

    @fiedisch Hatte ich auch schon probiert, aber dann ist das Problem das beim Aufruf der Funktion ein Argument fehlt und wenn ich dann nur $mailer mit übergebe, bleibt der erhoffte Erfolg aus. Tja, so einfach wie ich es mir gedacht habe, ist es dann wohl doch nicht.

    @Spooky The goal here is to show others to use contao built-in component like e.g. swiftmailer. I couldn't see here anything as Symfony describes "...Overall, sending an email is pretty straightforward:..." This didn't match here. Thats the reason why I open here this small thread.

  11. #11
    Community-Moderator
    Wandelndes Contao-Lexikon
    Avatar von Spooky
    Registriert seit
    12.04.2012.
    Ort
    Scotland
    Beiträge
    33.882
    Partner-ID
    10107

    Standard

    Wie sieht dein Code nun aus und was genau ist der Fehler?

  12. #12
    Marcell
    Gast

    Standard

    Mein Code sieht wie folgt aus:

    PHP-Code:

    <?php

    namespace VendorMAS\ContaoApi\Classes;

    use 
    Contao\Database;
    use 
    Contao\Message;

    class 
    MyClass
    {

    // autowiring

    private $mailer;

    public function 
    __construct( \Swift_Mailer $mailer)
    {
          
    $this->mailer $mailer;
    }

    public function 
    checkStatus($arrOrderIDs)
    {

          foreach(
    $arrOrderIDs as $orderID) {
                 if(
    $orderID === 339){
                            
    //send extra info customer wish
                            
    $sendResult $this->sendMessage($myDataArr$orderID);
                            }
          }
    }

    public function 
    sendMessage($myDataArr$orderID)
    {
           
    $db Database::getInstance();
           
    $sql "SELECT * FROM tl_iso_address WHERE pid = ?";
           
    $objResultDB $db->prepare($sql)->execute($orderID)->fetchAllAssoc();

           foreach (
    $objResultDB as $row){
                   
    $customerEMail $row['email'];
           }

           try {
                    
    $message = (new \Swift_Message('This is a testmail with Swiftmailer'))
                          ->
    setForm('meine@email.de')
                          ->
    setTo($customerEMail)
                          ->
    setBody('<h1>HTML email</h1><p>Email with HTML tags if the client supports it.</p>','text/html')
                          ->
    addPart('Plain text email test''text/plain');

                    
    $mailer->send($message);
           } catch (
    Exception $e) {
                    echo 
    $e->getMessage();
           }

           die();
     
           return 
    "";

         }

    }
    Mit diesem code-snippet bekomme ich jetzt die Fehlermeldung wie folgt:

    Internal Server Error

    Too few arguments to function VendorMAS\ContaoApi\Classes::__construct(); 0 passed


    Was für eine "never"-Ending Story (:-(( Ist das jetzt ein Hinweis auf fehlende Services?

    Es muß doch möglich sein an x-beliebiger Stelle eine Funktion zum Versand einer Mail aufzurufen. Unvorstellbar....

  13. #13
    Community-Moderator
    Wandelndes Contao-Lexikon
    Avatar von Spooky
    Registriert seit
    12.04.2012.
    Ort
    Scotland
    Beiträge
    33.882
    Partner-ID
    10107

    Standard

    Du musst dir den Swiftmailer Service in deinen Service injecten lassen. Laut Fehlermeldung ist das noch nicht der Fall. Wie sieht die Service Definition aus?

  14. #14
    Marcell
    Gast

    Standard

    @spooky

    Meine services.yml sieht wie folgt aus:

    Code:
    #config/services.yml
    services:
           _defaults:
                   autowire: true
                   autoconfigure: true

  15. #15
    Community-Moderator
    Wandelndes Contao-Lexikon
    Avatar von Spooky
    Registriert seit
    12.04.2012.
    Ort
    Scotland
    Beiträge
    33.882
    Partner-ID
    10107

    Standard

    Da sind aber nur defaults für Services definiert, keine eigentlichen Services. Da du autowiring benutzt, willst du vielleicht auch Services automatisch registrieren mit "resource": https://symfony.com/doc/4.4/service_...-with-resource

    Siehe auch https://docs.contao.org/dev/getting-...es-and-actions

Aktive Benutzer

Aktive Benutzer

Aktive Benutzer in diesem Thema: 1 (Registrierte Benutzer: 0, Gäste: 1)

Lesezeichen

Lesezeichen

Berechtigungen

  • Neue Themen erstellen: Nein
  • Themen beantworten: Nein
  • Anhänge hochladen: Nein
  • Beiträge bearbeiten: Nein
  •