I'm running an online course of Python3, I'm trying to send an email via my localhost server. Here is my code script
#!/usr/bin/python3 Sending email with attachment
import smtplib
import base64
filename = "temp/test.text"
#Read a file and encode it into base64 format
fo = open(filename, "rb")
filecontent = fo.read()
encodedcontent = base64.b64encode(filecontent) #base64
sender = '[email protected]'
receiver = '[email protected]'
marker = "AUNIQUEMARKER"
body = """
This is our bulk email marketing software launching message.
#Define the main headers.
part1 ="""From: From EAGOT <[email protected]>
To: To Baba Elgon <[email protected]>
Subject: Sending Demo copy of The Email marketing software
MIME-Version: 1.0
Content-Type: multipart/mixed; boundary=%s
--%s
""" % (marker, marker)
# Define the message action
part2 = """Content-Type: text/plain
Content-Transfer-Encoding:"8bit"
%s
--%s
""" (body, marker)
# Define the attachment section
part3 = """Content-Type: multipart/mixed; name=\"%s\"
Content-Transfer-Encoding:base64
Content-Disposition: attachment; filename=%s
%s
--%s--
""" %(filename, filename, encodedcontent, marker)
message = part1 + part2 + part3
try:
smtpObj = smtplib.SMTP('localhost')
smtpObj.sendmail(sender, receiver, message)
print "Successfully sent email"
except Exception:
print (Error! Unable to send email!)
但是我最终遇到了这个代码错误:
File "C:\Users\Vicento Graphix\AppData\Roaming\JetBrains\PyCharmEdu2022.2\scratches\scratch_7.py", line 22
part1 ="""From: From EAGOT <[email protected]>
^^^^
SyntaxError: invalid syntax
Process finished with exit code 1
我尝试在网上寻找解决方案,但未能解决问题,请帮忙。
我尝试通过本地主机发送电子邮件,我期望收到“电子邮件已成功发送”消息。不幸的是我最终得到了
part1 ="""From: From EAGOT <[email protected]>
^^^^
SyntaxError: invalid syntax
我该如何解决这个问题。我需要帮助,因为我处于学习 python3 的初学者水平
我建议您使用一个好的 IDE 来指出这些语法错误。
您忘记关闭
body
变量。应该是:
body = "This is our bulk email marketing software launching message."
第 50 行也有语法问题:
print ("Successfully sent email") # <-- add parentheses and quotes.
第 52 行:
print ("Error! Unable to send email!") # <-- add quotes