3

メールを送信する単純な python スクリプトを作成しようとしています。私はこの次のコードを使用しました:

import subprocess

params = {'from':    'from@example.com',
          'to':      'to@example.com',
          'subject': 'Message subject'}

message = '''From: %(from)s
To: %(to)s
Subject: %(subject)s

Message body

''' % params

sendmail = subprocess.Popen(['/usr/share/sendmail', params['to']])
sendmail.communicate(message)

しかし、実行しようとすると、次のエラーメッセージが表示されます。

Traceback (most recent call last):
  File "/home/me/test.py", line 15, in <module>
    sendmail = subprocess.Popen(['/usr/share/sendmail', params['to']])
  File "/usr/lib/python2.7/subprocess.py", line 679, in __init__
    errread, errwrite)
  File "/usr/lib/python2.7/subprocess.py", line 1249, in _execute_child
    raise child_exception
OSError: [Errno 13] Permission denied

この問題の解決策、またはより良いコードを知っている人はいますか?

ありがとう!

4

5 に答える 5

2

/usr/share/sendmail は非常に珍しいものです。sendmail バイナリが実際にそこにあると確信していますか? 通常は/usr/sbin/sendmailです。

私があなたなら、sendmail を直接呼び出す代わりに、標準ライブラリsmptlibを使用したいと思います。

次のように使用してメッセージを送信できます。

 server = smtplib.SMTP('smtp.example.com')
 server.sendmail(fromaddr, toaddrs, msg)
 server.quit()
于 2012-06-29T06:53:02.817 に答える
2

メールが設定されている場合は、特定のプロセスを呼び出す代わりに、専用のメール ライブラリを直接使用できます。

import smtplib
from email.mime.text import MIMEText

fp = open(textfile, 'rb')
# Create a text/plain message
msg = MIMEText(fp.read())
fp.close()

# Format headers
msg['Subject'] = 'My subject'
msg['From'] = 'from@from.fr'
msg['To'] = 'to@to.com'

# Send the message via Michelin SMTP server, but don't include the envelope header.
s = smtplib.SMTP('your mail server')
s.sendmail('from@from.fr', ['to@to.com'], msg.as_string())
s.quit()

ドキュメントには、さらに多くのpython メールの例があります。

于 2012-06-29T06:54:59.217 に答える
1

smtplibを使用してメールを送信し、TLS/SSLを実行できるコードを次に示します。

import smtplib
from email.MIMEText import MIMEText
from email.utils import parseaddr

class Mailer(object):
    def __init__(self, fromAddress, toAddress, password):
        self.fromAddress = parseaddr(fromAddress)[1]
        self.toAddress = parseaddr(toAddress)[1]
        self.password = password

    def send(self, subject, body):
        msg = MIMEText(body)
        msg["From"] = self.fromAddress
        msg["Reply-to"] = self.toAddress
        msg["To"] = self.toAddress
        msg["Subject"] = subject

        sender = msg["From"]
        recipient = msg["To"]

        messageText = "".join(str(msg))
        mxhost = self.lookup(sender) # lookup finds the host that you want to send to

        server = smtplib.SMTP(mxhost, 587) #port 465 or 587
        server.ehlo()
        server.starttls()
        server.ehlo()
        server.login(sender, self.password)
        server.sendmail(sender, recipient, messageText)
        server.close()
于 2012-06-29T06:58:01.523 に答える
0

私のプログラムは gMail で動作します。それ以外でも試してみることができます。SMS メッセージを送信することもできます。以下にコードを添付します。

import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.base import MIMEBase
from email import encoders
Type = input("Enter 1 for eMail, 2 for SMS: ")
toaddr = 0
if Type=='1':
   toaddr = input("Enter to address: ")
else:
   Provider = input("1 for Sprint, 2 for AT&T, and 3 for Verizon: ")
   Mobile = input("Enter the mobile number: ")
   if Provider=='1': 
      toaddr = str(Mobile) + "@messaging.sprintpcs.com"
   if Provider=='2':
      toaddr = str(Mobile) + '@txt.att.net'
   if Provider=='3':
      toaddr = str(Mobile) + ''
   print (toaddr)      
head = input("Enter your subject: ")
body = input("Enter your message: ")
fromaddr = input("Enter the 'From Address'(example@gmail.com): ")
msg = MIMEMultipart()
msg['From'] = fromaddr
msg['To'] = toaddr
msg['Subject'] = head
password = input("Enter the from address password: ")
msg.attach(MIMEText(body, 'plain'))
server = smtplib.SMTP('smtp.gmail.com', 587)
server.starttls()
server.login(fromaddr, password)
text = msg.as_string()
server.sendmail(fromaddr, toaddr, text)
server.quit()

これが役立つことを願っています。

于 2016-11-08T15:01:02.200 に答える
0

これは、メールを送信するための私のコードです。

#coding: utf-8

import smtplib
from email.mime.text import MIMEText
from email.utils import formatdate

def send_mail(to_list,sub,content):
    mail_host="smtp.example.com"
    mail_user="nvi"
    mail_pass="password"
    mail_postfix="example.com"
    me=mail_user + '555' +"<"+mail_user+"@"+mail_postfix+">"
    msg = MIMEText(content, _subtype='plain', _charset='utf-8')
    msg['Subject'] = sub
    msg['From'] = me
    msg['To'] = to_list
    msg['Date'] = formatdate(localtime=True)
    msg['Bcc'] = '123@example.com'
    try:
        s = smtplib.SMTP()
        s.connect(mail_host)
        s.login(mail_user,mail_pass)
        s.sendmail(me, to_list, msg.as_string())
        s.close()
        return True
    except Exception, e:
        print e
        return False


if __name__ == "__main__":
    send_mail('my_email_address', 'subject', 'content')
于 2014-10-16T12:29:15.033 に答える