unicode - postfix sends mail from wrong domain / Python 3 code for sending utf8 mail

12
2013-08
  • daveagp

    I have a machine with two domain names, say a.com and b.com.

    When I use postfix to send something from an address like [email protected], it replaces it with [email protected] every time. My main.cf file specifies only a.com so I don't know where the b.com is coming from.

    Here's the relevant part of maillog... the first line is the intiation of the message and then the 6th line is the one where [email protected] mysteriously pops up.

    mymachine sendmail[28543]: qA3NGbOR028543: from=[email protected], size=792, class=0, nrcpts=1, msgid=<[email protected]>, relay=apache@localhost
    mymachine postfix/smtpd[28546]: connect from b.com[127.0.0.1]
    mymachine postfix/smtpd[28546]: 912F9758031: client=b.com[127.0.0.1]
    mymachine postfix/cleanup[28549]: 912F9758031: message-id=<[email protected]>
    mymachine sendmail[28543]: qA3NGbOR028543: to=[email protected], delay=00:00:00, xdelay=00:00:00, mailer=relay, pri=30792, relay=[127.0.0.1] [127.0.0.1], dsn=2.0.0, stat=Sent (Ok: queued as 912F9758031)
    mymachine postfix/qmgr[28527]: 912F9758031: from=<[email protected]>, size=1130, nrcpt=1 (queue active)
    

    I'm not sure if it's relevant how I called this, but it's from some PHP mail() and I've ensured the -f flag is passed along. If any more info might help I'd be happy to deliver it!

  • Answers
  • daveagp

    Following

    http://mail.python.org/pipermail/mailman-developers/2001-March/008476.html

    I found it was easier just to uninstall sendmail, install postfix, and use a Python script to send the mail. Here is the Python 3 code which I used to send the message (it needed to be internationalized hence the UTF-8 encodings).

    import email.message, email.mime.text, smtplib, sys
    from email.utils import parseaddr, formataddr
    from email.header import Header
    from email.charset import Charset
    
    def format_address(name, email):
        if not name:
            return email
        name = Charset('iso-8859-1').header_encode(name)
        return formataddr((name, email))
    
    def send_unicode_email(mFrom, mTo, mSubject, mBody):
        sender_name, sender_addr = parseaddr(mFrom)
        recipient_name, recipient_addr = parseaddr(mTo)
    
        composed = email.mime.text.MIMEText(mBody.encode('UTF-8'), _charset='UTF-8')
        composed['from'] = format_address(sender_name, sender_addr)
        composed['to'] = format_address(recipient_name, recipient_addr)
        composed['subject'] = Header(mSubject, 'UTF-8')
    
        srv = smtplib.SMTP('localhost')
        srv.send_message(composed)
        srv.quit()
    

  • Related Question

    postfix - Easiest way to send mail from Linux Server
  • QAH

    I want my server to send me email alerts every time it does things such as run a backup. I have tried to setup programs like Sendmail, Postfix, etc on my Ubuntu Server box and it is really a pain for me to get it working. Is there any online service or some easy gateway where my server can send email alerts? If not, is there any easy scripts to get a Linux mail program up and running?

    Thanks


  • Related Answers
  • 8088

    If you're having trouble configuring the standard mail programs, you can use a command-line SMTP client such as sendEmail. Along with the address, message, and other data, simply specify an SMTP server. You can use GMail along with a username and password for example.

    Here are the available command-line switches:

    alt text

  • oraz
    echo "Your message"|mail -s "subject of the message" [email protected]