簡體   English   中英

使用 python smtplib 為“發件人”電子郵件發送別名(欺騙)

[英]Sending an alias(spoof) for the 'from' e-mail using python smtplib

我正在使用 python 3.4.3 發送電子郵件,此時我需要使用別名發送電子郵件。 該帳戶是 gmail 帳戶,但我需要能夠將我想要的任何內容作為惡搞(別名)“發件人”電子郵件。 我已經非常努力地研究如何做到這一點並且運氣很少。 鑒於我查看的線程數量以及我沒有得到可行答案的事實表明缺乏對這個特定主題的討論。 我希望這不僅僅是因為這很容易,以至於除了我之外的每個人都知道如何去做。

我應該提到我在 windows 10 機器上,但也可以使用 Ubuntu 和 Windows 7 機器。

import smtplib

fromreal = 'realmail@gmail.com'
fromshow = 'fakemail@gmail.com'
toaddy = ['rec01@gmail.com', 'rec02@gmail.com']
subject = ' test'
body = 'This is the body test'

content = '''\
From: %s
To: %s
Subject: %s
%s
''' % (fromshow, ', '.join(toaddy), subject, body)

server = 'smtp.gmail.com'
port = 587

mail = smtplib.SMTP(server, port)

mail.ehlo()
mail.starttls()
mail.login(fromreal, 'password')
try:
    mail.sendmail(fromshow, toaddy, content)
    print('E-mail sent.')
except:
    print('E-mail not sent.')

mail.close()

您可以使用yagmail發送別名(不會更改為假電子郵件,但至少是別名):

import yagmail
# first is "from" arg; using a dictionary you can give an alias as value
yag=yagmail.SMTP({fromreal:'fakealias'}, 'password') 
yag.send(toaddy, subject, body)

有 3 行而不是 30 行有多好;)

使用pip install yagmail

github頁面上閱讀有關許多其他功能的更多信息。

除此之外,您可以使用“無密碼”腳本(腳本中不需要密碼),非常容易發送 HTML、內嵌圖像和附件!

完全披露:我是yagmail的開發者/維護者。

你的代碼很好,

Google 會阻止您設置不屬於您的別名 email。 這就是為什么您需要在您的 gmail 帳戶中設置別名的原因。 為此,go 到https://mail.google.com/ -> 設置 ->(查看所有設置)-> 帳戶 -> 將郵件發送為: -> 添加另一個 Z0C83F57C786A0B4A39EFAB237317C 地址。

驗證 email 地址,然后您可以設置代碼中使用的別名。

如果您收到 SMTPAuthenticationError (534, b'5.7.9 Application-specific password required. ...),您應該按照鏈接設置應用程序密碼而不是您的真實密碼。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM