Sending an email from a bash script on OSX 10.10.5 - bash

I have the local smtp relay setup correctly, I have several scripts using a simple one line-ish mail command:
echo "I am the body." | mail -s "I am the subject." notamango.me#company.com -F "ReplyToPrettyName" -f ReplyToAdrress#server.com
This was working on 10.9.5, it has broken after an update to 10.10.5. Ok, I get that they changed something, but now I can't seem to get it to work. Has anyone successfully sent emails from a bash script in mac osx 10.10.5+ using a one line-ish command where I can specify the sender and replyto address? I am about to write a small piece to do the raw telnet commands if I have to, as installing non default apps isn't an option.

Took me some futzing about but this seems to be working:
echo "I am the body" | mail -s "$(echo -e "I am the subject.\nFrom: PrettyName <PrettyName#Server.com>\n REALReplytoAddress#Server.com\nContent-Type: text/html\n")" 'recipient.me#company.com'
Hope this helps someone else who is stuck.

Related

Mutt bash script sends email fine from command line but not from motioneye notification hook

So I'm about ready to pull my hair out on this one. I am running ssmtp and mutt on a freeBSD jail. I have a bash script called notify that contains the following line.
mutt -s "$subject" "$email" -a "$attachment" < "$logfile3"
When I run
bash notify.sh
The email will send just fine, but if I run that exact same command from inside motioneye I receive an extremely non-descript error from mutt
Could not send the message
I have tried using a daemon but that hasn't had any effect. I wish the mutt error message was more descriptive.
My script did not have access to the "sent" mailbox and therefore mutt was failing to send. -e "set copy=no" added to the mutt call corrected the issue.
For a better walkthrough: https://gitlab.com/muttmua/mutt/issues/119
Make sure you have bash installed (it isn't by default on FreeBSD) and try using absolute path to it (/usr/local/bin/bash).

mail (postfix) not running in launchAgent unless followed by another command

I'm running a user LaunchAgent in El Cap that is supposed to call postfix to send me an email when a certain automatically generated file is 0 length, meaning that something went wrong. Here's the line from the bash script where $me is my username:
echo 'Darn it' | mail -s 'Export failed' "$me"
It works fine if I run the script manually, but mail won't send anything when the agent runs.
By dumb luck I found that if I follow this command with the following command, the message will get sent:
/usr/local/bin/terminal-notifier -message "File export failed." -title "Alert"
Other following commands won't do it (like echo, for example).
Any idea what's going on there?

Send email from Linux command line to Gmail

There appear to be a number of answers to this question, but none of them seem to work:
I would like to send email from my localhost to a gmail account. The following command does nothing as far as I can tell:
mail -s "Hello!" address#gmail.com
After Ctrl-Ding to finish editing, there is no failure notification or anything, and the message never appears in that gmail account's inbox (or spam folder). I've tried various different ways of using that command from around the internet, but it appears that it just silently fails to send to nonlocal addresses. Any ideas?
I am using Debian Wheezy
EDIT: To be clear, the issue is not that I get command line prompts, which I know I can avoid with pipes etc. The issue is that mail is simply never sent. The command returns after a while, and it just silently fails.
Thanks!
You can use an echo with a pipe to avoid prompts or confirmation.
echo "This is the body" | mail -s "This is the subject" address#gmail.com
Make sure postfix or the MTA of your choice is installed and running.
sudo /etc/init.d/postfix status

postfix pipe mail to script does not work

I've done my research and tried lots of way but to no avail, i still could not get my postfix mail to run the script.
content of /etc/aliases
test2: "|/home/testscript.sh"
content of /home/testscript.sh Note: i've tried many kind of ways in the script. even a simple echo does not work.
#!/bin/sh
read msg
echo $MSG
i've tried running the script and it works fine.
So would you tell that it's working?
Even if you successfully direct mail to the script, you're not going to see the output of the "echo" command. If you expect to get an email response from the script, the script will need to call out to /bin/mail (or sendmail or contact an SMTP server or something) to generate the message. If you're just looking to verify that it's working, you need to create some output where you can see it -- for example, by writing the message to the filesystem:
#!/bin/sh
cat > /tmp/msg
You should also look in your mail logs (often but not necessarily /var/log/mail) to see if there are any errors (or indications of success!).

Can OS X Lion Server send an email when a user connects via SSH or VPN?

I'd like OS X Lion Server to send me an email alert whenever a user attempts to connect or establishes a successful connection to it via SSH or VPN or any other service.
The only email alerts OS X Lion Server can send are:
disk space
software updates
certificate expiration
Does anyone know how to make this happen?
Thanks!
You can create a script and save it in /etc/sshrc
#!/bin/bash
ADDRESS="your_email#example.com"
IP=`echo $SSH_CONNECTION | cut -d " " -f 1`
DATE=`date`
echo "User $USER just logged in from $IP at $DATE" | mail -s "ssh login alert" "$ADDRESS"
This will work using the default sshd settings in Mavericks. In order to avoid SPAM filters, once I get the first message in gmail, I mark it as important and add the sender to my address book.
You could write a process that tailed the ssh log at least, and kicked off an email with each appropriate entry.
something like:
#!/bin/bash
tail -f /var/log/secure.log | grep 'session opened for user' | mail -s "Server login event" "myemail#example.com"
A quick test, at least on Linux, shows it works. YMMV, you may have to adjust the grep for the appropriate pattern, and the usual caveats with email from random servers apply - check the mail log to see if went out, and of course, you'll need an MTA of some kind running. Don't be surprised if the email is rejected by the recipient MTA for about a dozen reasons.
If you need anything more sophisticated there are programs that do this kind of thing. I believe portsentry at least at one time was popular. You could crudely emulate it with tcpdump too if you are in a hurry.

Resources