向子组/超级组发送邮件——嵌套字典实现方案咨询
Alright, let’s break this down step by step. I’ll use Python for the example since it’s perfect for parsing text files and automating email sends, but you can easily adapt this logic to other tools/languages if that’s what you’re working with.
1. 先明确记录文件的预期结构
First, let’s assume your input file (let’s call it groups.txt) looks like this—adjust if your actual file uses a different separator between super groups:
超级组Alpha:super-alpha@example.com 子组Alpha-1:sub-alpha1@example.com 子组Alpha-2:sub-alpha2@example.com --- 超级组Beta:super-beta@example.com 子组Beta-1:sub-beta1@example.com
If your file doesn’t have separators, we’ll treat every first entry as a super group, followed by its sub groups until the next super group appears.
2. 解析文件,区分超级组和子组
First up, we’ll read the file, split each line into group names and email addresses, then group each super group with its corresponding sub groups.
Here’s a Python snippet to handle that:
def parse_groups(file_path): groups = [] current_super = None current_subs = [] with open(file_path, 'r', encoding='utf-8') as f: for line in f: line = line.strip() if not line: continue # Skip separator lines if your file uses them if line.startswith('---'): if current_super: groups.append((current_super, current_subs)) current_super = None current_subs = [] continue # Split each line into group name and email group_name, email = line.split(':', 1) # First entry (or after a separator) is the super group if not current_super: current_super = (group_name.strip(), email.strip()) else: current_subs.append((group_name.strip(), email.strip())) # Add the last super group + subs if they exist if current_super: groups.append((current_super, current_subs)) return groups
3. 构建收件人列表和邮件正文
Once we have our grouped data, we can create the colon-separated recipient list and generate an email body that includes all the group names as required.
def prepare_email_content(super_group, sub_groups): super_name, super_email = super_group # Build colon-separated recipient string recipient_emails = super_email sub_group_names = [] for sub_name, sub_email in sub_groups: recipient_emails += f':{sub_email}' sub_group_names.append(sub_name) # Generate body with all group names emphasized where needed body = f"您好,\n\n本次邮件发送给*{super_name}*及其下属子组:{', '.join(sub_group_names)}。\n\n请查收相关内容,如有疑问请联系管理员。\n\n此致\n邮件系统" return recipient_emails, body
4. 发送邮件
Finally, we’ll use Python’s built-in smtplib to send the email. You’ll need your email provider’s SMTP details (like Gmail’s smtp.gmail.com or your company’s internal SMTP server).
import smtplib from email.mime.text import MIMEText def send_email(recipient_emails, subject, body, sender_email, sender_pass, smtp_server, smtp_port): # Create the email message msg = MIMEText(body, 'plain', 'utf-8') msg['From'] = sender_email # Convert colon-separated recipients to comma-separated for email header msg['To'] = recipient_emails.replace(':', ', ') msg['Subject'] = subject # Connect to SMTP server and send with smtplib.SMTP(smtp_server, smtp_port) as server: server.starttls() # Enable TLS for security server.login(sender_email, sender_pass) server.send_message(msg) print(f"邮件已成功发送给:{recipient_emails}")
5. 整合所有步骤运行
Putting it all together into a single script:
if __name__ == "__main__": # Configure your settings here groups_file = 'groups.txt' sender_email = 'your-work-email@example.com' sender_password = 'your-app-password' # Use app-specific password for Gmail smtp_server = 'smtp.example.com' smtp_port = 587 email_subject = "【重要通知】超级组及子组专属邮件" # Parse the groups file all_group_sets = parse_groups(groups_file) # Send email for each super group + its sub groups for super_group, sub_groups in all_group_sets: recipients, email_body = prepare_email_content(super_group, sub_groups) send_email(recipients, email_subject, email_body, sender_email, sender_password, smtp_server, smtp_port)
额外小贴士
- If your record file has a different format (e.g., multiple emails per line), just tweak the
splitlogic in theparse_groupsfunction to match. - For non-Python environments: Use
awkin Bash to parse the file, thensendmailormuttto send emails—core logic stays the same: group first, build recipients, inject group names into the body. - Don’t forget to check your email provider’s SMTP rules: Gmail requires enabling SMTP and using app passwords, while company servers might need internal network access or specific authentication.
内容的提问来源于stack exchange,提问作者bob

