Python邮件与日历管理

简介: 【4月更文挑战第13天】Python 通过 `smtplib` 和 `email` 发送邮件,`imaplib` 接收邮件。`google-api-python-client` 库用于管理 Google Calendar,示例代码展示了列出日历事件的功能。要使用 Google Calendar API,需设置服务帐户凭据和范围。

image.png

Python 提供了多种库,可以帮助你管理电子邮件和日历。下面是一些常用的库及其基本用法。

电子邮件管理

使用 smtplibemail 库发送电子邮件

Python 的 smtplibemail 库可以用来发送电子邮件。以下是一个简单的示例:

import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText

# 创建邮件对象
msg = MIMEMultipart()
msg['From'] = 'your-email@example.com'
msg['To'] = 'recipient-email@example.com'
msg['Subject'] = 'Hello, this is a test email'

# 添加邮件正文
body = 'This is the email body.'
msg.attach(MIMEText(body, 'plain'))

# 发送邮件
server = smtplib.SMTP('smtp.example.com', 587)  # 替换为你的 SMTP 服务器和端口
server.starttls()  # 启动 TLS 加密
server.login('your-email@example.com', 'your-password')  # 替换为你的邮箱和密码
server.sendmail(msg['From'], msg['To'], msg.as_string())
server.quit()

使用 IMAP 接收电子邮件

要接收电子邮件,你可以使用 imaplib 库。以下是一个简单的示例:

import imaplib
import email

# 连接到 IMAP 服务器
mail = imaplib.IMAP4_SSL('imap.example.com')  # 替换为你的 IMAP 服务器
mail.login('your-email@example.com', 'your-password')  # 替换为你的邮箱和密码

# 选择邮箱
mail.select('inbox')

# 搜索邮件
result, data = mail.uid('search', None, '(UNSEEN)')  # 搜索未读邮件
mail_ids = data[0]
id_list = mail_ids.split()
latest_email_id = int(id_list[-1])

# 获取邮件内容
_, msg_data = mail.uid('fetch', latest_email_id, '(BODY.PEEK[])')
raw_email = msg_data[0][1].decode('utf-8')
email_message = email.message_from_string(raw_email)

# 打印邮件主题和正文
print(email_message['Subject'])
for part in email_message.walk():
    if part.get_content_type() == 'text/plain':  # 忽略附件
        body = part.get_payload(decode=True)
        print(body.decode('utf-8'))

# 断开连接
mail.logout()

日历管理

对于日历管理,你可以使用 google-api-python-client 库来与 Google Calendar API 交互。以下是一个简单的示例,展示如何列出你的日历事件:

首先,你需要安装 google-api-python-client 库:

pip install google-api-python-client

然后,你可以使用以下代码列出你的日历事件:

from apiclient.discovery import build
from oauth2client.service_account import ServiceAccountCredentials

# 设置你的 Google Calendar API 凭据和范围
SCOPES = ['https://www.googleapis.com/auth/calendar.readonly']
KEY_FILE_LOCATION = 'path/to/your/service-account.json'  # 替换为你的服务帐户 JSON 文件路径

# 创建凭据对象
credentials = ServiceAccountCredentials.from_json_keyfile_name(KEY_FILE_LOCATION, SCOPES)

# 构建日历服务对象
calendar_service = build('calendar', 'v3', credentials=credentials)

# 列出主日历中的事件
now = calendar_service.events().list(calendarId='primary', timeMin='2023-01-01T00:00:00Z', timeMax='2023-12-31T23:59:59Z', singleEvents=True, orderBy='startTime').execute()
events = now.get('items', [])

# 打印事件信息
for event in events:
    start = event['start'].get('dateTime', event['start'].get('date'))
    print('Start: {}'.format(start))
    print('Summary: {}'.format(event['summary']))
    print('Description: {}'.format(event.get('description')))

请注意,这只是一个基本示例。为了完整的功能和错误处理,你可能需要根据你的具体需求进行更多的

目录
相关文章
|
4月前
|
存储 搜索推荐 数据安全/隐私保护
python实战讲解之使用Python批量发送个性化邮件
python实战讲解之使用Python批量发送个性化邮件
|
4月前
|
BI Python
python报表自动化系列 - 获取某月日历并以列表形式返回(公历)
python报表自动化系列 - 获取某月日历并以列表形式返回(公历)
24 1
|
8天前
|
Linux API Python
【python】如何通过python来发邮件,各种发邮件方式详细解析
【python】如何通过python来发邮件,各种发邮件方式详细解析
|
14天前
|
运维 Shell Linux
第十四章 Python发送邮件(常见四种邮件内容)
第十四章 Python发送邮件(常见四种邮件内容)
|
12天前
|
人工智能 Python
beets,一个有趣的 Python 音乐信息管理工具!
beets,一个有趣的 Python 音乐信息管理工具!
28 4
|
5月前
|
Python
Python自动化办公之Excel拆分并自动发邮件
Python自动化办公之Excel拆分并自动发邮件
|
5月前
|
存储 安全 网络安全
python自动化操作邮箱
python自动化操作邮箱
80 0
|
9月前
|
数据采集 Python
python自动签到参考网站
python自动签到参考网站
|
Python
使用Python自动发送邮件
使用Python自动发送邮件
298 0
使用Python自动发送邮件
|
数据安全/隐私保护 Python
python小玩意——自动发送邮件
python小玩意——自动发送邮件

热门文章

最新文章