#!/usr/bin/env python # Import smtplib for the actual sending function import sys import getopt import smtplib sender = ‘[email protected]‘ #这里输入发件人邮箱地址 # If there are more than one receiver, you need to ganerate a list. receiver = [‘[email protected]‘,‘[email protected]‘] #这里输入收件人地址 cc_receiver = [‘[email protected]‘] #这里输入抄送地址 server = ‘smtp.163.com‘ #邮件服务器发送地址 port = ‘25‘ #端口 pwd = ‘xxxx‘ #你的邮箱密码 COMMASPACE = ‘, ‘ # Import the email modules we‘ll need #from email.mime.text import MIMEText from email.MIMEText import MIMEText from email.Header import Header def usage(): usageStr = ‘‘‘Usage: SendEmail -s "subject" -c "mail_content"‘‘‘ print usageStr def main(argv): # Get the Email content in the "-c" argv try: opts, args = getopt.getopt(argv, "s:c:") except getopt.GetoptError: usage() sys.exit(2) subject = ‘‘ content = ‘‘ for opt, arg in opts: if opt == ‘-c‘: content = arg if opt == ‘-s‘: subject = arg print content msg = MIMEText(content) msg[‘Subject‘] = subject msg[‘From‘] = sender msg[‘To‘] = COMMASPACE.join(receiver) msg[‘Cc‘] = COMMASPACE.join(cc_receiver) s = smtplib.SMTP(server, port) s.ehlo() s.login(sender, pwd) s.sendmail(sender, receiver, msg.as_string()) s.sendmail(sender, cc_receiver, msg.as_string()) s.quit() if __name__=="__main__": main(sys.argv[1:])
发送邮件用法:
python sendmail.py -s "标题" -c "发送的内容"
时间: 2024-11-03 09:53:47