Usually I install msmtp as the local mailer, setup is much easier than postfix/others and it's quite capable.
With this content in /etc/msmtprc
defaults
tls_trust_file /etc/ssl/certs/ca-bundle.crt
account default
host smtp.gmail.com
port 587
tls on
auth on
user redacted#example.com
password password
from redacted#example.com
logfile /var/log/msmtp.log
aliases /etc/aliases
I am having the error:
CROND[1587]: (ec2-user) MAIL (mailed 580 bytes of output but got status 0x004e#012)
For all the cron that should send email for ec2-user
Sending with mailx works fine:
echo "TEX" | mailx -s "TEST" redacted#example.com
Any tips on debugging this issue? I can't find much information about the status code I'm getting
Answering myself, I found a way to trigger the error on a verbose way, basically you have to send a mail using sendmail:
echo "From: root \
To: ec2-user \
Subject: Hello World \
\
This is the email body" | sudo sendmail -d -t ec2-user
On the error message I'm getting there the error explanation:
sendmail: /etc/aliases: line 11: invalid address 'postmaster'
Because there where some entries created on the /etc/aliases file (probably they where already there in the ec2 image) with an structure like this:
mailer-daemon: postmaster
Since postmaster does not have any meaning for msmtp, it's throwing the error. After commenting out this lines (#) the mail is being sent normally
Related
i am using command below for sending emails via curl in a linux docker container.
curl ${REPORT_SMTP_SERVER} --mail-from ${REPORT_MAIL_FROM} --mail-rcpt ${REPORT_MAIL_TO} --upload-file error.txt
This command works pretty well and emails are sent.I was wondering is there any way how to send those emails with high priority?Unfortunately did not find any flag for that in curl documentation.
Your error.txt can include something like this :
From: "User Name" <username#gmail.com>
To: "John Smith" <john#example.com>
X-Priority: 1
Subject: This is a test
Hi John,
I’m sending this mail with curl.
I am having a little trouble with a bash script im writing, i can run the individual commands in terminal and it works just fine, but running from a bash script is returning the error;
mysql: [Warning] Using a password on the command line interface can be insecure.
ERROR 1045 (28000): Access denied for user 'root'#'localhost' (using password: YES)
The bash script is as follows;
#!/bin/bash
ssh user#myhost.com << EOF
/usr/bin/mysql -h localhost -u root -p_kHaTX(!G_$=Y5Xa
show databases;
EOF
I have tried to also wrap the password in '' like so
/usr/bin/mysql -h localhost -u root -p'_kHaTX(!G_$=Y5Xa'
and still nothing
But if I run the commands myself through the terminal, it all works just fine
Any help would be greatly appreciated.
Thanks
To resolve the error, and for security purposes also, store the password in a separate file, let's say:
echo '_kHaTX(!G_$=Y5Xa' > /root/mysql_pwd
Then pass the password as variable:
MYSQL_PWD=`cat /root/mysql_pwd` /usr/bin/mysql -u root -p -e "show databases"
I am looking for a Bash script to redirect a simple ls command output to a file on my Linux box from my FTP Server.
Here follows the step by step commands to illustrate what I am looking to script. The FTP site can be accessed without a user/password, so i am entering user as anonymous and password as blank when prompted.
ftp <FTP_SERVER>
Connected to <FTP_SERVER> (IP_ADDRESS).
220 Microsoft FTP Service
Name (<FTP_SERVER>:root): anonymous
331 Anonymous access allowed, send identity (e-mail name) as password.
Password:
230 Anonymous user logged in.
Remote system type is Windows_NT.
ftp> ls /Outgoing/Artemis/incremental/Hashes-1492870261.zip
227 Entering Passive Mode (10,37,108,77,5,87).
125 Data connection already open; Transfer starting.
04-22-17 07:11AM 227634 Hashes-1492870261.zip
226 Transfer complete.
I need the output of the command 'ls' command I am executing to be saved in a file on my linux box.
Here is the script i have:
ftp FTP_SERVER <<EOF >outputfile
quote USER anonymous
quote PASS
prompt noprompt
ls -la /Outgoing/Artemis/incremental/Hashes-1492870261.zip
quit
EOF
when i execute this i get a login failed error.
sh -x test.sh
+ ftp FTP_SERVER`
Password:
Login failed.
local: /Outgoing/Artemis/incremental/Hashes-1492870261.zip: No such file or directory
I used some random password as a test instead of blank (null) which I used previously, but still get the same error. How can I fix this?
Turn interactive mode off:
ftp -i -n FTP_SERVER <<EOF >outputfile
user <user> <password>
binary
ls -la .
quit
EOF
You might get on better with lftp:
lftp -e 'ls -l someFile.zip; quit' -u USER,PASSWORD FTPSERVER > ls.txt
where all the words in capitals need replacing by your own values.
Or you could try with curl:
curl ftp://FTPSERVER --user USER:PASSWORD
I am cobbling my first Linux shell command. This command runs a yum command and email results in a periodic cron bash shell job. I am OK up to the email part where I get a "No such file or dir" error on email address(!). Can someone unravel syntax and provide method that works. Can be other shell scripting language if bash is not best for this. Seem to be having trouble with multiple line commands.
#!/bin/bash
body="Some Text"
## output yum command to a work file
echo $body > /home/security_check.txt
yum --security check-update >> /home/security_check.txt
## this works!
## mail -s 'Linux Security patches required' bob#example.com < /home/security_check.txt
## this does not
mail \
-a "From: root#example.com" \
-a "MIME-Version: 1.0" \
-a "Content-Type: text/html" \
-s "Linux Security patches required" \
bob#example.com \
< /home/security_check.txt
## error message:
## From: root#example.com: No such file or directory
Take a look at this post on sending HTML email with Unix mail.
https://unix.stackexchange.com/questions/15405/how-do-i-send-html-email-using-linux-mail-command
Seems that your need for the -a flag is to send HTML mail.
Here is solution that worked for me. Requires the mailx, yum and yum-plugin-security modules. It seems that mailx behaves differently on different distributions. Comments welcome
#!/bin/bash
## script name: yum_security_patch_report.sh
## description: emails list of security patches, saved in /etc/cron.monthly so it will be sent monthly
## generate yum security report
yum --security check-update > /home/security_check.txt
## get server hostname
HOSTNAME=echo hostname
## compose subject
SUBJECT="List of Linux security patches required - for server $HOSTNAME"
## set up TO and CC
TO="bob#example.com"
CC="sue#example.com"
## get report summary from file saved, uses $(code) notation to catch as variable
SUMMARY=$(grep 'needed for security' /home/yum_security_check.txt)
RUNFROM=($"$0")
## compose body of email
MAILBODY="Find enclosed report of Linux modules that require security patches.
$SUMMARY
This report comes from server: $HOSTNAME. This script is being run from: $RUNFROM"
## send email with mailx command
echo "$MAILBODY" | mailx -v -s "$SUBJECT" -c "$CC" -a /home/security_check.txt "$TO"
How can I use the curl command line program to send an email from a gmail account?
I have tried the following:
curl -n --ssl-reqd --mail-from "<sender#gmail.com>" --mail-rcpt "<receiver#server.tld>" --url smtps://smtp.gmail.com:465 -T file.txt
With file.txt being the email's contents, however, when I run this command I get the following error:
curl: (67) Access denied: 530
Is it possible to send an email from an account that is hosted by a personal server, still using curl? Does that make the authentication process easier?
curl --ssl-reqd \
--url 'smtps://smtp.gmail.com:465' \
--user 'username#gmail.com:password' \
--mail-from 'username#gmail.com' \
--mail-rcpt 'john#example.com' \
--upload-file mail.txt
mail.txt file contents:
From: "User Name" <username#gmail.com>
To: "John Smith" <john#example.com>
Subject: This is a test
Hi John,
I’m sending this mail with curl thru my gmail account.
Bye!
Additional info:
I’m using curl version 7.21.6 with SSL support.
You don't need to use the --insecure switch, which prevents curl from performing SSL connection verification. See this online resource for further details.
It’s considered a bad security practice to pass account credentials thru
command line arguments. Use --netrc-file. See the documentation.
You must turn on access for less secure apps or the newer App passwords.
if one wants to send mails as carbon copy or blind carbon copy:
curl --url 'smtps://smtp.gmail.com:465' --ssl-reqd \
--mail-from 'username#gmail.com' --mail-rcpt 'john#example.com' \
--mail-rcpt 'mary#gmail.com' --mail-rcpt 'eli#example.com' \
--upload-file mail.txt --user 'username#gmail.com:password' --insecure
From: "User Name" <username#gmail.com>
To: "John Smith" <john#example.com>
Cc: "Mary Smith" <mary#example.com>
Subject: This is a test
a BCC recipient eli is not specified in the data, just in the RCPT list.
Crate a simple email.conf file like so
Username: hi#example.com
Password: OKbNGRcjiV
POP/IMAP Server: mail.example.com
And simply run sendmail.sh, like so after making it executable (sudo chmod +x sendmail.sh)
./sendmail.sh
Code
#!/bin/bash
ARGS=$(xargs echo $(perl -anle 's/^[^:]+//g && s/:\s+//g && print' email.conf) < /dev/null)
set -- $ARGS "$#";
declare -A email;
email['user']=$1
email['pass']=$2
email['smtp']=$3
email['port']='587';
email['rcpt']='your-email-address#gmail.com';
email_content='From: "The title" <'"${email['user']}"'>
To: "Gmail" <'"${email['rcpt']}"'>
Subject: from '"${email['user']}"' to Gmail
Date: '"$(date)"'
Hi Gmail,
'"${email['user']}"' is sending email to you and it should work.
Regards
';
echo "$email_content" | curl -s \
--url "smtp://${email['smtp']}:${email['port']}" \
--user "${email['user']}:${email['pass']}" \
--mail-from "${email['user']}" \
--mail-rcpt "${email['rcpt']}" \
--upload-file - # email.txt
if [[ $? == 0 ]]; then
echo;
echo 'okay';
else
echo "curl error code $?";
man curl | grep "^ \+$? \+"
fi
more
Mind that the form of mail.txt seems to be important / CRLF for win, LF for Linux, special characters etc.
Finally after struggling 2 hours, it works for me for GMX (they tell their SMPT port to be 587 - and further down in small letters the hint: "also 465 can be used with SSL"):
UNDER Linux (TinyCore Linux on Raspberry 3B+ with curl.tcz installed):
curl --ssl-reqd --url 'smtps://mail.gmx.net:465' --user 'mymail#gmx.at:mymailPassword' --mail-from 'mymail#gmx.at' --mail-rcpt 'mymail#gmx.at' --upload-file mail.txt
UNDER Windows:
curl --ssl-reqd --url "smtps://mail.gmx.net:465" --user "mymail#gmx.at:mymailPassword" --mail-from "mymail#gmx.at" --mail-rcpt "mymail#gmx.at" --upload-file mail_win.txt
with mail.txt:
From: "User Name" <mymail#gmx.at>
To: "John Smith" <mymail#gmx.at>
Subject: This is a test
Hi John,
Im sending this mail with curl thru my gmx account.
Bye!
Note that if Perl's "system()" function is used to execute the curl command, each argument 'word' is a separate item in the argument array, and words must NOT be quoted.
Also note that if sending via Gmail after May 30, 2022, the gmail account must be set up with 2-factor authentication and then you must create an "App Password". The App Password is a long character string that acts as an alternative password and replaces the usual password on the "--user" parameter.