How to setup a mail server? - ruby

I want anyone who send a email to test#example.com, and the server could get the mail and so I can use some server side scripting language like Ruby to parser the content of the email.
Ruby solution would be great.

If you've never set up an smtp server, and you're not willing to dig in and learn a lot, you might want to check out some of the services that offer this kind of ability. I have never used any of these, not sure about quality or pricing aspects.
cloudmailin
mailgun
dispatch
postmark

An alternative to setting up and configuring a mail server is to use a service such as sendgrid or postmark. You point your DNS mx records at them and they will turn inbound messages into JSON documents and post them to you

You can install qmail on your server, and configure it to pipe incoming mail to your domain to the STDIN of your ruby script.
If you haven't done so already, create an MX record for your domain that points to your mailserver's IP. Then, login to the server as root, then install qmail by following the instructions at http://lifewithqmail.org/lwq.html#installation
Configure qmail to accept incoming mail for *#yourdomain.tld (catchall), and forward all incoming mail to the script at /home/yourhomedirectory/yourscript.sc:
add the following line to /var/qmail/control/me:
hostname.yourdomain.tld
add the following line to /var/qmail/control/defaultdomain:
yourdomain.tld
addd the following line to /var/qmail/control/virtualdomains
yourdomain.tld:yourusername
add the following line to /var/qmail/control/rcphosts:
yourdomain.tld
restart qmail:
qmailctl stop
qmailctl stat
Logout of the server, then log back in again as yourusername. Create a file /home/yourusername/.qmail-default, consisting of the following line:
| /home/yourhomedirectory/yourscript.sc
Create /home/yourhomedirectory/yourscript.sc script to process incoming mail. Enable executable permissions on the script for all users:
chmod a+x /home/yourhomedirectory/yourscript.sc
That's all there is to it. Incoming mail to your domain will be piped by qmail to this script's STDIN.

Install and configure an SMTP server. SMTP servers can usually be configured quite extensively, what to do with mails that should be deliverd locally. One such option would be to deliver the mail locally by invoking a ruby script. Popular SMTP servers are postfix, courier and qmail. The details about how to setup the mail server depends on which one you use.

Related

Postfix triggering a script

I've installed Postfix as mail server following this tutorial.
I've no problem sending emails. I would like to use the mail server to trigger a shell script each time a mail is received. To do so I've added the following code to the 'aliases' file:
myemailaccount: "|/home/pi/Desktop/TestScript/TestShell.sh"
The shell is also quite simple, I only try to generate a text file in order to know if the shell has been executed :
#!/bin/sh
echo "Let's check if this works!" > Output.txt
So I've tried to send an email to 'myemailaccount#gmail.com' waiting to see the file 'Output.txt' to be generated but nothing happens. If I run the shell from the terminal, no probs, it works.
It's seems that you have setup Postfix to send emails thru Gmail.
To receive emails from outside is another painfully road with a lot of tricky parts.
To receive emails you need a Reverse IP from you ISP and check if port 25 is visible outside. Email servers check for this ports to issue SMTP protocol verifications.
Nowadays, you also need a valid certificate to operate TLS and SSL over you server.
You can use Let's Encrypt free ones.
Don't forget to setup DKIM, SPF and MX stuff in your DNS.
You also need to setup Dovecot or Courier servers:
https://docs.gitlab.com/ee/administration/reply_by_email_postfix_setup.html
https://www.digitalocean.com/community/tutorials/how-to-set-up-a-postfix-e-mail-server-with-dovecot
If you are confused why do you need both Dovecot and Postfix, Read This!
But you can check this solution, if you don't need too much tech:
Execute script when new email arrives (Postfix / Dovecot)

How to run a shell script file whenever a new mail is received in Unix mailbox server?

I need to execute a .sh file whenever a new mail is received in mailbox server (UNIX). How can i do it? I am new to Shell scripting.
Standard suggestion is to use something like procmail program. It can be configured to process messages delivered to user's mailbox. procmail can execute custom script for specific email e.g. based on sender, subject and size.
On many linux system sendmail uses procmail by default for deliveries to user's mailbox.
In such case all you need is ~/.procmailrc script.
Which MTA/SMTP server do you use? [see SMTP greeting via telnet localhost 25]

Cant send email with bash script

The terminal does not pop out any error message, but I never receive the email.
this is my code:
mail -s "hello" "example#example.com" <<EOF
hello
world
EOF
Works fine for me:
pax> mail -s "hello" "pax" <<EOF
hi there
EOF
pax> mailx
Mail version 8.1.2 01/15/2001. Type ? for help.
"/var/mail/pax": 1 message 1 new
>N 1 pax#paxbox.com Sat Jun 14 10:25 16/629 hello
& _
You should try it with a local address first (as I have) to see if a mail is being created.
Beyond that, you should realise that mail simply adds mail messages into the mail system. If you want to find out what happens after that, you'll need to look into whatever MTAs (mail transfer agents) you have set up on your system.
If the MTA itself fails, you'll almost certainly get a mail back to the sending account stating so (you can use mailx as I have above, to discover this).
Since you haven't specified your systems, I'll give advice below based on Debian since that's what I'm used to.
On my Debian box, exim is the MTA but, by default, it does not support sending to remote domains. You can modify this by running:
sudo dpkg-reconfigure exim4-config
but you need to be careful not to relay emails lest you unknowingly become a spam-bot. More details can be found here.
You may find, if you want them to go to the outside world, that it's better to send them to your ISP via SMTP rather than trying to configure mail on your local box to do it.
However, if you want to go the mail route, simply run dpkg-reconfigure as above, select "Internet site; mail is sent and received directly using SMTP" as the answer to the first question, then accept defaults for all the other questions (checking to ensure you only accept mail from your local addresses 127.0.0.1 and ::1).
Then wait for exim to restart and try send the mail again.
Just be aware that exim typically starts queue runners (the processes that actually send out your email) on a schedule (30 minutes for me) so it may take some time for the message to go out.
You can examine the files in /var/log/exim4 to see what's happening (such as, in my case, my ISP rejecting the attempt since it knows nothing about pax#paxbox.com but you may be able to find an open SMTP relay somewhere or spoof your sending details to something your ISP will allow).

proxying through corporate firewall

I'm trying to get some protocols work through my company's firewall. Until now I have been succesfull in masking either http or https data by setting a http proxy on localhost and one on a remote server I own. The communication is done via $_POSTed and received modified .bmp files that contain a header and the encripted serialised request array.
This works fine, but there are a few drawbacks that make me think I might have taken a wrong approach.
Firstly I do not use apache's mod-proxy. instead I just created a local subdomain (proxy.localhost) and use that in browser's proxy settings. the subdomain's index.php does all the work. This creates some problems. I cannot use http and https simultaneously or the server will complain of using either "http on a https enabled port" or "incoresc ssl response length".
The second problem is, well, other protocols. I could make use of some ftp, sftp, remote deskoptop, ssh, nust name another... I need it
there are 2 solutions I can think of: First is if I run a php script in CLI so that it listens on a predefined port and handles the requests differently, or some sort of ssh tunnel. Problem is I haven't had any success with freeSSHd and putty because of my ignorance.
Thanks in advance for any advice.
I used the free version of bitvise SSH Client and server and it seems to work just fine.

jabber - create room

I have the following setup:
A server with CentOS 6
XMPP server (used ejabber)
A browser client (used Candy-chat)
Chat is working, users have permissions, etc.
What I need to do is to create a room when user X wants to. If the user uses a stand alone client (like Pidgin) everything is ok but I need to create a room using a browser based client.
The question is how do I do that?
Note: Tried to find a client that supports the feature - failed.
Note 2: It is ok if I can do it from server command line (shell script) since I can send an ajax request to a php script that runs the line. - I tried to use ejabberdctl for this but I couldn't find any functions that do that (found 9999 functions about users, server, etc. but nothing regarding rooms. I guess I must edit the mod_muc options then restart the server but I'm not sure and is not right for all users to restart server).
Thank you for your time.
See http://xmpp.org/extensions/xep-0045.html#createroom
XEP-0045 define the Multi-User Chat protocol in XMPP. You don't need to use the command line or any other out-of-band communication, the protocol is already defined and works in-band in the user XMPP session. This is what any Jabber client that supports chat rooms implements.
Note that the protocol itself is long, complex and has many options and alternatives (to configure the room parameters, affiliations, do administration work, etc), but you don't have to use all of them, see Example 153, just
<presence from='crone1#shakespeare.lit/desktop'to='coven#chat.shakespeare.lit/firstwitch'>
<x xmlns='http://jabber.org/protocol/muc'/>
</presence>
will be enough to create room "coven". After that user "crone1" will be the owner of that room, and will be logged in the room with nick "firstwitch".

Resources