96

smtplib モジュールを使用して電子メールを正常に送信できます。ただし、電子メールが送信されると、送信される電子メールに件名が含まれません。

import smtplib

SERVER = <localhost>

FROM = <from-address>
TO = [<to-addres>]

SUBJECT = "Hello!"

message = "Test"

TEXT = "This message was sent with Python's smtplib."
server = smtplib.SMTP(SERVER)
server.sendmail(FROM, TO, message)
server.quit()

SUBJECTも送信メールに含めるには、「server.sendmail」の書き方を教えてください。

server.sendmail(FROM, TO, message, SUBJECT) を使用すると、「smtplib.SMTPSenderRefused」に関するエラーが発生します

4

8 に答える 8

183

ヘッダーとして添付します。

message = 'Subject: {}\n\n{}'.format(SUBJECT, TEXT)

その後:

server = smtplib.SMTP(SERVER)
server.sendmail(FROM, TO, message)
server.quit()

また、標準の Python モジュールを使用するemailことも検討してください。メールを作成する際に大いに役立ちます。

于 2011-08-29T15:23:19.087 に答える
18

これを試して:

import smtplib
from email.mime.multipart import MIMEMultipart
msg = MIMEMultipart()
msg['From'] = 'sender_address'
msg['To'] = 'reciver_address'
msg['Subject'] = 'your_subject'
server = smtplib.SMTP('localhost')
server.sendmail('from_addr','to_addr',msg.as_string())
于 2014-10-16T21:53:35.010 に答える
7

おそらく、コードを次のように変更する必要があります。

from smtplib import SMTP as smtp
from email.mime.text import MIMEText as text

s = smtp(server)

s.login(<mail-user>, <mail-pass>)

m = text(message)

m['Subject'] = 'Hello!'
m['From'] = <from-address>
m['To'] = <to-address>

s.sendmail(<from-address>, <to-address>, m.as_string())

明らかに、<>変数は実際の文字列値または有効な変数である必要があります。プレースホルダーとしてそれらを埋めただけです。これは、件名付きのメッセージを送信するときに機能します。

于 2011-08-29T15:23:31.780 に答える
5

smtplib のドキュメントの下部にあるメモを参照してください。

In general, you will want to use the email package’s features to construct an email message, which you can then convert to a string and send via sendmail(); see email: Examples.

のドキュメントのサンプル セクションへのリンクを次にemail示します。これは、実際に件名付きのメッセージの作成を示しています。https://docs.python.org/3/library/email.examples.html

smtplib は件名の追加を直接サポートしておらず、メッセージが既に件名などでフォーマットされていることを期待しているようです。それがemailモジュールの出番です。

于 2011-08-29T15:23:02.267 に答える
4

メッセージに含める必要があると思います:

import smtplib

message = """From: From Person <from@fromdomain.com>
To: To Person <to@todomain.com>
MIME-Version: 1.0
Content-type: text/html
Subject: SMTP HTML e-mail test

This is an e-mail message to be sent in HTML format

<b>This is HTML message.</b>
<h1>This is headline.</h1>
"""

try:
   smtpObj = smtplib.SMTP('localhost')
   smtpObj.sendmail(sender, receivers, message)         
   print "Successfully sent email"
except SMTPException:
   print "Error: unable to send email"

コード: http://www.tutorialspoint.com/python/python_sending_email.htm

于 2011-08-29T15:25:20.020 に答える
3
import smtplib
 
# creates SMTP session 

List item

s = smtplib.SMTP('smtp.gmail.com', 587)
 
# start TLS for security   
s.starttls()
 
# Authentication  
s.login("login mail ID", "password")
 
 
# message to be sent   
SUBJECT = "Subject"   
TEXT = "Message body"
 
message = 'Subject: {}\n\n{}'.format(SUBJECT, TEXT)
 
# sending the mail    
s.sendmail("from", "to", message)
 
# terminating the session    
s.quit()
于 2019-06-16T14:39:40.323 に答える