PyPosta
Eski python deneme kodlarına göz atarken e-posta gönderimi ile ilgili bir dosya buldum. Biraz uğraşma ile bir modül haline getirdim. Eksik yönleri çok ama basit bir şekilde e-posta gönderimi ve okuma işlemlerini gerçekleştirebiliyor. Kodlara buradan ve aşağıdan ulaşabilirsiniz.
PyPosta.py içeriği:
# -*- coding: utf-8 -*- """ {PyPosta modülü e-posta gönderip almanızı sağlar} {Test Sürümü} Copyright (C) {2007} {Ömer Ücel} omerucel@gmail.com This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA """ import sys import poplib import rfc822 import StringIO from smtplib import SMTP from email.Header import Header from email.Utils import formataddr from email.MIMEText import MIMEText class PyPosta: def __init__(self,sunucu,hesap,parola): # E-Posta işlemleri için sunucu adresi. self.sunucu = sunucu # Sunucu üzerindeki kullanıcı adı ya da e-posta adresi. self.hesap = hesap # Kullanıcı adı ya da e-posta adresi parolası. self.parola = parola ######## # E-Posta gönderim işlemlerinde kullanılır. self.gisim = "" # Gönderen kişinin ismi. self.gadres = "" # Gönderen kişinin e-posta adresi. self.aisim = "" # Alan kişinin ismi. self.aadres = "" # Alan kişinin e-posta adresi. self.baslik = "" # E-Posta başlığı. self.icerik = "" # E-Posta içeriği. ######### self.idurum = False self.ihata = False self.imesaj = "" def gonder(self,format,karset): self.karset = karset # Karakter seti self.format = format # html,text if self.format <> "html" and self.format <> "text": self.idurum = False self.ihata = True self.imesaj = "Uyarı 1 :Lütfen doğru bir format belirtiniz." if (self.gadres == "" or self.aadres == "") and self.ihata == False: self.idurum = False self.ihata = True self.imesaj = "Uyarı 2 : E-Postayı gönderecek ve/veya alacak olan kişinin e-posta adresleri belirtilmelidir." if self.ihata == False: # İçerik oluşturuluyor try: self.dicerik = MIMEText(self.icerik, self.format, self.karset) self.dicerik['From'] = formataddr((self.gisim, self.gadres)) self.dicerik['To'] = formataddr((self.aisim, self.aadres)) self.dicerik['Subject'] = Header(self.baslik, self.karset) except Exception, hata: self.idurum = False self.ihata = True self.imesaj = "Hata 1 : %s" %(hata) # smtp servisi ile gönderim işlemi yapılıyor. if self.ihata==False: try: smtp = SMTP(self.sunucu) smtp.login(self.hesap,self.parola) smtp.sendmail(self.gadres, self.aadres, self.dicerik.as_string()) smtp.quit() self.idurum = True self.ihata = False self.imesaj = "E-Posta gönderim işlemi başarı ile gerçekleştirildi." except Exception, hata: self.idurum = False self.ihata = True self.imesaj = "Hata 2 : %s" %(hata) def al(self): # Bağlantı yapılıyor. try: self.pop3 = poplib.POP3(self.sunucu) self.pop3.user(self.hesap) self.pop3.pass_(self.parola) except Exception, hata: self.idurum = False self.ihata = True self.imesaj = "Hata 3 : %s" %(hata) # Kutuda bulunan e-posta sayısı alınıyor. if self.ihata == False: try: eposta_toplam = len(self.pop3.list()[1]) except Exception, hata: self.idurum = False self.ihata = True self.imesaj = "Hata 5 : %s" %(hata) # Kutuda bulunan e-postalar okuma işlemi listeleniyor. if self.ihata == False: cozumlenen_icerik = [] for i in range(eposta_toplam): # E-Posta alınıyor resp, eposta_icerik, oct =self.pop3.retr(i+1) # Çözümleme işlemi yapılıyor cozumlenen = StringIO.StringIO("\n".join(eposta_icerik)) cozumlenen = rfc822.Message(cozumlenen) cozumlenen_icerik.append({ "baslik":cozumlenen["subject"],"icerik":cozumlenen.fp.read()}) # Listeleme yapılıyor print "%d ) %s" %(i+1,cozumlenen["subject"]) iptal = False while iptal==False: oku = raw_input("Oku >> ") if oku<>"": if oku=="cikis": iptal = True break else: print cozumlenen_icerik[int(oku)-1]["baslik"] print "==============================" print cozumlenen_icerik[int(oku)-1]["icerik"] if self.ihata == False: try: self.pop3.quit() except Exception, hata: self.idurum = False self.ihata = True self.imesaj = "Hata 4 : %s" %(hata)
ornek.py içeriği:
# -*- coding: utf-8 -*- from PyPosta import * # PyPosta(Sunucu,Hesap,Parola) şeklinde kullanılır. eposta = PyPosta("Sunucu","hesap","parola") #------------------------ # E-Posta Gönderim işlemi #------------------------ # Gönderen İsmi eposta.gisim = "Gönderen İsmi" # Gönderen Adresi eposta.gadres = "Gönderen E-Posta adresi gonderen@adresi.com" # Alan İsmi eposta.aisim = "Alıcı İsmi" # Alan Adresi eposta.aadres = "Alıcı E-Posta adresi alici@adresi.com" # Başlık eposta.baslik = "Başlık" # İçerik eposta.icerik = "İçerik" # html,text posta türü, utf-8 karakter seti eposta.gonder("html","utf-8") # eposta.imesaj : Mesaj geri dönüşü için, hata ya da işlem tamamlanma mesajları. # eposta.ihata : Hata var mı? Yok mu ? True ya da False döner. # eposta.idurum : İşlem yapıldı mı? Yapılmadı mı? True ya da False döner. print eposta.imesaj #------------------------ #------------------------ # E-Posta Alma İşlemi # eposta.al() Tüm epostaları listeleyip okuma motorunu çalıştırır. cikis yazarak okuma bölümünden çıkabilirsiniz. eposta.al() # eposta.imesaj : Mesaj geri dönüşü için, hata ya da işlem tamamlanma mesajları. # eposta.ihata : Hata var mı? Yok mu ? True ya da False döner. # eposta.idurum : İşlem yapıldı mı? Yapılmadı mı? True ya da False döner. print eposta.imesaj #------------------------
Hocam değişik ve güzel bir site yapmışsınız,tebrik ediyorum
Comment by Yurtdışı Eğitim — February 17, 2008 @ 2:20 pm