bertvandepoel / tabby

A friendly tool to manage debt
GNU Affero General Public License v3.0
71 stars 11 forks source link

How to adapt php.ini for non-standard sendmail emailing? #15

Closed wychwitch closed 2 years ago

wychwitch commented 2 years ago

I was trying out tabby today and saw this text on the installation screen

Please refer to the PHP documentation and adapt your php.ini or .user.ini if standard sendmail emailing isn't available.

With a link to php documentation here.

My question is, are there any examples of ways to adapt php.ini for non standard email set ups? I currently don't have a working sendmail on my server, and usually use sendgrid for other applications that have the built in support for it. Is using sendgrid here even possible? Or is sendmail the only way to use this application?

Many thanks in advance.

bertvandepoel commented 2 years ago

I haven't used Sendgrid before, so I'm not completely aware what options they offer. If they offer SMTP login using a domain or IP and login details, that can easily be configured straight in php.ini. If it only supports some kind of JSON or XML API, then I would guess applications would specifically need to implement support for Sendgrid. Could you maybe elaborate on that a bit? I can then see what solutions are possible.

wychwitch commented 2 years ago

Thanks for getting back to me so soon! I wasn't expecting it hahaha

Thank you for prompting me to research this deeper! After looking at their documentation it looks as though you can sign in using SMTP login as outlined here. There's also an alternate PHP example here,.

So, if I understand correctly, I can just supply php.ini with sendgrid's details as per their documentation and that should work? In that case that's much easier than I was expecting hahaha.

I'll also post the php example if that is relevant here if you're interested!

sendgrid's php example code ```php setFrom("test@example.com", "Example User"); $email->setSubject("Sending with SendGrid is Fun"); $email->addTo("test@example.com", "Example User"); $email->addContent("text/plain", "and easy to do anywhere, even with PHP"); $email->addContent( "text/html", "and easy to do anywhere, even with PHP" ); $sendgrid = new \SendGrid(getenv('SENDGRID_API_KEY')); try { $response = $sendgrid->send($email); print $response->statusCode() . "\n"; print_r($response->headers()); print $response->body() . "\n"; } catch (Exception $e) { echo 'Caught exception: '. $e->getMessage() ."\n"; } ```
bertvandepoel commented 2 years ago

Seems like you can indeed chose between using SMTP credentials or implementing their API into the application. In case of Tabby, I didn't really want to figure out all the different APIs for the more popular email services (Google, Microsoft, Sendgrid, Mailgun, Mailjet, Twilio, ...), especially since I don't want to encourage the use of external and/or closed source solutions but try to adhere to the selfhosted concept of https://github.com/awesome-selfhosted/awesome-selfhosted/ .

Luckily, it seems Sindgrid offers SMTP credentials you can configure in php.ini for all your PHP applications, which is much cleaner and simpler, and very similar to the setup I use (but I use a local email server).

Hopefully you can get things working that way. If you get it set up perfectly, please let me know (and feel free to close the issue at that point). I might clarify further in a next release that you can configure SMTP credentials easily through php.ini, since I don't mention that in the message you quoted.

wychwitch commented 2 years ago

Great! I'll try to get it set up soon and let you know if it works!

Also if you don't mind, could you share what kind of email server you use? I may try my hand at running my own eventually and only just started looking into different software and docker images.

wychwitch commented 2 years ago

Oh I think I have it almost finished, but I ran into the following error when actually trying to send the email out:

[20-Sep-2021 06:25:11] WARNING: [pool www] child 72 said into stderr: "sh: 1: /usr/sbin/sendmail: not found"

I realized I hadn't installed sendmail, so I ran apt-get to install it, but it's still running into the same error. Is there some value in pbp.ini that I'm missing that needs to be set?

wychwitch commented 2 years ago

Okay some updates

My issues with sendmail in the above comments was because I was running tabby in docker as per the instructions found here, since I prefer to run things in docker where possible. However sendmail wasn't included in this configuration, so I had to connect to the docker container and running apt-get install sendmail which has almost got it working.

Now I'm running into this error: [SYSLOG] sendmail[1185]: 18KJbEp4001185: to=user@foo.bar, ctladdr=application (1000/1000), delay=00:00:00, xdelay=00:00:00, mailer=relay, pri=30465, relay=[127.0.0.1] [127.0.0.1], dsn=4.0.0, stat=Deferred: Connection refused by [127.0.0.1]

I modified the docker launch command like so: sudo docker run --link some-weird-mariadb -v /tmp/tabby2/:/app -p 8070:80 -p 587:587 webdevops/php-nginx

And no dice. As this is im sure far beyond the intended setup, I will abandon using docker and try with a standard nginx host configuration and update once i get something working.

wychwitch commented 2 years ago

OKAY FINALLY GOT IT WORKING WITH SENDGRID

I followed these instructions using the sendgrid credentials. Just in case the guide ever falls or someone needs exact instructions here's how I did it

using sendgrid with sendmail Requirements: Sendgrid and a sendmail account with a verified domain name. First install sendmail if you haven't already. I used `sudo apt-get install sendmail` on debian Change the hostname if you'd like using /etc/hosts but it's not needed. Run the following commands: ```bash sudo mkdir /etc/mail/authinfo sudo chmod -R 700 /etc/mail/authinfo cd /etc/mail/authinfo sudo nano smtp-auth ``` now paste the following, replacing `actual-api-key` with the api key you got from sendgrid ```txt AuthInfo: "U:root" "I:apikey" "P:actual-api-key" ``` Next run this command: ```bash sudo makemap hash smtp-auth < smtp-auth ``` Now to edit the sendmail.mc ```bash cd /etc/mail sudo nano sendmail.mc ``` paste this right below the `MAILER_DEFINITIONS` ```mc define(`SMART_HOST',`[smtp-host]')dnl define(`RELAY_MAILER_ARGS', `TCP $h 587')dnl define(`ESMTP_MAILER_ARGS', `TCP $h 587')dnl define(`confAUTH_OPTIONS', `A p')dnl TRUST_AUTH_MECH(`EXTERNAL DIGEST-MD5 CRAM-MD5 LOGIN PLAIN')dnl define(`confAUTH_MECHANISMS', `EXTERNAL GSSAPI DIGEST-MD5 CRAM-MD5 LOGIN PLAIN')dnl FEATURE(`authinfo',`hash -o /etc/mail/authinfo/smtp-auth.db')dnl ``` Then run `make` and restart sendmail with `sudo /etc/init.d/sendmail restart` Assuming you're using nginx, edit the following file, replacing $VERSION with yoiur actual php version `sudo nano /etc/php/$VERSION/fpm/php.ini` Restart nginx and php `sudo service php$VERSION-fpm restart` And that should be it! Feel free to @ me if anyone has any questions.
bertvandepoel commented 2 years ago

Seems you have been busy! Sorry for the late reply, I had a crazy day at work and some stuff I still had to do around the house.

Hmmm, if I understand things correctly, you are now using sendmail with the sendgrid SMTP instead of using the sendgrid SMTP directly in PHP. That's also possible, but a lot more complicated I think. But I'm glad you got it working! :D

To answer your earlier question: I run postfix with a MySQL backend to define mailboxes and aliases, I use dovecot as the LDA (so I can use sieve and managesieve) and for IMAP, and use amavis with spamassassin for anti-spam. It's not a complicated setup, but it can be challenging to start to understand the components.

wychwitch commented 2 years ago

No problem!! and yeah, I switched to SMTP + sendmail because apparently php on linux systems doesn't support SMTP?? at least without installing and configuring something else. This way ended up being easier.

I saw postfix come up repeatedly on my searches! I'll def look into it sometime, maybe even try a mail server docker out now that I know how to get sendgrid working with SMTP credentials. It sounds challenging but thanks for telling me anyways! It'll be good to have something to launch off of!