nette / mail

A handy library for creating and sending emails in PHP
https://doc.nette.org/mailing
Other
463 stars 70 forks source link

Unable to send email using SendmailMailer in v4.0.2 #96

Open zabous opened 9 months ago

zabous commented 9 months ago

Version: 4.0.2

Bug Description

I have very simple code to send email using SendmailMailer. Everything is working in nette/mail v3.1.11. When I upgrade to nette/mail v4.0.2 there is error:

Nette\Mail\SendException - Unable to send email

Steps To Reproduce

Try this code with nette/mail v3.1.11 (working) and with v4.0.2 (not working)

$mail = new Nette\Mail\Message;
$mail->setFrom('Franta <franta@example.com>')
    ->addTo('petr@example.com')
    ->setSubject('Potvrzení objednávky')
    ->setBody("Dobrý den,\nvaše objednávka byla přijata.");
$mailer = new Nette\Mail\SendmailMailer;
$mailer->send($mail);

It seems to me, that the problem is on line SendmailMailer.php on line 52 - after removal it work ok. $cmd .= ' -f' . key($from);

Expected Behavior

Code is working with both versions (v3.1.11 and v4.x.x)

dg commented 9 months ago

Related to #78

milo commented 7 months ago

Setting an envelope address (-f $from) is correct from more points of view.

@zabous Your problem can be on more levels. Default PHP sendmail command is sendmail_path = sendmail -t -i. Maybe your hosting modified it, should be seen in php info.

The sendmail command is quite UNIX standard and many non-sendmail MTA emulates it. Maybe it is not full compatible.

Or sending MTA does not allow $from to be envelope address. Such error could be found in mail log, not sure you can access it.

zabous commented 7 months ago

I see my phpinfo() that "sendmail_path" on my hosting is set to "/usr/bin/msmtp -C /etc/msmtprc -t --read-envelope-from" It works fine without ' -f' param. @milo is there some problem in this setting?

I cannot access mail log - should I try to ask my provider for my mail logs?

milo commented 7 months ago

From msmtp man page, parameter --read-envelope-from ensures same behaviour. I cannot test it but probably additional -f is evaluated as an error.

This is hard to solve in some automatic way. Anyway, I think passing -f is correct in most cases. Possible solutions in my mind:

Or you can try to use SmtpMailer, there will be probably available localhost:25 SMTP connection.

zabous commented 7 months ago

Parameter -f is correct in most cases? I'm afraid that's not enough for general library. In my opinion, it is better to leave the -f parameter to the developer using the library.

Thanks for the advice with SendmailMailer::setEnvelopeFrom(bool). That can be helpful, but I cannot find this function in SendmailMailer.

Is there another way to turn off adding the -f parameter?

milo commented 7 months ago

Parameter -f is correct in most cases? I'm afraid that's not enough for general library. In my opinion, it is better to leave the -f parameter to the developer using the library.

Yes. It is correct to set envelope address to From header.

Thanks for the advice with SendmailMailer::setEnvelopeFrom(bool). That can be helpful, but I cannot find this function in SendmailMailer.

It is only an idea how to solve it by pull request.

Is there another way to turn off adding the -f parameter?

No. Send pull request with explanation.