Skip to content
This repository has been archived by the owner on Jul 16, 2021. It is now read-only.

After update from laravel 5.5 to laravel 5.8 swiftmailers setBody method doesnt make any changes to body #1646

Open
xUJYx opened this issue May 16, 2019 · 2 comments

Comments

@xUJYx
Copy link

xUJYx commented May 16, 2019

Made a copy of this one issue.
PR #22995 to fix #22982 cause this problem...

Any ideas about ways to resolve such problem and save standart swiftmailers methods work?
Maybe it can be made with some flags which will say to laravel core when to make call_user_func before or after addContent call ?
==========================================================================

  • Laravel Version: 5.8.17
  • PHP Version: 7.3.5

Description:

After update laravel version from 5.5 to 5.8 when using swiftmailer raw method, mail sending starts to give error quoted_printable_encode() expects parameter 1 to be string, array given
swiftmail_error
If you provide some raw string data as first parameter - mail will be sent, BUT callback function will not make any changes to body with setBody method...
error_doesnt_overwrite

Steps To Reproduce:

Try to send e-mail with raw method using swiftmailer:
error_reproduce
`Mail::raw([], function ($message)

{
$message->to('some@email.test');
$message->from('from@my.mail', 'MyName');
$message->subject('Test email send');
$message->setBody('Some cute html body to send', 'text/html');
});`

How To Solve Problem:

In new versions of laravel 5.6+ had been moved call of call_user_func in /vendor/laravel/framework/src/Illuminate/Mail/Mailer.php in send($view, array $data = [], $callback = null) method...
swiftmailer_error_point
If you move call_user_func back as in previous laravel versions - problem will be solved and all will start work well.
swiftmail_error_solved

Thats it...

@Sladewill
Copy link

Pretty sure we changed to Mail::html instead of Mail::raw because its the method to use now and was documented somewhere.

@iget-master
Copy link

iget-master commented Jun 26, 2019

That PR #22995 also breaks things here:

In our case, we use the $this->withSwiftMessage(...) to modify the message body and add some attachments to the email. (we replace some cid references with urls and embed missing cid references files).

Our code is like

$this->withSwiftMessage(function (Swift_Message $message) {
    $body = $message->getBody();

    // Modify body, embed some files (searching for images with `src="cid:*` and replace with another cid and embed the file)

   $message->setBody($body);
}

This used to work on 5.5, and now we are migrating to latest version, but after 5.6 the callback order rearrange causes this issue.

I understand the reason of the rearrange, but I think that $this->withSwiftMessage() should be able to place a callback BEFORE and AFTER. Maybe, it can receive an optional parameter for placing it before.

Can @taylorotwell give a look here? I think this is an important issue.

Update: Workaround for interested people:

As workaround, I solved this issue with pointers, since my body was a property of my Mailable class:

public function build()
{
    $body = &$this->body;
    
    $this->withSwiftMessage(function (Swift_Message $message) use (&$body) {
        // now I can modify the $body here, attach my files without problem
    }

    return $this->view('my.view.template')->with(compact('body'));
}

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

No branches or pull requests

3 participants