Simple way to send a mail form command line from localhost - email

I want to send a simple E-mail notification (just an alert) from a bash script and came across mail, but there still you need to configure a mail server and I have not found any good tutorial on that. So is there another way to simply send an E-mail to my a gmail adress from the command-line using only my localhost?
Using Ubuntu 20.04

Related

How to set up mail server(postfix or anything else) on ubuntu?

I have installed Ubuntu Bionic on my virtual machine and have an application set up.
Now what I need is to send emails from my own server.
I am trying to set up postfix to do that but nothing works. I have tried many tutorials but still no success....even worse - the more tutorials I try the more confused I get.
So what have I tried?
I used apt install mailutils to install postfix and I chose internet site
When it asked for FQDM I entered mydomain.com .
Here is the first question. Some tutorials say to change it to mailmydomain.com .
So is it supposed to be a subdomain?
Do I need to create an A record to my DNS?
The I try to send an email
email" | mail -s "This is the subject line" somemail#mail.com
But nothing happens...no mail in my mailbox and no mails in posfix queue.
Is there a step by step tutorial for a complete beginner?
Do I need to create a MX record pointing to my server?
Do I need to create a TXT record? I read that that is how other mail servers validate a sender.
If yes, then how to do it?
So what do I have to do to send and email from my own server?
At this point I don`t even need to receive and email - just send it
And I don`t need to use postfix...I can use whatever works.

Can I send email from Jenkins using just sendmail or do I need to install some kind of SMTP gateway?

I currently have Jenkins setup to send through an AWS SMTP server, but I would like to change this so that I can send emails directly on the local network.
I have sendmail installed on the server, and a terminal test proves that Sendmail can transmit emails to an external address, so I know this is working, but I can't for the life of me get Jenkins to send directly via SendMail.
The few examples I find suggest Jenkins and SendMail are all I need but if that's the case then the setup doesn't work.
Please advise.
Jenkins sends email via SMTP so as long as you have sendmail listening on loopback and configured to accept email it should work as long as you put localhost as the mail server
I run into this problem today. Here is my solution for anyone who is looking for answer of the same problem.
If you don't want to setup a smtp provider such as gmail, you can install postfix (see here about how to install postfix non-interactively) on the maschine/container on which Jenkins is running.
Make sure postfix runs well. In my case postfix did not start automatically after installation.
service postfix start
Then in Jenkins select Manage Jenkins => Configure System => Email Notification. Enter localhost In field "SMTP Server". Let Jenkins send out a test mail to test it.

Send a terminal command through email

Is it possible to send an email which launches a predetermined command on a host machine? I wrote a script that sends an email every time some task finishes and it would be really great if I could send an email back that would launch something else. I am using Ubuntu 12.04.
Its not easy but nothing is impossible
Here is an example structure for linux
Use alpine to read your mail see here
Use alpine's notification to run your sendmail script see here
I don't think this is possible, is the machine receiving the email on the same network? if so you could ssh into the machine automatically and start a process

How can I quickly bulk-send files to an email?

I regularly send files in Gmail to exchange files between different computers. It is cumbersome. I want quickly to send my files, such as error logs. I found a promising article [1] about Mutt, but it apparently is not what I want:
Please note that mutt is a pure MUA
and cannot send e-mail without proper
email server .
I want no email server, since I cannot install it to my clients' computers. I don't need to know the sender. I want quickly to send many error logs to my Gmail. I am looking something like:
mutt -s "Many error logs" -a
./log1 -a ./log2 -a ./log3 mygmail#gmail.com
I would really love to do the job in terminal. It is too time-consuming to bulk-send files in Gmail. How can I quickly send a great amount of files?
[1] http://www.cyberciti.biz/tips/sending-mail-with-attachment.html
Mutt can be configured to use a different MTA than the default sendmail. You could install SSMTP (described here and available for download here) or ESMTP and configure it to send directly to Gmail's web server, then configure Mutt to use SSMTP or ESMTP. This is nicely described in this Ubuntu tutorial. If installing systemwide binaries isn't an option, you ought to be able to install SSMTP or ESMTP to a local folder.
If you prefer scripting, mimesend, which is one of the example programs in Perl's MIME-tools package, does the same thing as your mutt command and could be hacked to connect to Gmail's web server.
Why are you using email for this? Other options:
FTP/SCP (run a server yourself, or purchase a hosted service)
Internet storage service like Dropbox.
assuming that you don't want to use ftp/scp as suggested above, here is how i solved a similar problem ... emailing logs from a client:
i created a cgi script on my server that had the ability to mail things to myself. a sort of email proxy webservice.
the client posted the files it needed to send (along with authentication, obviously) to that cgi script.
that way the client only needs to talk to my server instead of talking to a mail server.
perhaps this helps.
-don

automation: email yourself a file

I have a computer at home which I can't access from work. I'd like to be able to view results from work that my home computer produces. The best idea I've come up with is an automated script running on my home computer that emails myself the results (from a text file or stderr/out) when complete.
I'm decent with bash (I have a linux machine) and java, so an answer using either or both of those would be ideal, but if there's something easier that's fine too.
I typically use gmail, but also have yahoo mail.
My question is this: what would be the basic steps in solving this problem? I can do the nitty gritty stuff, but can't really get the big picture of how something like this would work.
Please help.
jbu
Howto set up ssmtp to send through a Gmail account
Some of the steps here might seem strange at first, but the rationale is put
in footnotes that should hopefully explain why.
First create a spare account on gmail which you will only use for
sending email. For instance, if your normal account is user#gmail.com,
create an account user.noreply#gmail.com with a newly created password
which you only will use for this account [1].
Set up the new account to forward all email to the normal account [2]
and under account settings you should add all other email adresses you
use [3].
Then install ssmtp (On Debian: aptitude install ssmtp) and edit ssmtp's configuration file /etc/ssmtp/ssmtp.conf:
root=user#gmail.com
mailhub=smtp.gmail.com:587
UseSTARTTLS=YES
AuthUser=user.noreply
AuthPass=passwdusedonlyforthisaccount
FromLineOverride=YES
and configure the local mail delivery by editing /etc/ssmtp/revaliases
assuming that your local login is localuser:
root:user#gmail.com:smtp.gmail.com:587
localuser:user#gmail.com:smtp.gmail.com:587
Make sure the two configuration files are readable to all users who
should be able to send email [4].
Test the setup by e.g. mailx (On Debian: aptitude install bsd-mailx):
echo 'testing, one, two' | mailx -s 'test 1' user#gmail.com
Hope this helps.
[1] The new gmail user name and password will be visible to everyone who
can log onto your machine, so you do not want this account to be
critical in any way, meaning you can close it down immediately if
someone should get access to it.
[2] If some email you sent bounces back to you, you might want to know
about it, and there actually exists people who will happily reply to an
email from johnsmith.noreply.
[3] Gmail will rewrite the From header on the email if it does not recognise the address.
[4] Ssmtp runs as the local user who sends the email, so that user needs
read access to the configuration files.
On any Linux I have used the mail sending from command-line is simple:
mail -s "My subject here" recipient#wherever.com <message_body.txt
AFAIK this acts as a front-end to sendmail, and you have to have sendmail configured to forward the messages to your ISP mail server.
You can't access your home computer from work which rules out a "remote support" option.
Can you access other computers on the Internet? If so, you could simply set up one of the online storage options and then ftp the results from your home computer. That's a lot simpler then trying to write scripts or code to generate emails with attachments or whatever.
You could then view the external computer from work.
If you have netcat, this command will send you an e-mail:
Given a file in this format (from Wikipedia):
HELO relay.example.org
MAIL FROM:<bob#example.org>
RCPT TO:<alice#example.com>
RCPT TO:<theboss#example.com>
DATA
From: "Bob Example" <bob#example.org>
To: Alice Example <alice#example.com>
Cc: theboss#example.com
Date: Tue, 15 Jan 2008 16:02:43 -0500
Subject: Test message
Hello Alice.
This is a test message with 5 headers and 4 lines in the body.
Your friend,
Bob
.
QUIT
Then netcat it to an SMTP server you have access to:
nc mail.somewhere.com 25 < file.txt
This will then send the e-mail. You can see how you can create a Java program to do this for you (just execute the commands).
Traditionaly, with unix systems like Linux, you'd have an MTA, a mail transfer agent, on the computer that deals with sending e-mail.
This could be a full blown e-mail server like exim, or something simple like ssmtp that just sends messages on to a relaying SMTP server such as would be provided by your ISP.
This isn't neccessarily the case anymore, since mail clients like Thunderbird include their own MTA, much like mail clients on Windows do.
However, it is likely that your distro will install some MTA or other by default, if for no other reason than the fact that other things on your system, like cron, want to be able to send e-mail. Generally there will be a command line tool called sendmail (sendmail being the original MTA [citation needed], other MTAs maintain compatability with its interface and it has sort of become the standard) that can be used from a shell script to send an e-mail.
My solution assumes that you have a SMTP server available which allows you to send an email programmatically. Alternatively, you can use a local install of sendmail which generally is available with most linux distros.
Create a standalone java program which watches the directory your home computer saves the file to. Use the JavaMail API to attach and send the file to any email you wish.
If you're also familiar with the Spring Framework, it has a nice abstraction layer for working with JavaMail and makes this sort of thing trivial.
Of course, your home ISP probably has the common SMTP port blocked as well.