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.
Related
How can I send POST request with a csv or a text file to the server running on a localhost using cURL.
I have tried curl -X POST -d #file.csv http://localhost:5000/upload but I get
{
"message": "The browser (or proxy) sent a request that this server could not understand."
}
My server is flask_restful API. Thanks a lot in advance.
There are many alternate ways to accomplish this. One way is
I have used the following:
curl -F ‘data=#<file_location>’ <URL>
Eg. curl -F data=#data.csv localhost:5000/h
Your command can also be changed slightly like this
curl -X POST -H 'Content-Type: text/csv' -d #file.csv http://localhost:5000/upload
The above is one of the many ways.It can be sent either as a part of form or data, or multipart, etc. You can refer Medium Post
Curl's default Content-Type is application/x-www-form-urlencoded so your problem is probably that the data you are POSTing is not actually form data. It might work if you set the content type header properly:
-H "Content-Type: text/csv"
Though it does depend on the server.
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
I am running a python script in my linux PC and use 'mailx' command to send email when script fails. The command is as mentioned below,
os.system(" mailx -a 'Content-Type: text/html' -s 'Failure: Log script status' abc#domain.com def#domain.com < ../report/log_output.html")
In this case user 'abc' & 'def' gets email in his outlook client with sender as
Alert User <alertuser#x26611-testbuntu04.unassigned-domain>
(which is a dummy email).
When any of the user try to do reply all, it will send a copy to
Alert User <alertuser#x26611-testbuntu04.unassigned-domain>
as well. I dont want this to happen.
How can I write mailx command by specifying my own Reply-To email list while sending script failure email itself.
I am using below,
Distribution: Ubuntu 14.04.5 LTS (GNU/Linux 3.13.0-135-generic x86_64)
&
Edited
$ dpkg -s mailutils
Package: mailutils
Status: install ok installed
Priority: optional
Section: mail
Installed-Size: 1674
Maintainer: Ubuntu Developers <ubuntu-devel-discuss#lists.ubuntu.com>
Architecture: amd64
Version: 1:2.99.98-1.1
Provides: mail-reader, mailx
If the version of mailx you are using supports adding custom headers with the -a option like you already do with -a 'Content-type: text/html' then you can just supply it a second time with the header you want; -a 'Reply-to: you#example.net'
Relying on mailx is somewhat brittle and a huge portability problem because there are multiple incompatible mailx implementations in common use.
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.