0

I am working on a big project, that includes a database for remembering users. il skip the details, but my client wants me to include a function by wich he can backup all the user data and other files.

I was thinking of a email, (since the project is a android app) and I was trying to figure out how you could send a attachement (i.e a .db sqlite3 file) in a email. I know theres alot of similair questions around over here, but all of the answers to this question gives me a error. here is the closest that I got:

This program sends a email without a attachment:

import smtplib

from email.mime.multipart import MIMEMultipart

from email.mime.text import MIMEText

boodskap = MIMEText("Toekomsweb Epos toets", 'plain')

van_adres = "from adres"

na_adres = "to adres"

epos_liggaam = MIMEMultipart('alternatief')

epos_liggaam['Subject'] = "Toets"

epos_liggaam['From'] = van_adres

epos_liggaam['To'] = na_adres

epos_liggaam.attach(boodskap)

mail = smtplib.SMTP('smtp.gmail.com',587)

mail.ehlo()

mail.starttls()

mail.login(van_adres,'PASSWORD')

mail.sendmail(van_adres,na_adres,epos_liggaam.as_string())

mail.close()
print("succes!")

please excuse my poor variable naming, its not in english.

any help on sending a attachment?

Thanks!

Community
  • 1
  • 1
Cid-El
  • 500
  • 7
  • 30
  • Why not just send the backup file to some dedicated FTP server? Sending thisas email attachement seems to be very bad idea - at least since there can be some attachement size restrictions on the STMP server side which will not let you send the backup one day (when DB snapshot will enlarge). – m.antkowicz Sep 27 '16 at 09:04
  • Ok, you have prooved you had made some research by showing code and referencing other questions and answers from SO. But you say that the other answers *give you error* without explaining what was the code used in testing and what the error was. You should show that to get help in debugging it. – Serge Ballesta Sep 27 '16 at 09:13
  • The error I got was: error: [Errno 111] Connection refused, but not to worry, I figured it out, for some reason linux have a different way of sending a email, so the problem was not with the attachment, but rather with the sending... Il answer my own question with the code I used to get it working... – Cid-El Sep 27 '16 at 09:26

1 Answers1

0

Hi this is the code I used... it turns out that ubunto uses a different way of sending a email than windows.

import smtplib

from email.mime.multipart import MIMEMultipart

from email.mime.text import MIMEText

from email.mime.base import MIMEBase

from email import encoders

import os

boodskap = MIMEText("Toekomsweb Epos toets", 'plain')

van_adres = 'From adres'

na_adres = 'To adres'

epos_liggaam = MIMEMultipart('alternatief')

epos_liggaam['Subject'] = "Toets"

epos_liggaam['From'] = van_adres

epos_liggaam['To'] = na_adres

epos_liggaam.attach(boodskap)

f = "toets.db"
part = MIMEBase('application', "octet-stream")
part.set_payload( open(f,"rb").read() )
encoders.encode_base64(part)
part.add_header('Content-Disposition', 'attachment; filename="{0}"'.format(os.path.basename(f)))
epos_liggaam.attach(part)

mail = smtplib.SMTP('smtp.gmail.com',587)

mail.ehlo()

mail.starttls()

mail.login(van_adres,'PASSWORD')

mail.sendmail(van_adres,na_adres,epos_liggaam.as_string())

mail.close()
print("succes!")

this answer was adapted from here (second answer)

hope that this answer will answer other people's question aswell

Community
  • 1
  • 1
Cid-El
  • 500
  • 7
  • 30