[英]Sending blank emails in python
使用python函數發送電子郵件時,我可以接收電子郵件。 但這是空白。
這是我的功能:
def send_email(first_name, password, access_key, secret_key, user_group_list, user_secrets_list, aws_account, aws_account_number, mail_body):
## Get the address to send to
to_addr = str(input("Enter the recipient's email address: "))
from_addr = 'cloudops@noreply.company.com'
## Get the user's first name
print(Fore.YELLOW)
first_name = input("Enter the recipient's first name: ")
subject = 'Welcome to AWS'
content = mail_body
msg = MIMEMultipart()
msg['From'] = from_addr
msg['To'] = to_addr
msg['Subject'] = subject
print("This is the content:", content, "\n")
body = MIMEText(content, 'html')
print("This is the body: " , body, "\n")
print("This is the Mesage: ", msg, "\n")
#msg.attach(body)
server = smtplib.SMTP('smtpout.us.companyworld.company.com', 25)
server.send_message(msg, from_addr=from_addr, to_addrs=[to_addr])
我已經驗證了我在函數中使用的變量的內容:
這是內容: <font size=2 face=Verdana color=black>Hello ,<br><br>You have been given access to this AWS account:<br><br>*jf-python-dev369812892824<br><br>You can get started by using the sign-in information provided below.<br><br>-------------------------------------------------<br><br>Sign-in URL:https://jf-python-dev.signin.aws.amazon.com/console<br>User name:tdunphy<br>Password:<br><br>-------------------------------------------------<br><br>When you sign in for the first time, you must change your password.<br><br>The user nametdunphy belongs to these groups:<br><br>grp-cloud-admins, grp-mfa-enforce<br><br>Regards,<br>Cloud Ops</font>
這是身體:
Content-Type: text/html; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
<font size=2 face=Verdana color=black>Hello ,<br><br>You have been given access to this AWS account:<br><br>*jf-python-dev369812892824<br><br>You can get started by using the sign-in information provided below.<br><br>-------------------------------------------------<br><br>Sign-in URL:https://jf-python-dev.signin.aws.amazon.com/console<br>User name:tdunphy<br>Password:<br><br>-------------------------------------------------<br><br>When you sign in for the first time, you must change your password.<br><br>The user nametdunphy belongs to these groups:<br><br>grp-cloud-admins, grp-mfa-enforce<br><br>Regards,<br>Cloud Ops</font>
這是味精:
Content-Type: multipart/mixed; boundary="===============1031248993=="
MIME-Version: 1.0
From: cloudops@noreply.company.com
To: tdunphy@company.com
Subject: Welcome to AWS
--===============1031248993==
--===============1031248993==--
為什么我收到空白電子郵件?
在問題代碼中,正文未附加到消息中,因此不會發送消息。 此版本的功能:
def send_email():
# Get the address to send to
to_addr = "to@example.com"
from_addr = "from@example.com"
# Get the user's first name
subject = "Welcome to AWS"
content = "<p>Hello world</p>"
msg = MIMEMultipart()
msg["From"] = from_addr
msg["To"] = to_addr
msg["Subject"] = subject
print("This is the content:", content, "\n")
body = MIMEText(content, "html")
msg.attach(body)
print("This is the body: ", body, "\n")
print("This is the Message: ", msg, "\n")
server = smtplib.SMTP("localhost", 1025)
server.send_message(msg, from_addr=from_addr, to_addrs=[to_addr])
產生以下輸出:
This is the content: <p>Hello world</p>
This is the body: Content-Type: text/html; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
<p>Hello world</p>
This is the Message: Content-Type: multipart/mixed; boundary="===============7817399740373236689=="
MIME-Version: 1.0
From: from@example.com
To: to@example.com
Subject: Welcome to AWS
--===============7817399740373236689==
Content-Type: text/html; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
<p>Hello world</p>
--===============7817399740373236689==--
這是smtpd調試服務器的輸出:
---------- MESSAGE FOLLOWS ----------
b'Content-Type: multipart/mixed; boundary="===============7817399740373236689=="'
b'MIME-Version: 1.0'
b'From: from@example.com'
b'To: to@example.com'
b'Subject: Welcome to AWS'
b'X-Peer: ::1'
b''
b'--===============7817399740373236689=='
b'Content-Type: text/html; charset="us-ascii"'
b'MIME-Version: 1.0'
b'Content-Transfer-Encoding: 7bit'
b''
b'<p>Hello world</p>'
b'--===============7817399740373236689==--'
------------ END MESSAGE ------------
如果要發送不包含其他MIME部分的html正文,則使用MIMEText作為消息容器更為簡單。
此功能版本
def send_email():
# Get the address to send to
to_addr = "to@example.com"
from_addr = "from@example.com"
# Get the user's first name
subject = "Welcome to AWS"
content = "<p>Hello world</p>"
msg = MIMEText(content, 'html')
msg["From"] = from_addr
msg["To"] = to_addr
msg["Subject"] = subject
print("This is the content:", content, "\n")
print("This is the Message: ", msg, "\n")
server = smtplib.SMTP("localhost", 1025)
server.send_message(msg, from_addr=from_addr, to_addrs=[to_addr])
產生以下輸出:
This is the content: <p>Hello world</p>
This is the Message: Content-Type: text/html; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
From: from@example.com
To: to@example.com
Subject: Welcome to AWS
<p>Hello world</p>
這是smtpd調試服務器的輸出:
---------- MESSAGE FOLLOWS ----------
b'Content-Type: text/html; charset="us-ascii"'
b'MIME-Version: 1.0'
b'Content-Transfer-Encoding: 7bit'
b'From: from@example.com'
b'To: to@example.com'
b'Subject: Welcome to AWS'
b'X-Peer: ::1'
b''
b'<p>Hello world</p>'
------------ END MESSAGE ------------
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.