为什么我通过python脚本通过电子邮件附件收到.bin扩展文件?

时间:2019-05-19 07:15:27

标签: python email email-attachments mime

我正在使用我的hotmail ID通过python脚本通过电子邮件发送附件,但是我接收的是.bin扩展名文件,而不是原始的文件扩展名。

我尝试发送带扩展名和不带扩展名的附件,但是问题是相同的。

#required libraries
import email
import smtplib
from email.mime.multipart import MIMEMultipart
from email import encoders
from email.mime.base import MIMEBase
from email.mime.text import MIMEText

#creditentials required
fromaddr = "abc@hotmail.com"
toaddr = "xyz@hotmail.com"
password = "password"

#input for name of file and its path
print('enter name of the file to send:')
filename = str(input())

print('enter path of the file')
fileToSend = str(input())
fileToSend = '/root' + fileToSend + '/' + filename

body = 'type body of mail here!'

print('Processing Please Wait....')

msg = MIMEMultipart() #object of mime multipart
msg['From'] = fromaddr
msg['To'] = toaddr
msg['Subject'] = "Backup"

# attach the body with the msg instance
msg.attach(MIMEText(body,'plain'))

# open the file to be sent
attachment = open(fileToSend, 'rb')

# instance of MIMEBase and named as p
p = MIMEBase('application', 'octet-stream')

# To change the payload into encoded form
p.set_payload((attachment).read())

# encode into base64
encoders.encode_base64(p)

p.add_header('Content-Disposition', "attachment; fileToSend= "+ 
fileToSend)

# attach the instance 'p' to instance 'msg'
msg.attach(p)

#sending text as a string
text = msg.as_string(p)

#to hotmail ID(first parameter is domain and second parameter is port 
number)
s = smtplib.SMTP("smtp.live.com",587)

#Puts connection to SMTP server in TLS mode
s.starttls()

#logging in ID
s.login(fromaddr, password)

#sending mail
s.sendmail(fromaddr, toaddr, msg.as_string())

s.quit()
print('Mail successfully send...')

我希望接收具有相同扩展名的文件,例如.py或.txt等,但我只接收.bin扩展名文件。

0 个答案:

没有答案