Cant send email with bash script - bash

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).

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)

Windows 10 SNMP service not responding

I'm trying to get my head around SNMP for a project I'm working on. After I failed miserably getting it to work in my company's network, I set up a simple 3-device network to test things on, consisting of two Windows 10 PCs and a manageable switch between them.
I installed the optional feature "SNMP" on both PCs, made sure the service is running correctly and configured both services to accept SNMP queries from each other. I made sure to open up UDP port 161 in both PCs firewalls. Then I got the Net-SNMP binaries in order to use SNMPGET and SNMPWALK. As an alternative, I set up the SNMP extension for PHP through xampp (since I want to use PHP in my project once I get SNMP to work). Finally, I installed wireshark to monitor what exactly is going on and this is what I found:
When I try SNMPGET or SNMPWALK either through cmd or as a PHP command, I always get a timeout message. Wireshark is showing the get-next-request leaving one PC and arriving correctly on the other, so the network connection itself is working fine. But the receiving PC never sends a response. As I said, I'm pretty new to SNMP and I'm at a loss as to why this is happening. As I understand it, the optional feature for Windows 10 comes with its own SNMP agent, correct? If so, what could cause it to simply ignore an incoming request from a valid source IP?
The funny thing is that this even happens when I try to send an SNMP query to 127.0.0.1. I have no idea what I'm doing wrong...
Thanks to the comment of Lex Li, I was able to finally figure out which step I made a mistake with:
When setting up the SNMP service, under the security tab, I had to add 'public' as an accepted community name (with READ-ONLY rights). I figured since 'public' is sort of the standard read-only community, it would be accepted by default, which apparently it is not.
Alternatively, I guess I could have added my own communtiy name, but I didn't try that since I only want to read some values through SNMP anyways and read-only access is all I need for that.
Thank you very much Lex Li, I'm off to continue my project now!

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]

How to setup a mail server?

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.

Set up local SMTP and POP3 for testing mail send and receive loop

I would like to have alternate SMTP and POP3 servers on my local machine, for testing code that sends mails and waits for a response, even when I'm offline. GPRS rates in SA are quite prohibitive, so I try and avoid them where possible. I would like to have a test SMTP address instead of my routine Google one, and that SMTP server must be able to send mail to my local POP3 server, where my mail client can retrieve that mail.
How can I go about this on Windows 7 Home Premium?
I'm assuming your program that sends email can specify the smtp server address.
If so, then you need to install your own personal mailserver and configure your program to use localhost (127.0.0.1) as your smtp server. (When I say personal, I mean in the sense that you want something which does not have heavy system requirements.)
Years ago (before broadband and gmail) I used Surgemail (from http://netwinsite.com/surgemail/) on my home computer (It was free for up to 5 users)
But there are loads or others, for example http://www.hmailserver.com or Mercury 32 (from http://www.pmail.com/overviews/ovw_mercwin.htm)
You might think this is overkill, as these programs will probably do much more than you need, but I was surprised at how much they can do without needing a high spec machine. (From example, I was probably running Surgemail on a Pentium III with 1Gb of Ram)

Resources