mail can't send messages: Process exited with a non-zero status - linux

I wrote a bash script that sends out a mail, but after 50 e-mails it starts to say "mail can't send messages: Process exited with a non-zero status". Can anyone help solve my problem. The code I used is below if you want to take a look at it.
#!/bin/bash
#Declare variables area.
emailBody=email_body.txt; #you have to use without “ symbol for some reason
emailList=email_list_delimiter.txt;
#send mail command. using a read file loop.
while IFS= read -r emailTo; do
cat $emailBody |
mail -s "Hi, I'm looking for a position in IT Field." $emailTo |
echo “Success”;
done < <(grep . $emailList)

You are probably hitting a server-side limit on the number of messages you can send in a fixed time, or equivalently the number of connections allowed within a moving window of time.
If you can (the message is not "personalized") it is best to send one message to multiple recipients, rather than many messages, each to one recipient. Do that by perhaps putting your own e-mail address in the To field, and then Bccing the whole of the list of recipients in one go. You'll have to check your mail command for how to do that.

Related

Why do Period will be ignored at the beginning of a sentence in SendEmail on Linux

I want to use command line to send an email on Linux so I choose sendEmail (a lightweight, command line SMTP email client). However, I find Period (.) at the beginning of a sentence will be ignored and it really confused me.
-m MESSAGE message body
My command:
sendEmail -f sender#example.com -t receiver#example.com -u "Test mail" -s smtp.example.com -xu sender#example.com -xp sender_password -m ".Hello\n..Hello\nHello.world" -o tls=no
What I want to display is:
.Hello
..Hello
Hello.world
But the result is:
Hello
.Hello
Hello.world
Thanks a million.
This is a bug in the sendEmail client. In SMTP a line containing nothing else but a single period . is used to indicate the end of the message (DATA segment in SMTP). To avoid unintended transmission termination if a message contains a line with a single period, on all lines starting with a period an extra period has to be added before the message goes onto the wire; and removed upon receive. It's the task of a proper SMTP client to take care of this. It's clearly a faulty behavior of the client.
To get around the bug, add an extra period.
For details see RFC5321, sections 4.1.1.4 and 4.5.4.

Use Japanese characters in subject and attach file with mailx in RedHat Linux

I am trying to use the mailx command for sending email with attachment (zipped) and am facing two issues, below is the command I use:
(echo "$BODY"; UUENCODE $ZIP_FILE $ZIP_FILE) \
| mailx -s $SUBJECT_1 -r " " $SENDER $RECIPIENT
My email subject contains space and Japanese characters.
The variable $SUBJECT_1 has the following statement
Subject: [Budget] Subtype Error and some JAPANESE CHARECTERS
I get bet following error:
contains invalid character '\203'
Moreover for testing purpose I changed the statement of SUBJECT_1 to Test Message
SUBJECT_1="Test Message"
It worked, but I receive only Test instead of Test Message and in the mail I could see two more email ids in the To like Message#domain.com and -r#domain.com
I have not implemented the mail body yet, once subject issue fixed will implement the same in body because Body will also have Japanese characters.
Please help me with this error, how to resolve and what am i doing wrong
There's a list of things you need help with here, more than I want to to handle exhaustively on a sunny Saturday afternoon. But some hints.
Quote your variables.
"$SUBJECT_1" is a single string, whereas $SUBJECT_1 is a list of space-separated words. The second word is your email recipient, and subsequent options are also recipients.
Subject.
The basic idea is that you need to include encoding data in the subject, because email headers are only supposed to include 7-bit ASCII.
Here is a hint at how you put special characters in your Subject line.
Here is another hint.
Here is the RFC that describes in lurid detail what you need to do. Asking your favourite search engine for information about "utf8 email subject" and "rfc1522" is probably a good idea.
Email client.
Finally, rather than learning how to use MIME, consider using mutt instead of mailx to send your mail. Mutt has a -a option to add attachments, making it WAY easier than constructing your own headers and body, which I'm not even sure you'd be able to do with mailx in the first place.

If either of the files exist then send email alert in bash shell script

Trying to mock a little shell script.. There is a process that already exists which creates one of the two files (i.e, file1 or file2). My goal is to be able to send email alert depending on which file is created.
For example, if file1 exists then send email to John, David, Smith stating that file1 was created. If file2 exists then send an email to the same group of people (John, David, Smith ) stating that file2 was created.
If I write a simple if-else it will work but I will need to repeat some parts of the code which I am trying to avoid.
#!/bin/bash
file="file1"
if [ -f "$file1" ]
then
send email to three people mentioned
else
send email to three people mentioned saying file2 was created since it always creates two files
fi
}
Here I am trying to construct a script in a better way and also I don't want to repeat "send email..." commands three times because I may need to add more people into the list in the future.
Please help. Thanks in advance.
You can create a function block that send a mail to a desired list of people.Call the function from either of the case
Something like
#!/bin/bash
userlist="$HOME/myusers.cfg" # one user per line
function send_alert {
while read -r user comment; do
echo "my command to send to ${user}"
echo "subject=$1"
echo "Content=Hello ${comment}\n$2"
done < "${userlist}"
}
file="file1"
if [ -f "$file1" ]; then
send_alert "My subject" "Content: ${file1} exists."
else
send_alert "My subject" "Content: ${file2} exists."
fi
I added an optional field comment in the myusers.cfg, so you can add some comment after a space (name person, something).
You can adjust send_alert for more business rules, such as only send once an hour an alert, only send during working hours except for the person on night shift, whatever.

Linux shell script to remove 2 day old frozen emails from exim queue

I want to put shell script under the cron job which will do following:
1) Removes 2 days old email messages which are in the Exim queue and are bounced/frozen messages, which won't be delivered. Script must not remove messages which are legitimate and are just waiting their time to be delivered.
2) Removes messages which are from invalid sender like null or <> and / or sent to invalid recipient nobody or <>
Thanks a lot.
exiqgrep is your friend! You can easily find queue items matching certain criteria and pipe the found message-id:s to a remove command. E.g:
exiqgrep -z -o 172800 -i | xargs -r exim -Mrm
which translates as follows: find queue items (exiqgrep) that are frozen (-z) and older than two days (-o 172800) and output their message id:s (-i) to xargs that runs only if getting any input (-r) telling exim to remove (-Mrm) the items with given message id:s.
Null (or <>) senders are certainly not invalid! Bounces and other (non-)delivery reports are typically sent from a null address in order to avoid infinite loops in case the bounce is not deliverable. However if you have lots of these on the queue and they stay there for long (e.g. if you are trying to bounce spam sent from fake addresses) you can certainly clean them up too. For example:
exiqgrep -o 86400 -f '<>' -i | xargs -r exim -Mrm
which finds queue items older than one day with a null sender and deletes them.
EDIT: you could also set the following option in your exim.conf to automatically remove frozen bounces after two days:
ignore_bounce_errors_after = 2d

Create serial mail on linux commandline

I would like to generate serial emails on the linux commandline. Assume I have a file indicating mail address, subject and message text in columns on separate lines for each recipient. I.e.
recipient1#mail.com subject1 text1
recipient2#mail.com subject2 text2
...
The script should use standard commands as I intend to send it to colleagues who should create some emails for me.
The loop over the lines could be with xargs ... Can I use the commandline tool mail?
It is important that the mails are not send immediately. Ideally it creates files for import in the users preferred mail client. So that senders can check the mails before they submit.
I also would like to be able to add attachments to the Mails.
I tried e.g.
function mail_kmail {
kmail -s "$2" --body "$3" --attach "$4" --composer "$1"
}
function mail_thunderbird {
thunderbird -compose "to='$1',subject='$2',body='$3',attachment='$4'"
}
and reading the input data from file with
while read recipient subject body attach $file
do
mail_kmail "$recipient" "$subject" "$body" "$attach";
done
but this will work only if my colleagues installed and set up either of these mail clients.
I found this (closed) related question:
How can i send automated email in linux? .
You can use mutt to send the emails, here is an example:
echo "This is the message body" | mutt -a "/path/to/file.to.attach" -s "subject of message" -- recipient#domain.com
Since it's hard to know what you are trying to achieving here, you can even create a configuration file to be used, but you'll to investigate more or give more details about your use case.

Resources