MrOaiki
Posts: 33
Joined: Fri Jun 28, 2013 12:21 am

Send e-mails from the command line

Thu Jul 11, 2013 11:49 pm

I have googled. I have found various tutorials. I've tried everything, yet I can not get it to work. I want to send e-mails from my command line (and from my Bash script). Most tutorials out there refer to a Gmail account and using it as a mail hub. I do however want to use my own web host, who supports SMTP and SMTPS (SSL/TLS) for outgoing mail.

The closest I've gotten to send an e-mail was an error saying "550".

Any good ideas on where to begin?

User avatar
malakai
Posts: 1382
Joined: Sat Sep 15, 2012 10:35 am
Contact: Website

Re: Send e-mails from the command line

Fri Jul 12, 2013 12:19 am

Most reasons for using Gmail is that a lot of ISP's block a home setup also many dynamic ranges get blocked because of abuse so they get the entire block that an ISP will serve out and no matter what you do it will fail 550 indicates unknown mail box recipient.

The first step would be to google "what is my IP"

Put that into a site like http://www.sorbs.net/lookup.shtml a popular spam filter and if you come back in red you will face problems you can't overcome. Second contact your ISP if all looks good and ensure they are not blocking you.

By relaying through Gmail you overcome all the obstacles of rejections. Can you post some of the things you have tried and any other error messages as 550 would indicate it is working either your own server is generating the error because it is only trying to reach local the address on itself or you are getting the error from a remote server which means you have a working setup to the point your server is communicating with another server somewhere.
http://www.raspians.com - always looking for content feel free to ask to have it posted. Or sign up and message me to become a contributor to the site. Raspians is not affiliated with the Raspberry Pi Foundation. (RPi's + You = Raspians)

MrOaiki
Posts: 33
Joined: Fri Jun 28, 2013 12:21 am

Re: Send e-mails from the command line

Fri Jul 12, 2013 12:34 am

I tried:

http://c-mobberley.com/wordpress/index. ... h-postfix/
http://iqjar.com/jar/sending-emails-fro ... pberry-pi/
http://www.simplehelp.net/2008/12/01/ho ... mand-line/
http://rpi.tnet.com/project/faqs/smtp (quite clear instructions, but didn't work)
http://blog.a-netz.de/2012/12/sending-m ... pberry-pi/ (also quite clear, but no luck)

However, since asking my question here, I finally found a way that worked flawlessly. A program called sendEmail. No configuration files, no nothing. It just worked, right from the terminal!

sendEmail -f "fromaddress" -t "toaddress" -u "Subjectlinei" -m "Mymessage" -s "mysmtpserver":26 -o tls=no -xu "loginusername" -xp "loginpassword"

Done!

mikez104
Posts: 5
Joined: Fri Aug 02, 2013 2:42 am

Re: Send e-mails from the command line

Fri Aug 02, 2013 2:44 am

This is exactly what I have been searching for all night. This works great!

tiny68
Posts: 1
Joined: Tue Aug 06, 2013 12:52 pm

Re: Send e-mails from the command line

Tue Aug 06, 2013 12:55 pm

Yep, this worked for me too! I did need to leave out the ":26" for my mail server though.

Many thanks - this is perfect for what I want to do.

marcelp1
Posts: 132
Joined: Mon Jun 11, 2012 8:41 pm

Re: Send e-mails from the command line

Tue Aug 06, 2013 5:11 pm

Excellent program.

I have tried on smtp server with no authentication / username / password and it works.

sendEmail -f "fromaddress" -t "toaddress" -u "Subjectlinei" -m "Mymessage" -s "mysmtpserver":25

where the :25 is the port of the SMTP server, anyone had luck using this with GMail?

henkoegema
Posts: 13
Joined: Thu Oct 17, 2013 3:26 pm

Re: Send e-mails from the command line

Thu Oct 17, 2013 4:07 pm

MrOaiki wrote:I tried:

http://c-mobberley.com/wordpress/index. ... h-postfix/
http://iqjar.com/jar/sending-emails-fro ... pberry-pi/
http://www.simplehelp.net/2008/12/01/ho ... mand-line/
http://rpi.tnet.com/project/faqs/smtp (quite clear instructions, but didn't work)
http://blog.a-netz.de/2012/12/sending-m ... pberry-pi/ (also quite clear, but no luck)

However, since asking my question here, I finally found a way that worked flawlessly. A program called sendEmail. No configuration files, no nothing. It just worked, right from the terminal!

sendEmail -f "fromaddress" -t "toaddress" -u "Subjectlinei" -m "Mymessage" -s "mysmtpserver":26 -o tls=no -xu "loginusername" -xp "loginpassword"

Done!
I've been using sendEmail for years with Debian (6.0).
However with Raspbian-wheezy I can't get it working.

Code: Select all

pi@raspberrypi /usr/local/bin $ ./sendEmail -o tls=no -f henk@oegema.com -t henkoegema@gmail.com -u Asterisk bericht -m "Oproep van" -s smtp.gmail.com -xu myusername -xp mypassword 
Oct 17 18:05:05 raspberrypi sendEmail[18784]: NOTICE => Authentication not supported by the remote SMTP server!
Oct 17 18:05:05 raspberrypi sendEmail[18784]: ERROR => Received: 	530 5.7.0 Must issue a STARTTLS command first. j7sm194712008eeo.15 - gsmtp

dtannini
Posts: 1
Joined: Wed Nov 04, 2015 3:18 pm

Re: Send e-mails from the command line

Wed Nov 04, 2015 3:27 pm

Does anyone know how to break the email message up into separate lines witihin a php script?
Currently, I've tried below, and it does not work.

Code: Select all

// Send Email notification to let users know a new purchase request has been added to database
$alternate="user1@company.com";
$Email_Date=date('F d, Y');

// Credentials for notification email
$command = "sendEmail";
$from = "-f purchasing@company.com";
$to = $alternate;
$subject = "-u New Purchase Request Alert";

// These lines are the body of the email
$line1 = "A new purchase request has been generated by $Requestor on $Email_Date at $Req_Time.";
$line2 = "This is more information on Line 2.";
$line3 = "This is more information on Line 3.";
$body = $line1 . PHP_EOL . $line2 . PHP_EOL . $line3;

// Mail server and credentials
$server = "-s server.com";
$username = "-xu username@company.com";
$password = "-xp password";

// Concatenate all variables to create sendEmail executable command
$cmd = $command." ".$from." "."-t"." " .$to." ".$subject. " "."-m"." ".$body." ".$server." ".$username." ".$password;

// Execute sendEmail command
echo exec ($cmd);
When I don't try to break the lines (PHP_EOL, or other methods like /r/n, /n) it works fine.
What is the correct code here?

User avatar
DougieLawson
Posts: 38883
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: Send e-mails from the command line

Thu Nov 05, 2015 4:53 pm

Stick "\r\n" on the end of each line.
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

Return to “Networking and servers”