From 2d0bfb5b703184dc512cd7a7a80d8238bd7d99dc Mon Sep 17 00:00:00 2001 From: matbgn Date: Fri, 29 Apr 2022 13:13:48 +0200 Subject: [PATCH] Basic implementation of Dockerfile + fix mail used if userId differ from original sender --- .dockerignore | 13 +++++++++ Dockerfile | 48 ++++++++++++++++++++++++++++++++ README.md | 32 ++++++++++++++++++++- composer.lock | 2 +- config.example.php | 1 + docker-utils/configure_smtp.sh | 14 ++++++++++ docker-utils/install_composer.sh | 17 +++++++++++ docker-utils/test_mail.php | 13 +++++++++ index.php | 6 ++-- 9 files changed, 141 insertions(+), 5 deletions(-) create mode 100644 .dockerignore create mode 100644 Dockerfile create mode 100755 docker-utils/configure_smtp.sh create mode 100755 docker-utils/install_composer.sh create mode 100644 docker-utils/test_mail.php diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..b3f5fb1 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,13 @@ +# Ignore everything +** + +# Allow files and directories +!lib/ +!docker-utils/ +!composer* +!config* +!index.php + +# Ignore unnecessary files inside allowed directories +# This should go after the allowed directories +docker-utils/test_mail.php diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..3369e6b --- /dev/null +++ b/Dockerfile @@ -0,0 +1,48 @@ +FROM php:alpine3.15 AS builder + +# Install all dependencies required for API calls and mail handling +RUN apk add --no-cache git php-curl php-mbstring php-imap php-xml ssmtp gawk + +# Install PHP ext-imap +RUN set -eux; \ + persistentDeps=" \ + c-client \ + "; \ + buildDeps=" \ + imap-dev \ + krb5-dev \ + openssl-dev \ + "; \ + apk add --no-cache --virtual .imap-persistent-deps ${persistentDeps}; \ + apk add --no-cache --virtual .imap-build-deps ${buildDeps}; \ + \ + docker-php-ext-configure imap \ + --with-imap-ssl \ + --with-kerberos \ + ; \ + docker-php-ext-install -j$(nproc) imap; \ + \ + apk del --no-cache --no-network .imap-build-deps + +# Create a non-root user to own the files and run our server +RUN adduser -D -g "Mail2deck" deckbot +WORKDIR /home/deckbot/mail2deck + +# Copy scripts +# Use the .dockerignore file to control what ends up inside the image! +COPY . . + +# Install dependencies +RUN docker-utils/install_composer.sh && \ + ./composer.phar update && \ + rm docker-utils/install_composer.sh composer.phar + +# Setup SMTP Server +RUN docker-utils/configure_smtp.sh && \ + rm docker-utils/configure_smtp.sh + +# Use our non-root user +USER deckbot + +# Run script once +CMD ["php", "index.php"] \ No newline at end of file diff --git a/README.md b/README.md index 10c76af..49a632c 100644 --- a/README.md +++ b/README.md @@ -50,6 +50,7 @@ sudo postconf -e "recipient_delimiter = +" This could be any hosted email service. The only requirement is that you can connect to it via the IMAP protocol. *Please note this option may not be as flexible as a self-hosted server. For example your email service may not support the "+"delimiter for directing messages to a specific board.* ## Download and install +### Bare-metal installation If using a self-hosted Postfix server, clone this repository into the home directory of the *incoming* user. If not self-hosting, you may need to create a new user on your system and adjust the commands in future steps to match that username.
``` su - incoming @@ -62,12 +63,41 @@ cp config.example.php config.php sudo vim config.php ``` *You can refer to https://www.php.net/manual/en/function.imap-open.php for setting the value of MAIL_SERVER_FLAGS* -## Add a cronjob which will run mail2deck. +#### Add a cronjob which will run mail2deck. ``` sudo crontab -u incoming -e ``` Add the following line in the opened file: */5 * * * * /usr/bin/php /home/incoming/mail2deck/index.php >/dev/null 2>&1 +### Docker installation +Clone and edit the config.example.php you find in this repository and move it as config.php +``` +git clone https://github.com/newroco/mail2deck.git mail2deck +cd mail2deck +cp config.example.php config.php +nano config.php +``` + +Build your image locally +``` +docker build -t mail2deck:latest . +``` + +Run the docker image mapping the config.json as volume +``` +docker run -d --name mail2deck mail2deck:latest +``` + +Edit your crontab +``` +crontab -e +``` + +And add this line +``` +*/5 * * * * /usr/bin/docker start mail2deck +``` + ## Finish Now __mail2deck__ will add new cards every five minutes if new emails are received. diff --git a/composer.lock b/composer.lock index c693662..6840597 100644 --- a/composer.lock +++ b/composer.lock @@ -104,5 +104,5 @@ "prefer-lowest": false, "platform": [], "platform-dev": [], - "plugin-api-version": "2.1.0" + "plugin-api-version": "2.3.0" } diff --git a/config.example.php b/config.example.php index 52c6365..c5e37ed 100644 --- a/config.example.php +++ b/config.example.php @@ -5,6 +5,7 @@ define("NC_PASSWORD", "****"); define("MAIL_SERVER", "localhost"); // server.domain define("MAIL_SERVER_FLAGS", "/novalidate-cert"); // flags needed to connect to server. Refer to https://www.php.net/manual/en/function.imap-open.php for a list of valid flags. define("MAIL_SERVER_PORT", "143"); +define("MAIL_SERVER_SMTPPORT", "587"); // port for outgoing smtp server. Actually only used to configure Docker image outgoing SMTP Server define("MAIL_USER", "incoming"); define("MAIL_PASSWORD", "****"); define("DECODE_SPECIAL_CHARACTERS", true); //requires mbstring, if false special characters (like öäüß) won't be displayed correctly diff --git a/docker-utils/configure_smtp.sh b/docker-utils/configure_smtp.sh new file mode 100755 index 0000000..90016db --- /dev/null +++ b/docker-utils/configure_smtp.sh @@ -0,0 +1,14 @@ +#!/bin/sh + +CONFIG_FILE_PATH="config.php" + +cat > /etc/ssmtp/ssmtp.conf <&2 echo 'ERROR: Invalid installer checksum' + rm composer-setup.php + exit 1 +fi + +php composer-setup.php --quiet +RESULT=$? +rm composer-setup.php +exit $RESULT \ No newline at end of file diff --git a/docker-utils/test_mail.php b/docker-utils/test_mail.php new file mode 100644 index 0000000..3259c45 --- /dev/null +++ b/docker-utils/test_mail.php @@ -0,0 +1,13 @@ + php docker-utils/test_mail.php test@example.com + +$to = $argv[1]; +$subject = 'An issue has been reported!'; +$message = 'Mail2Deck response

You created a new issue on board XXXXX.

Check out this link to see your newly created card.

'; +$headers = array( + 'From' => 'no-reply@example.com', + 'MIME-Version' => '1.0', + 'Content-Type' => 'text/html' +); + +mail($to, $subject, $message, $headers); diff --git a/index.php b/index.php index 1d8fba6..a1d42ac 100644 --- a/index.php +++ b/index.php @@ -98,13 +98,13 @@ if ($emails) $newcard = new DeckClass(); $response = $newcard->addCard($data, $mailSender, $board); - $mailSender->userId .= "@{$overview->reply_to[0]->host}"; + $mailSender->origin .= "{$overview->reply_to[0]->mailbox}@{$overview->reply_to[0]->host}"; if(MAIL_NOTIFICATION) { if($response) { - $inbox->reply($mailSender->userId, $response); + $inbox->reply($mailSender->origin, $response); } else { - $inbox->reply($mailSender->userId); + $inbox->reply($mailSender->origin); } } if(!$response) {