本文目录导读:

- Python + SMTP(通用方案)
- Outlook 自动化(Windows)
- 使用 yagmail(更简单的Python方案)
- 纯HTML签名模板(可嵌入任何邮件系统)
- 快速开始指南
- 使用环境变量存储密码(推荐)
我来帮你写一个自动签署邮件的脚本,根据不同的邮件系统和使用场景,我提供几种常见方案:
Python + SMTP(通用方案)
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.utils import formataddr
import os
class EmailSigner:
def __init__(self, smtp_server, port, username, password):
self.smtp_server = smtp_server
self.port = port
self.username = username
self.password = password
def create_signature(self, name, title, company, phone, email, address=None):
"""创建HTML格式签名"""
signature = f"""
<div style="font-family: Arial, sans-serif; font-size: 14px; color: #333;">
<p style="margin: 0;">谨祝商祺,</p>
<br>
<p style="margin: 0; font-weight: bold; font-size: 16px;">{name}</p>
<p style="margin: 0; color: #666;">{title}</p>
<p style="margin: 0; color: #666;">{company}</p>
<hr style="border: 1px solid #0088cc; width: 300px;">
<p style="margin: 5px 0; color: #888;">
📞 电话: {phone}<br>
✉️ 邮箱: {email}
{'📍 地址: ' + address if address else ''}
</p>
</div>
"""
return signature
def send_email_with_signature(self, to_email, subject, content, signature):
"""发送带签名的邮件"""
msg = MIMEMultipart('alternative')
msg['From'] = formataddr((self.username, self.username))
msg['To'] = to_email
msg['Subject'] = subject
# 组合正文和签名
full_html = f"""
<html>
<body>
<p>{content}</p>
{signature}
</body>
</html>
"""
msg.attach(MIMEText(full_html, 'html', 'utf-8'))
try:
# 连接SMTP服务器
server = smtplib.SMTP(self.smtp_server, self.port)
server.starttls()
server.login(self.username, self.password)
server.sendmail(self.username, to_email, msg.as_string())
server.quit()
print(f"邮件发送成功到 {to_email}")
except Exception as e:
print(f"邮件发送失败: {e}")
# 使用示例
if __name__ == "__main__":
# 配置你的邮箱信息
signer = EmailSigner(
smtp_server="smtp.gmail.com",
port=587,
username="your_email@gmail.com",
password="your_app_password" # 使用应用专用密码
)
# 创建签名
signature = signer.create_signature(
name="张三",
title="销售经理",
company="某某科技有限公司",
phone="138-0000-0000",
email="zhangsan@company.com",
address="北京市朝阳区xx路xx号"
)
# 发送邮件
signer.send_email_with_signature(
to_email="client@example.com",
subject="产品报价",
content="尊敬的客户,您好!<br>感谢您的咨询,以下是我们的产品报价...",
signature=signature
)
Outlook 自动化(Windows)
import win32com.client as win32
import datetime
def add_signature_to_outlook_email(recipient, subject, body, signature_choice=0):
"""
使用Outlook发送带签名的邮件
signature_choice: 0=默认签名, 1=第一个签名, 2=第二个签名等
"""
outlook = win32.Dispatch('outlook.application')
mail = outlook.CreateItem(0) # 0 = olMailItem
# 设置收件人、主题和正文
mail.To = recipient
mail.Subject = subject
mail.Body = body
# 添加签名(方法1:通过HTML)
# Outlook签名通常需要HTML格式
signature_html = """
<html>
<body>
<p>{}</p>
<br>
<div style="font-family: Arial; font-size: 12px;">
<p><b>张三</b></p>
<p>某某科技有限公司</p>
<p>电话: 138-0000-0000</p>
<p>邮箱: zhangsan@company.com</p>
</div>
</body>
</html>
""".format(body)
mail.HTMLBody = signature_html
# 方法2:使用Outlook预置的签名
# 访问Outlook的签名文件
# mail.AddSignature(signature_choice) # 某些版本不支持此方法
# 保存到草稿箱(不直接发送)
mail.Save()
# 如果要直接发送,取消注释下面这行
# mail.Send()
print("邮件已创建(保存在草稿箱)")
# 使用示例
if __name__ == "__main__":
add_signature_to_outlook_email(
recipient="client@example.com",
subject="会议安排",
body="尊敬的客户,我们拟定于下周进行项目讨论会议..."
)
使用 yagmail(更简单的Python方案)
import yagmail
import os
def create_html_signature():
"""创建HTML签名模板"""
signature = """
<div style="font-family: Calibri, Arial, sans-serif; font-size: 14px; color: #333333; border-top: 2px solid #4A90D9; padding-top: 10px;">
<p style="margin: 0; font-size: 16px; font-weight: bold; color: #4A90D9;">李四</p>
<p style="margin: 5px 0 0 0; color: #666666;">技术总监</p>
<p style="margin: 5px 0 0 0; color: #666666;">某某网络科技有限公司</p>
<p style="margin: 5px 0 0 0; color: #999999;">
☎ +86-139-0000-0000 | ✉ lisi@company.com | 🌐 www.company.com
</p>
<p style="margin: 5px 0 0 0; font-size: 12px; color: #999999;">地址:上海市张江高科技园区</p>
</div>
"""
return signature
def send_email_with_signature(recipient, subject, body):
"""使用yagmail发送带签名的邮件"""
# 配置邮箱
yag = yagmail.SMTP(
user='your_email@gmail.com',
password='your_app_password' # 使用应用专用密码
)
# 组合HTML内容
html_content = f"""
<html>
<body>
<p>{body}</p>
<br>
{create_html_signature()}
</body>
</html>
"""
try:
yag.send(
to=recipient,
subject=subject,
contents=html_content
)
print(f"邮件已发送至 {recipient}")
except Exception as e:
print(f"发送失败: {e}")
# 使用示例
if __name__ == "__main__":
send_email_with_signature(
recipient="partner@company.com",
subject="合作意向书",
body="尊敬的合作伙伴,我们很荣幸能与贵公司开展合作..."
)
纯HTML签名模板(可嵌入任何邮件系统)
这是一个可复用的HTML签名模板:
<!-- email_signature.html -->
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
</head>
<body style="margin: 0; padding: 0; font-family: 'Helvetica Neue', Arial, sans-serif;">
<table cellpadding="0" cellspacing="0" border="0" style="border-spacing: 0; border-collapse: collapse;">
<tr>
<!-- 左侧:Logo或头像区域 -->
<td style="padding-right: 15px; vertical-align: middle;">
<div style="width: 80px; height: 80px; border-radius: 50%;
background-color: #4A90D9; display: flex; align-items: center;
justify-content: center; color: white; font-size: 24px; font-weight: bold;">
张三
</div>
</td>
<!-- 右侧:员工信息 -->
<td style="vertical-align: middle;">
<table>
<tr>
<td style="font-size: 16px; font-weight: bold; color: #333; padding-bottom: 3px;">
张三
</td>
</tr>
<tr>
<td style="font-size: 13px; color: #666; padding-bottom: 2px;">
销售总监 · 某某科技有限公司
</td>
</tr>
<tr>
<td style="font-size: 13px; color: #666; padding-bottom: 2px;">
<span style="color: #4A90D9;">☎</span> +86-138-0000-0000
</td>
</tr>
<tr>
<td style="font-size: 13px; color: #666; padding-bottom: 2px;">
<span style="color: #4A90D9;">✉</span> zhangsan@company.com
</td>
</tr>
<tr>
<td style="font-size: 13px; color: #666;">
<span style="color: #4A90D9;">📍</span> 北京市海淀区xx路xx号
</td>
</tr>
</table>
</td>
</tr>
</table>
<!-- 底部分隔线 -->
<div style="border-top: 2px solid #4A90D9; margin-top: 10px; width: 100%;"></div>
<!-- 公司标语(可选) -->
<p style="font-size: 11px; color: #999; margin-top: 5px; font-style: italic;">
诚信为本 · 专业制胜
</p>
</body>
</html>
快速开始指南
安装依赖
# pip安装 pip install yagmail smtplib # 或使用pip3 pip3 install yagmail
配置说明
-
SMTP服务器配置:
- Gmail:
smtp.gmail.com:587(需启用两步验证+应用专用密码) - QQ邮箱:
smtp.qq.com:465 - 163邮箱:
smtp.163.com:465 - Outlook:
smtp-mail.outlook.com:587
- Gmail:
-
安全提示:
- 不要直接在代码中存储密码,建议使用环境变量
- 生产环境使用加密存储
使用环境变量存储密码(推荐)
import os
import yagmail
# 设置环境变量(示例,实际运行时在终端设置)
# os.environ['EMAIL_USER'] = 'your_email@gmail.com'
# os.environ['EMAIL_PASS'] = 'your_app_password'
email_user = os.getenv('EMAIL_USER')
email_pass = os.getenv('EMAIL_PASS')
yag = yagmail.SMTP(user=email_user, password=email_pass)
这些脚本可以根据你的具体需求进行调整,如果需要特定邮件客户端(如Foxmail、Thunderbird等)的签名方案,请告诉我具体需求。