Trying to utilize mailx to send mail via apython script

Discussion in 'Linux Networking' started by Ferrous Cranus, Sep 5, 2013.

  1. i as root just installed

    sendmail and mailx. i have edited the ~/.mailrc to use:


    [~/www/cgi-bin]# cat ~/.mailrc
    account gmail {
    set smtp-use-starttls
    set smtp=smtp://smtp.gmail.com:587
    set smtp-auth=login
    set smtp-auth-user=may)gmail
    set smtp-auth-password=my_gmail_pass_not_stupid_enough_to_wite _it_again
    }e



    And now i'm trying to:


    cmd = "echo %s | mailx -A gmail -r %s -s %s %s" % (MESSAGE, FROM,
    SUBJECT, TO)
    p=subprocess.Popen( cmd, shell=True, stdout=subprocess.PIPE )
    output, errors = p.communicate()
    print( errors, output )


    Any ideas please why this fails to work?
    while i remove the '-A gmail' string in the cmd line then i can send
    fast mail but only to mail containing the @superhost.gr trail.

    i need to be eble to send to external mails to.
     
    Ferrous Cranus, Sep 5, 2013
    #1
    1. Advertisements

  2. Ferrous Cranus

    Jorgen Grahn Guest

    Where did you learn about this .mailrc syntax? It doesn't match the
    one documented in mailx(1) on my machines. (That's BSD mailx; there
    seems to be two different flavors, but I don't think they are /that/
    radically different.)

    There are two ways to send mail on Unix:

    1 Enqueue it to the local MTA using /usr/lib/sendmail. The local MTA
    then becomes responsible for passing it on, if needed. This is the
    traditional approach used by at, cron, mail, mailx, mutt ...

    2 Send it via SMTP to some external mail relay. Local mail won't
    work, every mail client must be configured, and temporary delivery
    problems will be fatal.

    You seem to have the software for (1), but the configuration for (2).

    ....
    ....

    Make it work from the command line before attempting to automate it.

    (You may also want to avoid invoking the shell. Think about what
    might happen if MESSAGE=="; rm -rf $HOME ;".)

    /Jorgen
     
    Jorgen Grahn, Sep 5, 2013
    #2
    1. Advertisements

  3. Στις 5/9/2013 9:13 μμ, ο/η Jorgen Grahn έγÏαψε:

    Hello,

    Even so, even trying it from the shell that is, 'mailx' fails to
    delivers my test mails.

    Can one helo me congigure sendmail properly?


    mailX == an MUA == a Mail Client right?
    sendmail == MTA == program that is responsilble to send mails?

    So, i gather 'mailx' does use 'sendmail' to send mail yes?

    can you provide me a working mail exmaple that works?

    Why i can only send mail to local server addresses an not to an external
    mail address?
     
    Ferrous Cranus, Sep 6, 2013
    #3
  4. Ferrous Cranus

    Todd Guest

    Hi Ferrous,

    Your .mailrc syntax is too weird. I place all my parameters
    inside my script.

    gMail is a pain in the ... to use with mailx. Here is a test
    script I wrote that should help you. Change the "Your_email_address",
    "Your_other_email_address", and "Your_email_password"
    appropriately

    Create a separate mailcert directory, copy your mailcerts
    $ ls
    cert7.db cert8.db key3.db secmod.db
    from your Firefox directory to it, and point your "nss-config-dir"
    at it.

    Note that some of my lines scrolled when I pasted.

    Good luck,
    -T


    $ cat MailxTest.gmail
    #!/bin/bash

    echo "mailx test" | mailx -v -s `dnsdomainname`" gmail smtp test subject" \
    -S smtp-use-starttls \
    -S ssl-verify=ignore \
    -S smtp-auth=login \
    -S smtp=smtp://smtp.gmail.com:587 \
    -S from= \
    -S smtp-auth-user= \
    -S smtp-auth-password="Your_email_password" \
    -S nss-config-dir=/home/linuxutil/mailcerts/ \
     
    Todd, Sep 6, 2013
    #4
  5. Στις 6/9/2013 10:22 πμ, ο/η Todd έγÏαψε:
    [~/www/cgi-bin]# cat ~/.mailrc
    account gmail {
    -S smtp-use-starttls \
    -S ssl-verify=ignore \
    -S smtp-auth=login \
    -S smtp=smtp://smtp.gmail.com:587 \
    -S from= \
    -S smtp-auth-user= \
    -S smtp-auth-password="my_gmail_pass" \
    -S nss-config-dir=/home/nikos/certs/ \

    }

    ==============================

    cmd = "echo %s | mailx -v gmail -r %s -s %s %s" % ( MESSAGE, FROM,
    SUBJECT, TO )
    p=subprocess.Popen( cmd, shell=True, stdout=subprocess.PIPE )
    output, errors = p.communicate()
    print( errors, output )


    ================================


    Output of my attempt to send mail:

    [~/www/cgi-bin]# python mail.py
    LOG: MAIN
    cwd=/home/nikos/public_html/cgi-bin 9 args: send-mail -i -v -r -s

    BD1ZJHE1340OB76AYIM81YP51VBYFCR5B2BCH08LKVT06T5I9E gmail

    LOG: MAIN
    <= U=nikos P=local S=1125
    Content-type: text/html; charset=utf-8

    None b''
    <h2><font color=blue>ΕυχαÏιστώ Ï€Î¿Î»Ï Î³Î¹Î± το ενδιαφέÏον! Θα επικοινωνήσω
    μαζί σου άμεσα :)</font></h2>
    LOG: MAIN
    cwd=/var/spool/exim 4 args: /usr/sbin/exim -v -Mc 1VHr2Y-0004wE-RL
    delivering 1VHr2Y-0004wE-RL
    [~/www/cgi-bin]# LOG: MAIN
    SMTP connection outbound 1378454714 1VHr2Y-0004wE-RL superhost.gr

    LOG: MAIN
    SMTP connection outbound 1378454714 1VHr2Y-0004wE-RL superhost.gr

    LOG: MAIN
    remote host address is the local host: secure.superhost.gr
    LOG: MAIN
    == R=lookuphost defer (-1): remote host
    address is the local host
    LOG: MAIN
    SMTP connection outbound 1378454715 1VHr2Y-0004wE-RL superhost.gr

    LOG: MAIN
    remote host address is the local host: secure.superhost.gr
    LOG: MAIN
    ==

    <>
    R=lookuphost defer (-1): remote host address is the local host
    LOG: MAIN
    SMTP connection outbound 1378454715 1VHr2Y-0004wE-RL superhost.gr

    LOG: MAIN
    ** <>
    R=fail_remote_domains: The mail server could not deliver mail to
    . The account or domain may not exist, they
    may be blacklisted, or missing the proper dns entries.
    Connecting to gmail-smtp-in.l.google.com [173.194.70.26]:25 ... connected
    SMTP<< 220 mx.google.com ESMTP w44si1186161eef.195 - gsmtp
    SMTP>> EHLO secure.superhost.gr
    SMTP<< 250-mx.google.com at your service, [84.200.17.58]
    250-SIZE 35882577
    250-8BITMIME
    250-STARTTLS
    250-ENHANCEDSTATUSCODES
    250 CHUNKING
    SMTP>> STARTTLS
    SMTP<< 220 2.0.0 Ready to start TLS
    SMTP>> EHLO secure.superhost.gr
    SMTP<< 250-mx.google.com at your service, [84.200.17.58]
    250-SIZE 35882577
    250-8BITMIME
    250-ENHANCEDSTATUSCODES
    250 CHUNKING
    SMTP>> MAIL FROM:<> SIZE=2165
    SMTP<< 250 2.1.0 OK w44si1186161eef.195 - gsmtp
    SMTP>> RCPT TO:<>
    SMTP<< 250 2.1.5 OK w44si1186161eef.195 - gsmtp
    SMTP>> DATA
    SMTP<< 354 Go ahead w44si1186161eef.195 - gsmtp
    SMTP>> writing message and terminating "."
    SMTP<< 250 2.0.0 OK 1378454716 w44si1186161eef.195 - gsmtp
    SMTP>> QUIT
    LOG: MAIN
    => R=lookuphost T=remote_smtp
    H=gmail-smtp-in.l.google.com [173.194.70.26] X=TLSv1:RC4-SHA:128
    LOG: MAIN
    **
    :
    retry timeout exceeded
    LOG: MAIN
    ** : retry timeout exceeded
    LOG: MAIN
    cwd=/var/spool/exim 8 args: /usr/sbin/exim -v -t -oem -oi -f <>
    -E1VHr2Y-0004wE-RL
    LOG: MAIN
    <= <> R=1VHr2Y-0004wE-RL U=mailnull P=local S=2374 T="Mail delivery
    failed: returning message to sender"
    LOG: MAIN
    Completed
    LOG: MAIN
    cwd=/var/spool/exim 4 args: /usr/sbin/exim -v -Mc 1VHr2a-0004wK-G7
    delivering 1VHr2a-0004wK-G7
    LOG: MAIN
    remote host address is the local host: secure.superhost.gr
    LOG: MAIN
    == R=dkim_lookuphost defer (-1): remote host
    address is the local host
    LOG: MAIN
    ** : retry timeout exceeded
    LOG: MAIN
    : error ignored
    LOG: MAIN
    Completed



    I have pasted the .mailrc, the snippet of mail.py and the output

    Can you help based on that info please?
     
    Ferrous Cranus, Sep 6, 2013
    #5
  6. Ferrous Cranus

    Jorgen Grahn Guest

    Yes, that's exactly my point. It's a general problem solving trick:
    if you can split your problem in two parts, try to solve one at a
    time.
    Yes, the version of mailx I am used to[1] does that ... but apparently
    you and the other posters are using some /other/ tool with the same
    name.
    Sorry; no. I use neither sendmail nor gmail.
    Don't know, but remember that local mail is a lot easier. As long as
    you have an MTA (e.g. sendmail) running and its config is not horribly
    broken, local->local will work.

    /Jorgen

    [1] Mailx is /old/. I used it on SunOS 20 years ago, and it wasn't
    new then.
     
    Jorgen Grahn, Sep 6, 2013
    #6
  7. Στις 6/9/2013 11:50 πμ, ο/η Jorgen Grahn έγÏαψε:

    I have managed already to make it work local=>local

    But then how about local => internet

    what configurations should i do? try to run what condig?

    Is there an alternative that will help me send mails more easily?
     
    Ferrous Cranus, Sep 6, 2013
    #7
  8. Ferrous Cranus

    David Brown Guest

    The obvious question here is why are you trying to use sendmail and
    mailx? You are writing a script in Python, and you want to send mail
    via an SMTP server. Just use Python's SMTP module.
     
    David Brown, Sep 6, 2013
    #8
  9. Στις 6/9/2013 12:10 μμ, ο/η David Brown έγÏαψε:
    Liek this you mean:

    # open Gmail's SMTP server
    #server = smtplib.SMTP('smtp.gmail.com:587')
    #server.ehlo()
    #server.starttls()

    # next, log in to the server
    #server.login("", "my_pass")


    Because GMail tend to add a Sender field when receiving my mail
    supresing the real FROM field used in the webform to my Gmail's auth
    account.

    I want to avoid that by using my own mail server so my mails appear for
    the correct client mail and not all of them from as coming from my GMail
    account/
     
    Ferrous Cranus, Sep 6, 2013
    #9
  10. If you are only going it that direction "local => internet" and
    "internet" is gmail a simple solution is sSMTP.

    https://wiki.debian.org/sSMTP
    https://wiki.archlinux.org/index.php/SSMTP

    I use it for remote servers to send error messages and info via scripts
    to an "admin" gmail account.

    Very simple to setup and works with gmail. Works for me to keep tabs on
    various servers I maintain.
     
    Jonathan N. Little, Sep 6, 2013
    #10
  11. Στις 6/9/2013 9:10 μμ, ο/η Jonathan N. Little έγÏαψε:
    Can i use my own FROM field with sSMTP?
    But connecting to google's smtp server will still add a Sender header.
     
    Ferrous Cranus, Sep 7, 2013
    #11

  12. Yes. I even add "X-*" headers for sorting done within my scripts, Perl
    and shell scripts so would be no problem with Python.

    Not for the from From field, but the hostname is recorded in Received
    field...but maybe I am misunderstanding your question.
     
    Jonathan N. Little, Sep 7, 2013
    #12
  13. Στις 7/9/2013 3:33 μμ, ο/η Jonathan N. Little έγÏαψε:
    is there a way to send mail from the mail address i specify and without
    the hostname of my server being revealed?
     
    Ferrous Cranus, Sep 7, 2013
    #13
  14. Ferrous Cranus

    Todd Guest


    Don't use mailrc until you get it working from a batch file.
     
    Todd, Sep 7, 2013
    #14
  15. Ferrous Cranus

    Todd Guest

    replying over on comp.os.linux.networking
     
    Todd, Sep 7, 2013
    #15
  16. Ferrous Cranus

    Todd Guest

    Certainly! gmail and zmail (zoho.com) are both huge
    pains in the --- to get working from a script.

    Hi Îίκος,

    The batch script is how to send "From:" a gmail
    account with a batch script using mailx. The
    "To:" can be anyone.

    I also have a script to do the same thing from a
    zoho account (zmail), if anyone wants it.

    First let me write the comments in my batch file a little better.
    Remember, this BASH script is a only for sending from a script
    through gMail.

    -------- cat MailxTest.gmail -----------
    #!/bin/bash
    #
    echo "mailx test" | mailx -v -s `dnsdomainname`" gmail smtp test subject" \
    -S smtp-use-starttls \
    -S ssl-verify=ignore \
    -S smtp-auth=login \
    -S smtp=smtp://smtp.gmail.com:587 \
    -S from= \
    -S smtp-auth-user= \
    -S smtp-auth-password="From_email_password" \
    -S nss-config-dir=/home/linuxutil/mailcerts/ \


    ---------------------------------------------------
    You will find them in your gMail profile. Your profile
    is in your home directory under
    $HOME/.mozilla/firefox/your_profiles_cypher_name

    1) install firefox

    2) start firefox and let it automatically create its first
    profile

    3) open a terminal/xterm in your home directory

    4) search for the certificates with
    find $HOME/.mozilla/firefox -iname \*cert\*.db
    remember to copy all the *.db's over from your profile
    to a separate directory. Mine are:

    cert7.db cert8.db key3.db secmod.db

    Firefox needs these certificates to do the "s" in https

    5) modify "nss-config-dir=/home/linuxutil/mailcerts/" in
    the above script to match the directory you copied the
    certificates to

    gMail uses SSL (secure socket layer or the "s" in https) and
    need these certificates to authenticate. So does zmail, but
    uses a different sign in method.
    This is a Bash script. The first line in the script "#!/bin/bash"
    tells you this.
    No. Not to a Python script. Get it working in Bash first,
    them translate it to Python. YOUR PYTHON SUBROUTINE WILL NEED
    THESE CERTIFICATES TOO! If you can get it working in BASH,
    you are 90% of the way there.
    My bad. I should have said it was the gMail "To"
    address, which can be anyone.

    Good luck, I am dying to know if this helped you or
    anyone else. I am usually the one asking the questions.

    -T
     
    Todd, Sep 8, 2013
    #16
  17. Στις 8/9/2013 2:20 πμ, ο/η Todd έγÏαψε:
    Hello Todd!

    Let me tell you what i did:

    i have:

    rm ~./mailrc

    [~]# cat MailXTest.gmail
    #!/bin/bash
    #
    echo "mailx test" | mailx -v -s `dnsdomainname`" gmail smtp test subject" \
    -S smtp-use-starttls \
    -S ssl-verify=ignore \
    -S smtp-auth=login \
    -S smtp=smtp://smtp.gmail.com:587 \
    -S from= \
    -S smtp-auth-user= \
    -S smtp-auth-password="my_gmail_pass" \
    -S nss-config-dir=/home/nikos/certs/ \


    chmod u+x MailXTest.gmail

    and finally when i try to run it:

    [~]# MailXTest.gmail
    -bash: MailXTest.gmail: command not found

    What iam doing?

    Apart from that i want to avoid using GMail's service becazue it adds up
    a Sender: header sayign that the mail originating from my gmail accoutn
    while i want to use the FROM field that the visitor used in my html web
    form to fill in his email

    Also it would be nice if the hostname would appear too.

    Do we need to setup our own mail servr to achieve this?
     
    Ferrous Cranus, Sep 8, 2013
    #17
  18. Στις 7/9/2013 11:54 μμ, ο/η Jonathan N. Little έγÏαψε:
    I want to avoid using GMail's service because it adds up a Sender:
    header saying that the mail originating from my gmail accoutn while i
    want to use the FROM field that the visitor used in my html web form to
    fill in his email

    Also it would be nice if the hostname would appear too. Pehars by using
    a proxy.
    Can ssmtp handle this?

    I cannot even download ssmtp when i 'yum install ssmtp'

    where can i find it as a package and can you please provide me a
    ssmtp.conf file that does the above( set its own FROM field and alter
    hostname)

    Thank you.
     
    Ferrous Cranus, Sep 8, 2013
    #18
  19. Στις 8/9/2013 9:58 πμ, ο/η Ferrous Cranus έγÏαψε:
    I cannot evn decide if i need an external mail server or setup my own
    mail server.
     
    Ferrous Cranus, Sep 8, 2013
    #19
  20. What distro are you using? Ubuntu/Debian has it in the standard repos
    and one of the links on configuration was for Arch
    The ssmtp.conf should be in /etc/ssmtp/ssmtp.conf if you installed the
    package.
     
    Jonathan N. Little, Sep 8, 2013
    #20
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.