[CentOS] Sending mail from CLI to another SMTP host

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


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:
    try:
        line = raw_input()
    except EOFError:
        break
    if not line:
        break
    msg = msg + line

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

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


-- 
      Enjoy global warming while it lasts.



More information about the CentOS mailing list