Advertisement
Guest User

Untitled

a guest
May 31st, 2016
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.39 KB | None | 0 0
  1. import csv
  2. import smtplib
  3. import mimetypes
  4. import datetime
  5. import MySQLdb
  6. from email.mime.multipart import MIMEMultipart
  7. from email import encoders
  8. from email.message import Message
  9. from email.mime.audio import MIMEAudio
  10. from email.mime.base import MIMEBase
  11. from email.mime.image import MIMEImage
  12. from email.mime.text import MIMEText
  13. #l=raw_input("Dateiname(mit Endung): ")
  14. f = open("foo.csv")
  15. csv_f = csv.reader(f,delimiter=';')
  16. kunden_emails = []
  17. best_nr = []
  18. vorname= []
  19. nachname= []
  20.  
  21. for row in csv_f:
  22. best_nr.append(row[0])
  23. kunden_emails.append(row[1])
  24. vorname.append(row[2])
  25. nachname.append(row[3])
  26.  
  27.  
  28. n = len(best_nr) - 1
  29. i = 1
  30. #connect to db
  31. db = MySQLdb.connect(host="192.168.111.1",user="mails",passwd="sa83j2nNQXVWvTVL",db="mailversand")
  32. #setup cursor
  33. cur = db.cursor()
  34. #emailfrom = raw_input("Absenderadresse eingeben: ")
  35. #username = emailfrom
  36. #password = raw_input("Passwort: ")
  37. while i <= n:
  38. emailfrom = "jung@wunschreich.de"
  39. emailto = kunden_emails[i]
  40. fileToSend = best_nr[i] + ".pdf"
  41. username = "jung@wunschreich.de"
  42. password = "9Cdd9pTx"
  43.  
  44. msg = MIMEMultipart()
  45. msg["From"] = emailfrom
  46. msg["To"] = emailto
  47. msg["Subject"] = "Ihre Rechnung mit der Rechnungsnummer: " + best_nr[i]
  48. msg.preamble = "Ihre Rechnung mit der Rechnungsnummer: " + best_nr[i]
  49. html = """\
  50. <html>
  51. <head></head>
  52. <body>
  53. <p>Sehr geehrter Kunde,<br>
  54. im Anhang finden sie die Rechnung ihrer Bestellung.<br>
  55. </p>
  56. </body>
  57. </html>
  58. """
  59. part1 = MIMEText(html, 'html')
  60.  
  61. msg.attach(part1)
  62.  
  63. ctype, encoding = mimetypes.guess_type(fileToSend)
  64. if ctype is None or encoding is not None:
  65. ctype = "application/octet-stream"
  66.  
  67. maintype, subtype = ctype.split("/", 1)
  68.  
  69. if maintype == "text":
  70. fp = open(fileToSend)
  71.  
  72. attachment = MIMEText(fp.read(), _subtype=subtype)
  73. fp.close()
  74. elif maintype == "image":
  75. fp = open(fileToSend, "rb")
  76. attachment = MIMEImage(fp.read(), _subtype=subtype)
  77. fp.close()
  78. elif maintype == "audio":
  79. fp = open(fileToSend, "rb")
  80. attachment = MIMEAudio(fp.read(), _subtype=subtype)
  81. fp.close()
  82. else:
  83. fp = open(fileToSend, "rb")
  84. attachment = MIMEBase(maintype, subtype)
  85. attachment.set_payload(fp.read())
  86. fp.close()
  87. encoders.encode_base64(attachment)
  88. attachment.add_header("Content-Disposition", "attachment", filename=fileToSend)
  89. msg.attach(attachment)
  90.  
  91. server = smtplib.SMTP("smtp.1und1.de:587")
  92. server.starttls()
  93. server.login(username,password)
  94. server.sendmail(emailfrom, emailto, msg.as_string())
  95. server.quit()
  96. print kunden_emails [i]
  97. print best_nr [i]
  98. ts = datetime.datetime.now()
  99. ts.isoformat()
  100. #cur.execute("""insert into Mailversand values (%s,%s,%s)""", (kunden_emails[i],best_nr[i],ts))
  101. #INSERT INTO `mailversand` (`id`, `firstname`, `lastname`, `email`, `invoiceId`, `timestamp`, `processId`) VALUES (NULL, 'Max', 'Mustermann', 'max@mustermann.de', '', '2016-05-31', NULL);
  102. #INSERT INTO mailversand (firstname, lastname, email, invoiceId, timestamp) VALUES ("max", "Mustermann", "max@mustermann.de", "123123", "234324")
  103. try:
  104. cur.execute("""INSERT INTO mailversand (firstname, lastname, email, invoiceId, timestamp) VALUES (%s, %s, %s, %s, %s)""",(vorname[i], nachname[i],kunden_emails[i],best_nr[i], ts))
  105. #cur.commit()
  106. except:
  107. print "FEHLER!!!!!!!!!!!!!!!!"
  108.  
  109. i = i+1
  110.  
  111. cur.execute(""""SELECT * FROM mailversand;""")
  112. print cur.fetchall()
  113.  
  114. cur.close()
  115. db.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement