WebFaction
Community site: login faq
0
1

Hi,

How can I send e-mail using Python.

this is my code:

import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText

s = smtplib.SMTP('smtp.webfaction.com')
s.sendmail(from_email, to_email, msg.as_string())

I always get this error:

File "/usr/local/lib/python2.7/smtplib.py", line 712, in sendmail\n    raise SMTPRecipientsRefused(senderrs)\n']

asked 09 May '12, 09:21

xpanta
622744
accept rate: 0%

edited 09 May '12, 09:22


You need to login to the smtp server in order to send mail with a script, try using this example:

from smtplib import SMTP

from_addr = 'my_email_address@mydomain.tld'
to_addrs = ['email@domain.com']
msg = open('email_msg.txt','r').read()

s = SMTP()
s.connect('smtp.webfaction.com')
s.login('my_mailbox_name','password1')
s.sendmail(from_addr, to_addrs, msg)
s.quit()
permanent link

answered 09 May '12, 09:48

timg ♦♦
1.3k4
accept rate: 30%

Thanks. It seems to work. Need to check with my mail accounts if mails were received. Before that, I need to ask something more. Can I send multiple e-mails and quit() once in the end? Or do I need to login/quit everytime I need to send an email. Answer seems obvious, I just need to clarify.

(09 May '12, 09:55) xpanta

You should be able to send multiple emails and only need to quit once.

(09 May '12, 10:00) timg ♦♦
Your answer
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Question tags:

×337
×261

question asked: 09 May '12, 09:21

question was seen: 5,259 times

last updated: 09 May '12, 10:00

WEBFACTION
REACH US
SUPPORT
AFFILIATE PROGRAM
LEGAL
© COPYRIGHT 2003-2019 SWARMA LIMITED - WEBFACTION IS A SERVICE OF SWARMA LIMITED
REGISTERED IN ENGLAND AND WALES 5729350 - VAT REGISTRATION NUMBER 877397162
5TH FLOOR, THE OLD VINYL FACTORY, HAYES, UB3 1HA, UNITED KINGDOM