send mail from linux terminal in one line [closed] - linux

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 6 years ago.
Improve this question
I know there is the command mail in linux to send emails via command line. How can I send an simple email with one line from the terminal though?
For example:
mail user#gmail.com [subject] [body]
And have the email sent without any confirmation or prompts to the user?
The reason is, I want to send a brief message via email to myself when a specific event happens in a java program. The idea is that I will use Runtime.getRuntime()… etc. to send the mail command from my java program.
I used cron to do something similar in the past, but the current implementation doesn't use cron, so I need to try this out instead.

mail can represent quite a couple of programs on a linux system. What you want behind it is either sendmail or postfix. I recommend the latter.
You can install it via your favorite package manager. Then you have to configure it, and once you have done that, you can send email like this:
echo "My message" | mail -s subject user#gmail.com
See the manual for more information.
As far as configuring postfix goes, there's plenty of articles on the internet on how to do it.
Unless you're on a public server with a registered domain, you generally want to forward the email to a SMTP server that you can send email from.
For gmail, for example, follow
http://rtcamp.com/tutorials/linux/ubuntu-postfix-gmail-smtp/
or any other similar tutorial.

echo "Subject: test" | /usr/sbin/sendmail user#domain.com
This enables you to do it within one command line without having to echo a text file. This answer builds on top of #mti2935's answer. So credit goes there.

You can use an echo with a pipe to avoid prompts or confirmation.
echo "This is the body" | mail -s "This is the subject" user#gmail.com

For Ubuntu users: First You need to install mailutils
sudo apt-get install mailutils
Setup an email server, if you are using gmail or smtp. follow this link. then use this command to send email.
echo "this is a test mail" | mail -s "Subject of mail" username#domain.com
In case you are using gmail and still you are getting some authentication error then you need to change setting of gmail:
Turn on Access for less secure apps from here

You can also use sendmail:
/usr/sbin/sendmail user#domain.com < /file/to/send

You can install the mail package in Ubuntu with below command.
For Ubuntu -:
$ sudo apt-get install -y mailutils
For CentOs-:
$ sudo yum install -y mailx
Test Mail command-:
$ echo "Mail test" | mail -s "Subject" youremail#domain.com

Sending Simple Mail:
$ mail -s "test message from centos" recipient#example.com
hello from centos linux command line
Ctrl+D to finish

Related

Linux shell emailing - set sender address using "mail" command

I am using the "mail" command in Linux shell to send an email when programmatically prompted to do so. I am using
mail -s 'subject' recipient#theirhost.com <<< 'Email body'
to send it at present, but this does not include a sender address. Instead, the system uses the account name as the sender address. I've seen the claim that "-aFrom:myname#myhost.com" should work, but -a is being interpreted as an attachment attempt, not a header change. This results in the error: "From:Mynamemyname#myhost.com: No such file or directory". I've also seen a claim that this works:
-r "from#fromserver.com"
But the OS doesn't even know what that means, in my case.
Is there a way to make this work without additional software? I don't need a from name, but a from address would be very helpful.
Note: let's consider ssmtp, mutt, and other such add-on software unavailable. Attempting to install and use such software would be problematic.
Thank you!
Edit 1: I'm not certain how to check what version of "mail" I have. With some difficulty, I got it to spit out the usage, which is as follows.
-T FILE -u USER -h hops -r address -s SUBJECT -a FILE -q FILE -f FILE -A ACCOUNT -b USERS -c USERS -S OPTION users
Never mind, I got it. "-r" can actually work.
I successfully used:
echo "This is the message body" | mail -s "This is the Subject" -r "FromName<fromAddress#example.com>" recipientName#example.com
It looks like karakfa was onto something also, as I do have mailx available to me also, but it looks like I won't be needing that for this use case.
Thank you!

Got error "passwords do not match" using passwd in docker [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 3 years ago.
Improve this question
I wanna create a user had random password for FTP in Docker. The section of Dockerfile like below:
RUN useradd web
RUN export ftppass=`cat /proc/sys/kernel/random/uuid`
RUN echo -e "$ftppass\n$ftppass" | passwd web
But I got an error in last line:
New password: Retype new password: Sorry, passwords do not match.
passwd: Authentication token manipulation error
passwd: password unchanged
Why the passwords do not match even I using a same variable?
update:
I found the output of echo -e "$ftppass\n$ftppass" is:
Step 9/15 : RUN echo -e "$ftppass\n$ftppass"
---> Running in ddd97df41d85
-e
Removing intermediate container ddd97df41d85
---> a64b606ea898
Step 10/15 : ...
Why it's not works for echo -e and where are my $ftppass?
Resolved, the new section of Dockerfile is:
RUN useradd web
RUN cat /proc/sys/kernel/random/uuid > /etc/vsftp-password
RUN echo "web:"`cat /etc/vsftp-password` | chpasswd
RUN echo "ftp info web:"`cat /etc/vsftp-password`
Thanks anyone and happy new year~
Instead of using passwd in a creative way, you should rather look at its manual page, which mentions chpasswd in the See Also section.
The passwd command is meant for interactive use, and it doesn't read its input like most other programs, since the input is not echoed to the screen.

Bash script check permissions to run command on remote [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 4 years ago.
Improve this question
I have a local development machine and from my bash script am sending commands to the remote server.
How can I write bash code to check if I am allowed to run the remote command so that I can handle the success/failure response from my script?
Alternatively, how can I capture the output so that I can parse it and detect if it succeeded. The difficulty with parsing is that the ssh command might trigger a password prompt so I can't interfere with that.
That bash script uses ssh -qt to send the remote commands
Command
ssh user#host -qt "sudo -u www /usr/local/bin/php /mnt/data/script.php"
Output:
[sudo] password for xxx:
Sorry, user xxx is not allowed to execute '/usr/local/bin/php /mnt/data/script.php' as www on host.domain.com
Assuming that user != root above: you can't - there's no way to read /etc/sudoers or /etc/sudoers.d/* in a normally set-up Linux box if you're not root, so apart from trial & error there's nothing to be done.
As for capturing the result - that's fairly simple (parsing it, of course, is a different story, depending on what you're doing over there).
output=$( ssh user#host -qt "sudo -u www /usr/local/bin/php /mnt/data/script.php" 2>&1 )
After the execution (and you typing the password for sudo)
echo $? # gives you the return-code of what happened on the far end, if it's a success that should be 0
echo $output # gives you the strings to parse

Alternative to mail or mutt in Linux to send mail?

I have a CentOS server which does not have the mail client. I do not have sudo access to install it or to install mutt.
What may be the best alternative that I can use to send myself an email to notify a failure/success of a process that I run on this server?
Maybe you could write a quick script in Python doing the job. Python is surely already installed, and you will be able to do the whole job with the standard library. Here is a small example as a starting point: https://docs.python.org/2/library/email-examples.html
Regards.
Try this out:
/bin/mail -s "Enter Subject Line here" -a /home/.../whateverFileYouWantAttached "you#YourEmailAddress" < /home/me/textBody.letter

How to use linux command line ftp with a # sign in my username? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 8 years ago.
Improve this question
How can I run this on linux command line when my username has an # sign in the middle?
ftp -u user:password#host/destination_folder/ sourcefile.txt
My username is info#domain.com and it thinks my host is domain.com.
NOTE: This is an unattended upload, so I can't just type in the username and password.
Try this: use "%40" in place of the "#"
As an alternative, if you don't want to create config files, do the unattended upload with curl instead of ftp:
curl -u user:password -T file ftp://server/dir/file
Try to define the account in a ~/.netrc file like this:
machine host login info#domain.com password mypassword
Check man netrc for details.
I simply type ftp hostdomain.com and the very next prompt asked me to enter a name, if it wasn't the same as my current user.
I guess it depends on how your FTP is configured. That is, whether it assumes the same username (if not provided) or asks. the good news is that even without a solution, next time you face this it might Just Work™ for you :D
A more complete answer would be it is not possible with ftp(at least the ftp program installed on centos 6).
Since you wanted an un-attended process, "pts"'s answer will work fine.
Do the unattended upload with curl instead of ftp:
curl -u user:password -T file ftp://server/dir/file
%40 doesn't appear to work.
[~]# ftp domain.com
ftp: connect: Connection refused
ftp> quit
[~]# ftp some_user%40domain.com#domain.com
ftp: some_user%40domain.com#domain.com: Name or service not known
ftp> quit
All I've got is to open the ftp program and use the domain and enter the user when asked. Usually, a password is required anyway, so the interactive nature probably isn't problematic.
[~]# ftp domain.com
Connected to domain.com (173.254.13.235).
220---------- Welcome to Pure-FTPd [privsep] [TLS] ----------
220-You are user number 2 of 1000 allowed.
220-Local time is now 02:47. Server port: 21.
220-This is a private system - No anonymous login
220-IPv6 connections are also welcome on this server.
220 You will be disconnected after 15 minutes of inactivity.
Name (domain.com:user): some_user#domain.com
331 User some_user#domain.com OK. Password required
Password:
230 OK. Current restricted directory is /
Remote system type is UNIX.
Using binary mode to transfer files.
curl -f -s --disable-epsv -u someone#somewhere.com:gr8p455w0rd -T /some/dir/filename ftp://somewher.com/ByramHealthcareCenters/byram06-2011.csv
I've never seen the -u parameter. But if you want to use an "#", how about stating it as "\#"?
That way it should be interpreted as you intend. You know something like
ftp -u user\#domain.tld#ftp.host.tld

Resources