[CentOS] Sending mail from CLI to another SMTP host

Tue Apr 6 06:18:45 UTC 2010
Agile Aspect <agile.aspect at gmail.com>

On Wed, Mar 31, 2010 at 9:04 PM, Fajar Priyanto <fajarpri at arinet.org> wrote:
> Hi all,
> Is it possible to send mail from CLI (bash, python) without any LOCAL
> SMTP installed, using SMTP on another machine.
> Care to give a glimpse of the code?
> Thank you.
> _______________________________________________
> CentOS mailing list
> CentOS at centos.org
> http://lists.centos.org/mailman/listinfo/centos

The enclosed script works for python 2.5 (it's an example.)

If you don't use the localhost sendmail, then you'll have to relay
through another sendmail server - and good luck with that.

Maybe you can relay through a Google?


#!/usr/bin/env python

import smtplib

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

fromaddr = prompt("From: ")
toaddrs  = prompt("To: ").split()
print "Enter message, end with ^D (Unix) or ^Z (Windows):"

# Add the From: and To: headers at the start!
msg = ("From: %s\r\nTo: %s\r\n\r\n"
       % (fromaddr, ", ".join(toaddrs)))
while 1:
        line = raw_input()
    except EOFError:
    if not line:
    msg = msg + line

print "Message length is " + repr(len(msg))

server = smtplib.SMTP('localhost')
server.sendmail(fromaddr, toaddrs, msg)

      Enjoy global warming while it lasts.