簡體   English   中英

Python-可以在普通項目中使用附件中的郵件,但不能在類中使用

[英]Python- Can use the mailing in attachment in a plain project but not in a class

我正在從Windows文件夾中選擇文件,並且嘗試通過gmail發送文件。
當在單獨的python程序中執行此代碼時,它工作正常,但我無法在類中使用同一程序。

import smtplib
import base64

filename = str("JDI Offline.txt").split('\\')[-1:][0]
filepath = "C:\Windows\JDI Offline.txt"

fo = open(filepath, "rb")
filecontent = fo.read()
fo.close()
encodedcontent = base64.b64encode(filecontent)
sender = 'vusvarshil@gmail.com'
receiver = 'vusvarshil@gmail.com'

marker = "AUNIQUEMARKER"

body = """
Test to send attatchment
"""

part1 = """From: From Varshil<vusvarshil@gmail.com>
To:me
Subject: Sending attatchment
MIME-Version: 1.0
Content-Type: multipart/mixed; boundary=%s
--%s
""" % (marker, marker)

part2 = """Content-Type: text/plain
Content-Transfer-Encoding:8bit

%s
--%s
""" % (body, marker)

part3 = """Content-Type:multipart/mixed; name=\"%s\"
Content-Transfer-Encoding:base64
Content-Disposition: attatchment; filepath=%s

%s
--%s--
""" % (filename, filepath, encodedcontent, marker)
message = part1 + part2 + part3

try:
    mail = smtplib.SMTP('smtp.gmail.com', 587)
    mail.ehlo()
    mail.starttls()
    mail.login('vusvarshil@gmail.com', 'xxxxxxxx')
    mail.sendmail('vusvarshil@gmail.com', 'vusvarshil@gmail.com', message)
    print "Mail Sent successfully"
except Exception:
    print "Error"

當我在課堂上使用此代碼時,我會收到一封空白郵件。

在類中使用時的程序程序
不帶類使用時的程序程序

sendmail類:def send_mail():導入smtplib導入base64

filename = str("JDI Offline.txt").split('\\')[-1:][0]
filepath = "C:\Windows\JDI Offline.txt"

fo = open(filepath, "rb")
filecontent = fo.read()
fo.close()
encodedcontent = base64.b64encode(filecontent)
sender = 'vusvarshil@gmail.com'
receiver = 'vusvarshil@gmail.com'

marker = "AUNIQUEMARKER"

body = """
Test to send attatchment
"""

part1 = """From: From Varshil<vusvarshil@gmail.com>
To:me
Subject: Sending attatchment
MIME-Version: 1.0
Content-Type: multipart/mixed; boundary=%s
--%s
""" % (marker, marker)

part2 = """Content-Type: text/plain
Content-Transfer-Encoding:8bit

%s
--%s
""" % (body, marker)

part3 = """Content-Type:multipart/mixed; name=\"%s\"
Content-Transfer-Encoding:base64
Content-Disposition: attatchment; filepath=%s

%s
--%s--
""" % (filename, filepath, encodedcontent, marker)
message = part1 + part2 + part3

try:
    mail = smtplib.SMTP('smtp.gmail.com', 587)
    mail.ehlo()
    mail.starttls()
    mail.login('vusvarshil@gmail.com', 'xxxxxxxx')
    mail.sendmail('vusvarshil@gmail.com', 'vusvarshil@gmail.com', message)
    print "Mail Sent successfully"
except Exception:
    print "Error"
ob=sendmail()
ob.send_email()

有人告訴我這是什么問題。

我找到了解決問題的方法。 該代碼無效,因此我搜索了一個新代碼。我將其發布在此處以供將來參考。 在下面的代碼中,我正在從文件中讀取數據並附加標簽,因為這是我的要求。 您可以根據需要更改代碼

# coding:utf-8



  import smtplib
  from email import MIMEMultipart
  from email import MIMEText
  from email import MIMEBase
  from email import encoders

  class classname:
  def fname(self):
    fromaddr = "mshah9900@gmail.com"
    toaddr = "vusvarshil@gmail.com"

    msg = MIMEMultipart.MIMEMultipart()

    msg['From'] = fromaddr
    msg['To'] = toaddr
    msg['Subject'] = "SUBJECT OF THE EMAIL"

    body = "TEXT YOU WANT TO SEND"

    msg.attach(MIMEText.MIMEText(body, 'plain'))

    filename = "JDI Offline.txt"
    fo = open("C:\Windows\JDI Offline.txt", "rb")

    length = len(fo.readlines())
    fo.close()
    fo = open("C:\Windows\JDI Offline.txt", "rb")

    filecontent = ""

    arr = ["Company name", "Contact person name", "Email", "Contact no.", "Address_Line_1", "Address_Line_2",
           "Address_Line_3", "City", "Pincode", "State", "Country", "PanCard No.", "Model", "Size", "Device Name",
           "Serial Number", "MediaType", "Interface type", "OS", "Ip address", "Registered date", "Registered Time"]
    for x in range(0,length):
             filecontent += arr[x] + ": " + fo.readline().lstrip().rstrip() + "\n"



    #     fo.close()

    filecontent += """\n
Additional content

"""

    fo.close()
    #print filecontent


    attachment = open("C:\Windows\JDI Offline.txt", "rb")
    # attachment += "hello"

    part = MIMEBase.MIMEBase('application', 'octet-stream')
    part.set_payload((filecontent))
    encoders.encode_base64(part)
    part.add_header('Content-Disposition', "attachment; filename= %s" % filename)

    msg.attach(part)

    server = smtplib.SMTP('smtp.gmail.com', 587)
    server.starttls()
    server.login(fromaddr, "xxxxxxxx")
    text = msg.as_string()
    server.sendmail(fromaddr, toaddr, text)
    server.quit()

    cn=classname()
    cn.fname()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM