Python İle E-Posta Gönderimi (Windows)
-
çoğu zaman python gibi bir betik(scripting) dilinin, çalışması sonucu oluşturacağı raporu e-posta ile almak isteriz. Fakat internette bulduğumuz çoğu kod, bu işin üstesinden bir türlü gelemez. Biz e-posta yollamak için Microsoft'un serbest olarak dağıttığı "CDO" com objesi'ni kullanacağız. Ve bu "gerçekten" çalışıyor:)
CDO ile ilgili: http://msdn2.microsoft.com/en-us/library/ms978698.aspx
com objeleri ile ilgili: http://www.google.com
ek olarak kendi makinanıza da bir posta sunucusu kurmanız gerekli.
aslında google'da "CDO" + "Python" şeklinde bir aramayla kodlara ulaşabilecek olsak da bu aramaya gerek olmaması için kodları aşağıya ekleyeyim.
Örnek kodumuzu aşağıdaki gibi yazıp deneyelim:
#win com objelerini python'dan kullanabilmek için win32com.client kullanacağız
import win32com.client
# sendEmail fonksiyonumuzu yazalım
def SendEmail(emailAddresses, subject, body, files,mailServer,mailPort,authNeeded,accountName, sendName,accountPwd):
try :
newConf = win32com.client.Dispatch("CDO.Configuration")
Flds = newConf.Fields
Flds("http://schemas.microsoft.com/cdo/configuration/smtpserver").Value =mailServer
Flds("http://schemas.microsoft.com/cdo/configuration/smtpserverport").Value = mailPort
Flds("http://schemas.microsoft.com/cdo/configuration/sendusing").Value = 2 # cdoSendUsingPort
# Authentication gerekmiyorsa aşağıya 0 göndermeliyiz
Flds('http://schemas.microsoft.com/cdo/configuration/smtpauthenticate').Value = authNeeded
# yukarıdaki authentication fieldi 1 veya 2 yapılmışsa alttaki alanlar doldurulmalı
Flds('http://schemas.microsoft.com/cdo/configuration/smtpaccountname').Value = accountName
Flds('http://schemas.microsoft.com/cdo/configuration/sendusername').Value = sendName
Flds('http://schemas.microsoft.com/cdo/configuration/sendpassword').Value = accountPwdFlds.Update()
newMsg = win32com.client.Dispatch("CDO.Message")
newMsg.Configuration = newConf
newMsg.To = ";".join(emailAddresses)
newMsg.From = "Otomatik Posta" # seni @seviyorum.esra daha güzel olabilir:)
newMsg.Subject = subject
newMsg.TextBody = body# eğer belirtilen listede dosya(lar) varsa onları da eklemeliyiz
try:
for file in files:
newMsg.AddAttachment(file)
except Exception, eee:
newMsg.TextBody += "HATA: Dosya Ekleme Hatası " + str(eee.args)
newMsg.Send()
print "Ileti Gonderildi !!"
return True
except Exception, eee:
print str(eee.args)
return False# şimdi send_mail fonksiyonumuzu çağıralım
def postaAt():
file1 = "file:///" + os.path.abspath("c:\ismailbeygaspiralininhayati.txt")
file2 = "file:///" + os.path.abspath("c:\nutuk.txt")
files = [file1, file2]
postaAdresleri = ["yasa@varol.harbiye.com","eposta2@epostaadresi.com" ]# aşağıda <> ifadeleri arasında bulunan değerler, posta sunucunuzla ilgili değerlerdir. posta sunucunuzu biraz kurcalayarak çözebilirsiniz.
SendEmail(postaAdresleri, "mesaj basligi: Her Sey Vatan Icin", "Mesaj icerigi: Vatan Borcu Namustur", files, , , <1->auth needed>, , , )
postaAt() -
peki phytonu localde nası calıstırcaz ?
-
http://www.python.org adresinden windows için olanı indirip kurduktan sonra, python dosyasını herhangi bir editörle hazırlayıp ".py" uzantılı olarak kaydederek çalıştırabilirsin.
-
kodlar çalışmıytor.
-
phyton unix tabanlı bir dil windows altında tam verim alarak çalışabileceğinizi zannetmiyorum windows uygulmaları geliştiriyorsanız microsoft dillerini öneririm c# vb gibi