SMTP sending two profile

I want to send emails using the mail () function - localhost, and also using SMTP.

I set up a profile

'EmailTransport' => [
    'default' => [
        'host' => 'localhost',
        'port' => 25,
        'username' => null,
        'password' => null,
        'client' => null,
        'url' => env('EMAIL_TRANSPORT_DEFAULT_URL', null),
    ],
    'smtp' => [
        'className' => 'Smtp',
        'host' => 'smtp-server',
        'port' => 465,
        'username' => '***@***.com',
        'password' => 'secret',
        'tls' => true,
        'client' => null,
        'url' => env('EMAIL_TRANSPORT_DEFAULT_URL', null),
    ],
],

But I don’t know how to set the SMTP profile to activate.

I have custom send message an email as follows:

use Cake\Mailer\Message;
use Cake\Mailer\Transport\MailTransport;

$message = new Message;
$message
    ->setCharset('utf-8')
    ->setHeaderCharset('utf-8')
    ->setSubject('subject')
    ->setBodyHtml('body');

$transport = new MailTransport;
$transport->send($message);

Thank you

Instead of
$transport = new MailTransport();
try
$transport = TransportFactory::get('smtp');

1 Like

Thank you, maybe works, but

SMTP timeout.

is there any way to debug sending?

Its works, i have wrong tls port…

Please how set additionalParameters in mailer?

What “additional parameters” do you want to set?

'-femail@email.com'

$message->setFrom('email@email.com') ?

The documentation also mentions “configuration profiles”, which it seems might let you set this for all emails instead of one at a time, but it’s not clear to me exactly how this works.

1 Like

I add and its works… If I did not enter this parameter, then the email was not delivered to the same mail as the sender.

'EmailTransport' => [
    'default' => [
        'host' => 'localhost',
        'port' => 25,
        'username' => null,
        'password' => null,
        'client' => null,
        'url' => env('EMAIL_TRANSPORT_DEFAULT_URL', null),
        'additionalParameters' => '-femail@email.com'
    ],
],

It’s great if that works for you, but it’s a limited solution. It’s not necessarily portable to other server setups, and it’s not unit-test friendly. If you don’t care about those things, then it’s fine.

I did not find any other solution for the e-mail to be delivered. Only use SMTP.

SMTP, sure, but the “-f” thing should not be required. Did you try $message->setFrom('email@email.com')?

Yes, I have this set, but the email will not arrive without the f parameter.

That’s just weird. Both of these things should set who the email is from, one or the other should be sufficient. email@email.com is obviously not the actual address you’re using. Is there any chance that Cake is rejecting the actual email address as having an invalid format?

This is not a real address. I also forgot to write that the email will not arrive, especially to the email within the hosting email@email.com> email@email.com, but for example email@email.com> something@gmail.com will arrive. I tried another domain and it didn’t arrive either … so it’s not 100% sure that the email will get where I need it. CAKE does not return any error. But with the -f parameter, it now delivers everywhere.

Wait, what are the ">"s in here? Are you using a mailto link for the email address instead of just the email address? Or passing in a single string that contains both the email address and the name?

No, > is TO
eg: from: email@email.com to: email@email.com = email@email.com > email@email.com

I used only email.

Well then, I’d start looking into the specifics of the email that gets generated (e.g. with a debugger or the like), to see if Cake is correctly setting the From details on the outgoing email without the -f option. If it isn’t debug why not. If it is, maybe the problem is something in your localhost SMTP server setup; you’re connecting to it without any username and password, it seems, which many servers will treat differently.

1 Like

Thank you, I’ll try to look in detail.