Thread: SMTP Help

    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    25
    Rep Power
    0

    SMTP Help


    So, I was trying to make a script that sends e-mails every once in a while, but I have a problem, this program is written to send via the gmail server, but I don't think that would really work because of their rules and stuff.
    This is what I have for gmail:
    python Code:
    import smtplib
    import time
    import sys
     
    sent = 0
     
    def send_mail(gmail, gmail_pwd, to, subj, body):
        message = ("From: %s\nTo: %s\nSubject: %s\n\n%s" %(gmail, to, subj, body))
        try:
            s = smtplib.SMTP('smtp.gmail.com', 587)
        except IOError, e:
            print 'Error: %s' %(e)
            time.sleep(2)
            sys.exit()
        s.ehlo()
        s.starttls()
        s.ehlo
        try:
            s.login(gmail, gmail_pwd)
        except BaseException, e:
            print 'Error: %s' % (e)
            time.sleep(3)
            sys.exit(1)
        s.sendmail(gmail, [to], message)
        s.quit()
     
    def main():
        print "Message builder:\n "
        sender = raw_input('\tFrom: ')
        sender_pwd = raw_input('\tYour password: ')
        receiver = raw_input('\tTo: ')
        subject = raw_input('\tSubject: ')
        msg = raw_input('\t\t> ')
        while True:
            print "\n\nIf you want to stop sending e-mails, press CTRL-C."
            print "Sending... Please wait..."
            send_mail(sender, sender_pwd, receiver, subject, msg)
            print "\nSent."
            sent += 1
            print "%d e-mails sent so far." % sent
            time.sleep(900)
     
    if __name__ == "__main__": main()

    I know that in order to send the actual e-mail via localhost, I am supposed to change
    Code:
    s = smtplib.SMTP('smtp.gmail.com', 587)
    to
    Code:
    s = smtplib.SMTP('localhost')
    Anyhow, my question is, what do I do about the e-mail part in this case, do I even need to specify a sender? If yes, what should it be, if not, what should I do instead?
  2. #2
  3. No Profile Picture
    Lost in code
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 2004
    Posts
    8,317
    Rep Power
    7170
    If your script is low-volume you can send it through gmail without any problems.

    You should specifier a sender address. But depending on how your local mail server is configured you may or may not need to actually authenticate.
    PHP FAQ

    Originally Posted by Spad
    Ah USB, the only rectangular connector where you have to make 3 attempts before you get it the right way around
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    25
    Rep Power
    0
    Thanks for the quick answer.
    I decided to go with gmail, since the SMTP server I installed didn't work well, plus it's for a friend, can't go through the trouble of installing it all over again

IMN logo majestic logo threadwatch logo seochat tools logo