[python]代码库
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.application import MIMEApplication
sender_email = "sender@example.com"
receiver_email = "receiver@example.com"
password = "password"
subject = "Email Subject"
body = "Email Body"
attachment = "/path/to/attachment.pdf"
message = MIMEMultipart()
message["From"] = sender_email
message["To"] = receiver_email
message["Subject"] = subject
message.attach(MIMEText(body, "plain"))
with open(attachment, "rb") as file:
attachment = MIMEApplication(file.read(), _subtype="pdf")
attachment.add_header("Content-Disposition", "attachment", filename=os.path.basename(attachment))
message.attach(attachment)
smtp_server = smtplib.SMTP("smtp.gmail.com", 587)
smtp_server.starttls()
smtp_server.login(sender_email, password)
smtp_server.sendmail(sender_email, receiver_email, message.as_string())
smtp_server.quit()