Python Spam Email Sending Errors - python

import smtplib
fromaddr = "Insert your email here"
toaddr = ["insert receivers adress here"]
message = """From: fromname <from#fromdomain.com>
To: To Person <to#todomain.com>
Subject: Insert Subject here"""
emails_done = 1
server = smtplib.SMTP('smtp.gmail.com', 587)
server.starttls()
server.login(fromaddr, "InsertPassword")
for i in range(int(raw_input('how many emails?'))):
server.sendmail(fromaddr, toaddr,message)
print(emails_done)
emails_done = emails_done + 1
server.quit()
Im trying to spam my friend with 100 emails as a prank, but i have a few problems with the code, i get alot of different errors such as:
Traceback (most recent call last):
File "/Volumes/Data/Users/106299/Desktop/EMAIL .py", line 45, in <module>
server.sendmail(fromaddr, toaddr,message)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/smtplib.py", line 723, in sendmail
self.rset()
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/smtplib.py", line 462, in rset
return self.docmd("rset")
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/smtplib.py", line 387, in docmd
return self.getreply()
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/smtplib.py", line 360, in getreply
+ str(e))
SMTPServerDisconnected: Connection unexpectedly closed: [Errno 54] Connection reset by peer
Ive Gotten up to 78 emails and it always ends there. Also sometimes it sends in one big email thread/change, rather then 100 separate emails.Here is another
Traceback (most recent call last):
File "/Volumes/Data/Users/106299/Desktop/EMAIL .py", line 45, in <module>
server.sendmail(fromaddr, toaddr,message)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/smtplib.py", line 723, in sendmail
self.rset()
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/smtplib.py", line 462, in rset
return self.docmd("rset")
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/smtplib.py", line 387, in docmd
return self.getreply()
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/smtplib.py", line 363, in getreply
raise SMTPServerDisconnected("Connection unexpectedly closed")
SMTPServerDisconnected: Connection unexpectedly closed
I am running python 2.7.8 on a MacBook Air
Any help would be appreciated :>
P.S sorry for crap formatting, first post.

A possibility is that gmail is rejecting requests after a certain number per second.
Try putting server.set_debuglevel(1) right after server.starttls(), this will let you see the debug details of the function call. This information will likely lead you to the solution.

Related

Send email from server python

I am trying to send an email form my server with python, I asked my server provider what port to use for this and they said "choose SMTP Ports 465 (Secure SSL/TLS outgoing server) , 25 ( Non-SSL outgoing server)." Not sure what this exactly means but currently I am using 25, here is my code
#! /usr/bin/python
import smtplib
import smtplib
server = smtplib.SMTP('smtp.gmail.com', 25)
#Next, log in to the server
server.login("youremailusername", "password")
#Send the mail
msg = "\nHello!" # The /n separates the message from the headers
server.sendmail("you#gmail.com", "target#example.com", msg)
I filled in my username (which is my email address right) and password,a dn the target but it is not working, when I try to navigate to the url where my py script is, it just doesn't load. I have an internet connection cause I am loving other things, and go to other pages on my server. I have also tried running with cron jobs but that also doesn't work.
The permissions on the script are 0755, is there a problem with the script?
When i ran with cron jobs here is the traceback
Traceback (most recent call last):
File "/home/spencerf/public_html/cgi-bin/send_email.py", line 6, in <module>
server = smtplib.SMTP('smtp.gmail.com', 25)
File "/usr/lib64/python2.6/smtplib.py", line 239, in __init__
(code, msg) = self.connect(host, port)
File "/usr/lib64/python2.6/smtplib.py", line 295, in connect
self.sock = self._get_socket(host, port, self.timeout)
File "/usr/lib64/python2.6/smtplib.py", line 273, in _get_socket
return socket.create_connection((port, host), timeout)
File "/usr/lib64/python2.6/socket.py", line 567, in create_connection
raise error, msg
socket.error: [Errno 101] Network is unreachable
Thanks for the help in advance.
EDIT
here is updated error log with the port at 587
Traceback (most recent call last):
File "/home/spencerf/public_html/cgi-bin/send_email.py", line 7, in <module>
server = smtplib.SMTP('smtp.gmail.com', 587)
File "/usr/lib64/python2.6/smtplib.py", line 239, in __init__
(code, msg) = self.connect(host, port)
File "/usr/lib64/python2.6/smtplib.py", line 295, in connect
self.sock = self._get_socket(host, port, self.timeout)
File "/usr/lib64/python2.6/smtplib.py", line 273, in _get_socket
return socket.create_connection((port, host), timeout)
File "/usr/lib64/python2.6/socket.py", line 567, in create_connection
raise error, msg
socket.error: [Errno 101] Network is unreachable
EDIT 2
When I had server = smtplib.SMTP('telnet smtp.gmail.com', 587)
I got this error
Traceback (most recent call last):
File "/home/spencerf/public_html/cgi-bin/send_email.py", line 8, in <module>
server = smtplib.SMTP('telnet smtp.gmail.com', 587)
File "/usr/lib64/python2.6/smtplib.py", line 239, in __init__
(code, msg) = self.connect(host, port)
File "/usr/lib64/python2.6/smtplib.py", line 295, in connect
self.sock = self._get_socket(host, port, self.timeout)
File "/usr/lib64/python2.6/smtplib.py", line 273, in _get_socket
return socket.create_connection((port, host), timeout)
File "/usr/lib64/python2.6/socket.py", line 553, in create_connection
for res in getaddrinfo(host, port, 0, SOCK_STREAM):
socket.gaierror: [Errno -2] Name or service not known
BUG:
First of all you are using gmail server not your company server to send mail .For gmail server the output port is 587
The code:
Due to security issues gmail blocks accessing mail via code or program
But still you can use gmail to send mail via code if you do the following things
What i have done in code :
1.Added a error object to get the error message
import smtplib
try:
server = smtplib.SMTP('smtp.gmail.com', 587)
#Next, log in to the server
server.login("youremailusername", "password")
#Send the mail
msg = "\nHello!" # The /n separates the message from the headers
server.sendmail("you#gmail.com", "target#example.com", msg)
print "Successfully sent email"
except smtplib.SMTPException,error:
print str(error)
print "Error: unable to send email"
If u ran this code u would see a error message like this stating that google is not allowing u to login via code
Things to change in gmail:
1.Login to gmail
2.Go to this link https://www.google.com/settings/security/lesssecureapps
3.Click enable then retry the code
Hopes it help :)
But there are security threats if u enable it
Updated
import smtplib
try:
content = 'test'
mail = smtplib.SMTP('smtp.gmail.com',587)
mail.ehlo()
mail.starttls()
mail.login("ABC#gmail.com", "password")
mail.sendmail("ABC#gmail.com", "recivermailaddress", content)
mail.quit
print "Successfully sent email"
except smtplib.SMTPException,error:
print str(error)
In python, please download yagmail (disclaimer: I'm the developer):
pip install yagmail
It is then simply a matter of:
import yagmail
yag = yagmail.SMTP("you#gmail.com", "password")
yag.send("target#example.com", 'This is the subject', 'Hello!')
I guess it mostly just has to do with the fact that you gave a wrong port (smtp.gmail.com is not at 25).
On the github you can also see a common list of errors.

Python application using office365 exchange server timing out

I have a Python program that collects about 11K entries from a CORBA database, then uses those entries to parse through about 1,0000 Mb of text files. This entire process normally takes 5-7 minutes. I log into the Exchange365 server before this starts (so that any unusual events can be emailed to the administrator) and then try to email the results to the 'normal' recipients. That last email is failing and the root cause seems to be that I am getting logged off of the Exchange server.
Adding a 'dummy' email about half way through the processing seems to eliminate the timeout, but I'd like to find a more programatic way to accomplish this. Following is a code snippet where I enable the email:
import smtp
import time
pretty_localtime = time.asctime()
smtpserver = smtplib.SMTP("smtp.office365.com",587)
smtpserver.ehlo()
smtpserver.starttls()
smtpserver.ehlo()
#
try:
smtpserver.login(office365_user,office365_pwd)
except:
msg = ("%s --- FATAL ERROR: Couldn't log into SMTP server.")%pretty_localtime
log_file.write("%s \n"%msg)
print msg
sys.exit()
Has anyone worked through this problem?
***edit** Python error added:
Traceback (most recent call last):
File "piu_billing.py", line 739, in <module>
log_file.write(email_msg)
File "piu_billing.py", line 102, in send_mail
smtpserver.sendmail(office365_user,to,themsg)
File "C:\Python27\lib\smtplib.py", line 719, in sendmail
(code, resp) = self.mail(from_addr, esmtp_opts)
File "C:\Python27\lib\smtplib.py", line 471, in mail
self.putcmd("mail", "FROM:%s%s" % (quoteaddr(sender), optionlist))
File "C:\Python27\lib\smtplib.py", line 334, in putcmd
self.send(str)
File "C:\Python27\lib\smtplib.py", line 324, in send
raise SMTPServerDisconnected('Server not connected')
smtplib.SMTPServerDisconnected: Server not connected

Using pyramid_mailer results in ValueError: TPC in progress

I'm running pyramid on an Ubuntu Linux server, and am getting a ValueError when trying to use pyramid_mailer. My code is relatively simple, anything seems to cause it:
def my_view(request):
mailer = get_mailer(request)
emailMessage = Message(subject="Welcome", sender="noreply#mysite.com", recipients = ["me#email.com"], body="test")
mailer.send(emailMessage)
Results in this error:
Traceback (most recent call last):
File "/usr/share/nginx/wwwProj/local/lib/python2.7/site-packages/pyramid-1.5-py2.7.egg/pyramid/router.py", line 242, in __call__
response = self.invoke_subrequest(request, use_tweens=True)
File "/usr/share/nginx/wwwProj/local/lib/python2.7/site-packages/pyramid-1.5-py2.7.egg/pyramid/router.py", line 217, in invoke_subrequest
response = handle_request(request)
File "/usr/share/nginx/wwwProj/local/lib/python2.7/site-packages/pyramid_debugtoolbar-2.0.2-py2.7.egg/pyramid_debugtoolbar/toolbar.py", line 160, in toolbar_tween
return handler(request)
File "/usr/share/nginx/wwwProj/local/lib/python2.7/site-packages/pyramid-1.5-py2.7.egg/pyramid/tweens.py", line 21, in excview_tween
response = handler(request)
File "/usr/share/nginx/wwwProj/local/lib/python2.7/site-packages/pyramid_tm-0.7-py2.7.egg/pyramid_tm/__init__.py", line 79, in tm_tween
manager.abort()
File "/usr/share/nginx/wwwProj/local/lib/python2.7/site-packages/transaction-1.4.3-py2.7.egg/transaction/_manager.py", line 116, in abort
return self.get().abort()
File "/usr/share/nginx/wwwProj/local/lib/python2.7/site-packages/transaction-1.4.3-py2.7.egg/transaction/_transaction.py", line 468, in abort
reraise(t, v, tb)
File "/usr/share/nginx/wwwProj/local/lib/python2.7/site-packages/transaction-1.4.3-py2.7.egg/transaction/_transaction.py", line 453, in abort
rm.abort(self)
File "/usr/share/nginx/wwwProj/local/lib/python2.7/site-packages/repoze.sendmail-4.2-py2.7.egg/repoze/sendmail/delivery.py", line 119, in abort
raise ValueError("TPC in progress")
ValueError: TPC in progress
I followed the instructions for "Getting Started (The Easier Way)" on this site: http://pyramid-mailer.readthedocs.org/en/latest/
This is a known issue. It can be worked around in the meantime by reverting to repoze.sendmail 4.1 (from 4.2)
recipients = ["me#email.com"]
here you can see
recipients – list of email addresses
This was the first error that I encountered while trying to set up an emailing system, through I cannot remember what I did. In any case, I finally got it working for a gmail sender for SMTP. Hope this someone else in my position:
import smtplib
sender = "noreply"
to = "username"
subject = "Verification Code"
headers = "From: %s\r\nTo: %s\r\nSubject: %s\r\n\r\n" % (sender, to,
subject)
sEmailMessage = headers + "whatever message you want"
mailserver = smtplib.SMTP("smtp.gmail.com", 587)
#--- because of gmail
mailserver.ehlo()
mailserver.starttls()
mailserver.ehlo()
#---
mailserver.login("your_email_address#gmail.com", "your_password")
mailserver.sendmail("from_here#gmail.com", to_here#whatever.com, sEmailMessage)
mailserver.close()

Error during sending mail

I try to send mail via python. I got a function:
def mail():
fromaddr = 'mymail#gmail.com'
toaddrs = 'targetmail#o2.pl'
msg = 'There was a terrible error that occured and I wanted you to know!'
# Credentials (if needed)
username = 'mymail#gmail.com'
password = 'password'
# The actual mail send
server = smtplib.SMTP('smtp.gmail.com:587')
server.ehlo()
server.starttls()
server.ehlo()
server.login(username,password)
server.sendmail(fromaddr, toaddrs, msg)
server.quit()
When I try to execute this function it whows me WinError 10060. Error text below:
Traceback (most recent call last):
File "C:\python-factory\ServiceChecker\main\mainBlock.py", line 75, in <module> mail()
File "C:\python-factory\ServiceChecker\main\mainBlock.py", line 65, in mail server = smtplib.SMTP('smtp.gmail.com:587')
File "C:\Python34\Lib\smtplib.py", line 242, in __init__ (code, msg) = self.connect(host, port)
File "C:\Python34\Lib\smtplib.py", line 321, in connect self.sock = self._get_socket(host, port, self.timeout)
File "C:\Python34\Lib\smtplib.py", line 292, in _get_socket self.source_address)
File "C:\Python34\Lib\socket.py", line 509, in create_connection raise err
File "C:\Python34\Lib\socket.py", line 500, in create_connection sock.connect(sa)
TimeoutError: [WinError 10060]
I don't know where is problem... i tried many mail servers (gmail,o2,wp,) all of them returned the same error. I tried also other available solutions in the internet to send mail via pyython.... the same problem...
Please help
According to your posted backtrace, you are failing at the line
server = smtplib.SMTP('smtp.gmail.com:587')
I've run that line and it works for me. The clue is in the TimeoutError. I believe you are having networking problems and the code itself is fine.
You do have an extra call to ehlo but as it stands you are not getting that far.
Additionally, it would probably be better to pass the port number as a separate argument, rather than using a colon to split it. E.g.
server = smtplib.SMTP(host='smtp.gmail.com', port=587)
The documentation for smptlib is here: https://docs.python.org/2/library/smtplib.html
Try removing the second server.ehlo() . The one just before login. That worked for me.
toaddrs should be a list
toaddrs = ['targetmail#o2.pl']

Erratic behavior with email attachments using python smtplib

My email script is based on this script at Fine Frog. I'm using this script to send HTML log files from a number of remote machines using a variety of ISPs.
The attachment isn't being sent consistently, however. It does work 80% of the time, but I'm getting two types of weird behavior. The first is where the email isn't sent at all, but throws the error(s) you see below
Traceback (most recent call last):
File "/root/sapapps/reporter/usage_report.py", line 336, in ?
se.send_mail(['thinkwelldesigns#g1234.com'], cd.contact, 'dave#1234.com', report_subject, text_body, files=[report_name], bcc=[cd.tech_email])
File "/usr/lib/python2.4/site-packages/sap/send_email.py", line 61, in send_mail
mail_server.sendmail(server_addr, addresses, message.as_string())
File "/usr/lib/python2.4/smtplib.py", line 692, in sendmail
(code,resp) = self.data(msg)
File "/usr/lib/python2.4/smtplib.py", line 489, in data
self.send(q)
File "/usr/lib/python2.4/smtplib.py", line 319, in send
raise SMTPServerDisconnected('Server not connected')
smtplib.SMTPServerDisconnected: Server not connected
Or, on some machines, this error is raised.
reply: '421 Command timeout, closing transmission channel\r\n'
reply: retcode (421); Msg: Command timeout, closing transmission channel
data: (421, 'Command timeout, closing transmission channel')
send: 'rset\r\n'
Traceback (most recent call last):
File "/root/sapapps/reporter/usage_report.py", line 336, in ?
se.send_mail(['thinkwelldesigns#1234.com'], cd.contact, 'dave#1234.com', report_subject, text_body, files=[report_name], bcc=[cd.tech_email])
File "/usr/lib/python2.4/site-packages/sap/send_email.py", line 61, in send_mail
mail_server.sendmail(server_addr, addresses, message.as_string())
File "/usr/lib/python2.4/smtplib.py", line 694, in sendmail
self.rset()
File "/usr/lib/python2.4/smtplib.py", line 449, in rset
return self.docmd("rset")
File "/usr/lib/python2.4/smtplib.py", line 374, in docmd
return self.getreply()
File "/usr/lib/python2.4/smtplib.py", line 348, in getreply
line = self.file.readline()
File "/usr/lib/python2.4/socket.py", line 340, in readline
data = self._sock.recv(self._rbufsize)
socket.error: (104, 'Connection reset by peer')
In the second instance of weird behavior, the email is processed without error, the attachment goes along, but some data is stripped out of the HTML attachment in the emailing process. IOW, if you login to the remote machine, you'll find the original file intact, but the attached file is missed log entries.
I'm guessing that these issues are unrelated problems, but does anyone have some advice on how to most reliably send HTML attachments?
TIA,
Dave
I had a problem before using smtplib.SMTP, and figured out the email server required using an SSL connection. You could try using smtplib.SMTP_SSL if the regular call fails.
The first problem which raised one of the two following errors:
smtplib.SMTPServerDisconnected: Server not connected
or
socket.error: (104, 'Connection reset by peer')
was solved by switching to Gmail as the mail server.
The second error where part of HTML attachment was stripped out was solved by closing the report file before the email script processed...
html_report.close()
I'm a Python newbie. :-|

Categories

Resources