Posts Tagged ‘CLI’

How to send mail in command line interface

Hi again.
In the linux CLI you can easily send mail your files or texts you want. just you can use the nail command.

nail -r “” -s “Some subject” -S smtp=some.smtp.server < msg.txt

also you can permanently set the SMTP server in your ~/.mailrc file (or /etc/nail.rc if you want to set it system-wide), which removes the need for using the “-S smtp=…” option on the command-line:

set smtp=some.smtp.server

the second way you can use the following script i download from internet :

#!/usr/bin/env python

# Usage: ./

import sys
import smtplib

def prompt(prompt):
return raw_input(prompt).strip()

def create_message():
fromaddr = prompt(‘From: ‘)
toaddrs = prompt(‘To: ‘).split()
subject = prompt(‘Subject: ‘)
print ‘Enter message, end with ^D (Unix) or ^Z (Windows): ‘
msg = (‘From: %s\r\nTo: %s\r\nSubject: %s\r\n\n’ \
% (fromaddr, ‘, ‘.join(toaddrs), subject))
while 1:
line = sys.stdin.readline()
except EOFError:
if not line:
msg = msg + line
return fromaddr, toaddrs, msg

def send_mail(fromaddr, toaddrs, msg):
server = smtplib.SMTP(‘localhost’)
server.sendmail(fromaddr, toaddrs, msg)

def main():
[fromaddr, toaddrs, msg] = create_message()
send_mail(fromaddr, toaddrs, msg)

if __name__ == ‘__main__’:


     Copyrighted Mehdi Behamin , All Rights Reserved
Blog has been visited 204258 times