def gmail_send(): smtp_host = 'smtp.gmail.com' smtp_port = 587 from_email = '' username = '' # mailのアドレス password = '' # mailのパスワード # メールの内容を作成 msg = message.EmailMessage() html = mail_msg() PRDmsg = PRD_msg() msg.set_content(html) # メールの本文 yesterday = PRDmsg['Day'][1] msg['Subject'] = f"【{yesterday}】{pj_info.COMPANY}_{pj_info.BATCH}Trainee news" # 件名 msg['From'] = from_email # メール送信元 msg['To'] = pj_info.to_email # メール送信先 msg['Cc'] = pj_info.cc_email asparagus_cid = make_msgid() msg.add_alternative(html.format(asparagus_cid=asparagus_cid[1:-1]), subtype='html') # メールサーバーへアクセス server = smtplib.SMTP(smtp_host, smtp_port) server.ehlo() server.starttls() server.ehlo() server.login(username, password) # server.send_message(msg) toaddrs = [pj_info.to_email] + [pj_info.cc_email] server.sendmail(from_email, toaddrs, msg.as_string()) server.quit()
def mail(update): # メールの内容を作成 msg = message.EmailMessage() msg.set_content('Book1st Ranking') # メールの本文 msg['Subject'] = 'Book1st Ranking' + update # 件名 msg['From'] = from_email # メール送信元 msg['To'] = to_email #メール送信先 msg['Bcc'] = bcc_email #bcc送信先 #添付ファイルを作成する。 mine={'type':'text','subtype':'comma-separated-values'} attach_file={'name':'Book1stRankingBooks.csv','path':'./Book1stRankingBooks.csv'} file = open(attach_file['path'],'rb') file_read = file.read() msg.add_attachment(file_read, maintype=mine['type'], subtype=mine['subtype'],filename=attach_file['name']) file.close() # メールサーバーへアクセス server = smtplib.SMTP(smtp_host, smtp_port) server.ehlo() server.starttls() server.ehlo() server.login(username, password) server.send_message(msg) server.quit()
def create_message(self, to_addr, subject, body): msg = message.EmailMessage() msg.set_content(body) msg['Subject'] = subject msg['From'] = self.EMAIL_ADDRESS msg['To'] = to_addr return msg
def test2(): smtp = smtplib.SMTP('smtp.163.com', 25) smtp.starttls() smtp.login('*****@*****.**', 'Dx2011512') msg = message.EmailMessage() msg.set_content('你好,中国') SUBJECT = u" 于振泉python邮件测试 " # 定义邮件主题 FROM = "*****@*****.**" # 定义邮件发件人 TO = "*****@*****.**" # 定义邮件收件人 msg['Subject'] = SUBJECT msg['From'] = FROM msg['To'] = TO msg.preamble = 'You will not see this in a MIME-aware mail reader.\n' # msg.preamble = u'我是前言' for file in Path('.').iterdir(): if file.is_file() and file.name.endswith('.jpg'): with open(file, 'rb') as f: read = f.read() msg.add_attachment(read, maintype='image', subtype=imghdr.what(None, read)) smtp.send_message(msg) smtp.quit()
def send_mail(code, email, name): text = f"""{name}様 LIMEです。 ご登録ありがとうございます。 以下の認証コードをLIMEの画面内に入力いただきますと、登録が完了いたします。 認証コード: 【{code}】 認証コードは30分後の {(datetime.datetime.today()+datetime.timedelta(0,1800)).strftime("%Y/%m/%d %H:%M")} まで有効です。 心当たりのない方はこのメールを破棄してください。 LIME """ smtp_host = 'smtp.gmail.com' smtp_port = 587 from_email = '*****@*****.**' to_email = email username = '******' password = open("password.txt", "r").read() msg = message.EmailMessage() msg.set_content(text) msg['Subject'] = '【LIME】登録認証コード' msg['From'] = from_email msg['To'] = to_email server = smtplib.SMTP(smtp_host, smtp_port) server.ehlo() server.starttls() server.ehlo() server.login(username, password) server.send_message(msg) server.quit()
def ReportOnGmail(filesize1,filesize2,programstatus): rawfiletmp = open("account.json" , "r") accountjsonfile = json.load(rawfiletmp) smtp_host = 'smtp.gmail.com' smtp_port = 587 from_email = accountjsonfile["from_email"] # 送信元のアドレス to_email = accountjsonfile["to_email"] # 送りたい先のアドレス username = accountjsonfile["username"] # Gmailのアドレス password = accountjsonfile["password"] # Gmailのパスワード # メールの内容を作成 msg = message.EmailMessage() mailbody="The olddest folder are deleted and {0}MB are relesed\n Security camera program is now active:{2}\n Yesterday file size was {1}MB".format(filesize1,filesize2,programstatus) msg.set_content(mailbody) # メールの本文 msg['Subject'] = 'bulletin_'+date.today().strftime("%Y_%m_%d")# 件名 msg['From'] = from_email # メール送信元 msg['To'] = to_email #メール送信先 # メールサーバーへアクセス server = smtplib.SMTP(smtp_host, smtp_port) server.ehlo() server.starttls() server.ehlo() server.login(username, password) server.send_message(msg) server.quit() return
def reset(email): conf = current_app.config user = (db_session.query(User).filter(User.email != None).filter( User.email == email).one_or_none()) if user: tk = ts.sign(user.id) reset = "{}/reset/{}".format("https://citizenscience.ch", tk.decode("utf-8")) text = "Hello! \n Someone requested a password for your account. Please click the link {} to change it. \n Thanks, The Citizen Science Team".format( reset) msg = message.EmailMessage() msg.set_content(text) smtp_user = "******" msg["Subject"] = "Password Reset for Your citizenscience.ch Account" msg["From"] = smtp_user msg["To"] = user.email try: s = smtplib.SMTP("asmtp.mailstation.ch", 587) s.login(smtp_user, "L6Ahfb3C") s.sendmail(smtp_user, [user.email], msg.as_string()) s.quit() except Exception as e: print("ERROR RESETTING", e) return e, 503 return NoContent, 200 else: return NoContent, 401
def email_object(): msg = message.EmailMessage() msg['From'] = 'sender' msg['To'] = 'reciever' msg['Subject'] = 'subject' msg['Date'] = datetime.datetime.now() msg.set_content('Body') return msg
def create_message(cls, from_email: str, to_email: str, content: str, subject: str) -> object: msg = message.EmailMessage() msg.set_content(content) msg["Subject"] = subject msg["From"] = from_email msg["To"] = to_email return msg
def main(): """构建并打印一封电子邮件""" msg = message.EmailMessage(policy.SMTP) msg['To'] = 'test recipient <*****@*****.**>' msg['From'] = 'test sender <*****@*****.**>' msg['Subject'] = 'test message, chapter 12' msg['Date'] = utils.formatdate(localtime=True) msg['Message-ID'] = utils.make_msgid() msg.set_content(TEXT) sys.stdout.buffer.write(msg.as_bytes())
def send_message(f, subject, note): m = message.EmailMessage() m.set_content(str(note)) m["Subject"] = str(subject) m["From"] = str(f) m["To"] = "*****@*****.**" m.as_string() s = smtplib.SMTP(host="web.electricembers.net", port=25) # print("made smpt object") s.send_message(m) # print("sent") s.quit()
def _send_email(self, mail_title, mail_message): msg = message.EmailMessage() msg.set_content(mail_message) msg['Subject'] = mail_title msg['From'] = self.from_email msg['To'] = self.to_email server = smtplib.SMTP(self.smtp_host, self.smtp_port) server.ehlo() server.starttls() server.ehlo() server.login(self.username, self.password) server.send_message(msg) server.quit()
def Mailsend(): # メールの内容を作成 msg = message.EmailMessage() # メールの本文 msg.set_content(bH) # 件名 msg['Subject'] = '今日のBlogosヘッドライン' # メール送信元 msg['From'] = from_email # メール送信先 msg['To'] = to_email # メールサーバーへアクセス server = smtplib.SMTP(smtp_host, smtp_port) server.ehlo() server.starttls() server.ehlo() server.login(username, password) server.send_message(msg) server.quit()
from email import message import smtplib # このままではgoogleには安全度が低いと見なされている # 参考: https://qiita.com/oppasiri330/items/58d42cfb556209c77896 smtp_host = 'smtp.gmail.com' smtp_port = 587 from_email = 'xxxxxxxxxxx' to_email = '*****@*****.**' username = '******' password = '******' msg = message.EmailMessage() msg.set_content('Test email') msg['Subject'] = 'Test email sub' msg['From'] = from_email msg['To'] = to_email server = smtplib.SMTP(smtp_host, smtp_port) server.ehlo() server.starttls() server.ehlo() server.login(username, password) server.send_message(msg) server.quit()
def __init__(self): self.msg = message.EmailMessage(policy.SMTP)
import email.message as em import email.utils as eu #邮箱账户 account = '*****@*****.**' #安全码 code = 'fzbifrurnhhwcajd' #连接SMTP服务器 # smtplib.SMTP() #普通的SMTP连接 #server = smtplib.SMTP_SSL('smtp.qq.com',, 21) # smtplib.SMTP_SSL() #基于SSL的SMTP连接 server = smtplib.SMTP_SSL('smtp.qq.com', 465) server.set_debuglevel(1) #登录邮箱服务器 server.login(account, code) #创建邮件对象 message = em.EmailMessage() #设置邮件内容 - 普通邮件 #message.set_content('The mail from python application.') #设置邮件内容 - HTML邮件 att_id1 = eu.make_msgid() message.set_content( """ <h2>The mail from python application.</h2> <div style = 'border:1px solid red'>HTML邮件内容</div> <img src = 'cid:%s' width = '200' height = '120'/> """ % att_id1[1:-1], 'html', 'utf-8') #设置邮件标题 message['subject'] = 'HTML邮件测试-带有附件' #设置发件人 message['from'] = 'Harry <%s>' % account #设置收件人
import smtplib from email import message from email import utils email_fromaddr = '*****@*****.**' email_psw = 'ehddbxzmegdydfid' email_conn = smtplib.SMTP_SSL('smtp.qq.com', 465) email_conn.set_debuglevel(1) email_conn.login(email_fromaddr, email_psw) email_info = message.EmailMessage() email_info.set_content('hello world', 'UTF-8') email_info['subject'] = '假装有一个主题' email_info['from'] = 'q.qchun<%s>' % email_fromaddr email_info['to'] = 'recochun<%s>' % '*****@*****.**' email_conn.sendmail(email_fromaddr, ['*****@*****.**'], email_info.as_string()) email_conn.quit()
import smtplib from email import message from unittest.mock import Mock, MagicMock # # Email # email_mock = message.EmailMessage() email_mock.set_content = Mock() email_mock.add_attachment = Mock() class EmailMessageMock: def __new__(cls, *args, **kwargs): return email_mock message.EmailMessage = EmailMessageMock smtp_mock = MagicMock() smtp_mock.__enter__.return_value = smtp_mock smtp_mock.send_message.return_value = "Mail sent to [email protected]" class SMTPMock: def __new__(cls, *args, **kwargs): return smtp_mock smtplib.SMTP = SMTPMock