إرسال البريد عبر sendmail من بايثون
سؤال
إذا كنت أرغب في إرسال بريد ليس عبر SMTP، بل عبر sendmail، فهل توجد مكتبة لبيثون تتضمن هذه العملية؟
والأفضل من ذلك، هل هناك مكتبة جيدة تلخص خيار "sendmail -versus- smtp" بالكامل؟
سأقوم بتشغيل هذا البرنامج النصي على مجموعة من مضيفات يونكس، بعضها فقط يستمع على المضيف المحلي:25؛عدد قليل منها جزء من الأنظمة المضمنة ولا يمكن إعدادها لقبول SMTP.
كجزء من الممارسة الجيدة، أود حقًا أن تعتني المكتبة بثغرات حقن الرأس بنفسها - لذلك فقط قم بإلقاء سلسلة إلى popen('/usr/bin/sendmail', 'w')
هو أقرب قليلا إلى المعدن مما أريد.
إذا كان الجواب "اذهب واكتب مكتبة"، فليكن؛-)
المحلول
لا يعد إدخال الرأس عاملاً في كيفية إرسال البريد، بل هو عامل في كيفية إنشاء البريد.افحص ال بريد إلكتروني الحزمة، وإنشاء البريد باستخدام ذلك، وتسلسله، وإرساله إلى /usr/sbin/sendmail
باستخدام عملية فرعية وحدة:
from email.mime.text import MIMEText
from subprocess import Popen, PIPE
msg = MIMEText("Here is the body of my message")
msg["From"] = "me@example.com"
msg["To"] = "you@example.com"
msg["Subject"] = "This is the subject."
p = Popen(["/usr/sbin/sendmail", "-t", "-oi"], stdin=PIPE)
p.communicate(msg.as_string())
نصائح أخرى
هذه وظيفة بايثون بسيطة تستخدم نظام Unix sendmail لتسليم البريد.
def sendMail():
sendmail_location = "/usr/sbin/sendmail" # sendmail location
p = os.popen("%s -t" % sendmail_location, "w")
p.write("From: %s\n" % "from@somewhere.com")
p.write("To: %s\n" % "to@somewhereelse.com")
p.write("Subject: thesubject\n")
p.write("\n") # blank line separating headers from body
p.write("body of the mail")
status = p.close()
if status != 0:
print "Sendmail exit status", status
لم تنجح إجابة جيم بالنسبة لي في Python 3.4.كان علي أن أضيف إضافية universal_newlines=True
حجة ل subrocess.Popen()
from email.mime.text import MIMEText
from subprocess import Popen, PIPE
msg = MIMEText("Here is the body of my message")
msg["From"] = "me@example.com"
msg["To"] = "you@example.com"
msg["Subject"] = "This is the subject."
p = Popen(["/usr/sbin/sendmail", "-t", "-oi"], stdin=PIPE, universal_newlines=True)
p.communicate(msg.as_string())
بدون ال universal_newlines=True
انا حصلت
TypeError: 'str' does not support the buffer interface
من الشائع جدًا استخدام أمر sendmail من Python باستخدام os.popen
شخصيًا، بالنسبة للنصوص البرمجية التي لم أكتبها بنفسي، أعتقد أن مجرد استخدام بروتوكول SMTP هو الأفضل، لأنه لن يتطلب تثبيت نسخة بريد إلكتروني على سبيل المثال للتشغيل على النوافذ.
كنت أبحث للتو عن نفس الشيء ووجدت مثالًا جيدًا على موقع Python الإلكتروني: http://docs.python.org/2/library/email-examples.html
من الموقع المذكور :
# Import smtplib for the actual sending function
import smtplib
# Import the email modules we'll need
from email.mime.text import MIMEText
# Open a plain text file for reading. For this example, assume that
# the text file contains only ASCII characters.
fp = open(textfile, 'rb')
# Create a text/plain message
msg = MIMEText(fp.read())
fp.close()
# me == the sender's email address
# you == the recipient's email address
msg['Subject'] = 'The contents of %s' % textfile
msg['From'] = me
msg['To'] = you
# Send the message via our own SMTP server, but don't include the
# envelope header.
s = smtplib.SMTP('localhost')
s.sendmail(me, [you], msg.as_string())
s.quit()
لاحظ أن هذا يتطلب إعداد sendmail/mailx بشكل صحيح لقبول الاتصالات على "المضيف المحلي".يعمل هذا على خوادم Mac وUbuntu وRedhat بشكل افتراضي، ولكن قد ترغب في التحقق مرة أخرى إذا واجهت أي مشكلات.
الإجابة الأسهل هي smtplib، حيث يمكنك العثور على المستندات عليه هنا.
كل ما عليك فعله هو تكوين بريدك الإلكتروني المحلي لقبول الاتصال من المضيف المحلي، وهو ما يفعله بالفعل افتراضيًا.من المؤكد أنك لا تزال تستخدم SMTP للنقل، ولكنه البريد الإلكتروني المحلي، وهو في الأساس نفس استخدام أداة سطر الأوامر.