Phyton ile mail gönderme - Phyton Webmaster bilgi bankası, knowledge base Webmaster Araçları

AnaSayfa > Phyton > Phyton ile mail gönderme
Kategori: Phyton
Gönderen: Admin
Tarih: 2011-02-20
Puan: 3   Puan:3 | Katılımcı:15 | Katılımcı : 15
Okunma: 15224
  
 
1 puan 2 puan 3 puan 4 puan 5 puan 6 puan 7 puan 8 puan 9 puan 10 puan

İçine resim gömülü HTML mail gönderir. SMTP sunucunun, localhost'ta kurulu olduğu ve yerel kullanıcılar için parola istemediği farzedilmiştir.

#!/usr/bin/python
# -*- coding: iso-8859-9; -*-

import smtplib
from email.MIMEMultipart import MIMEMultipart
from email.MIMEImage import MIMEImage
from email.MIMEText import MIMEText

KIMDEN = 'Gonderen <gonderen@domain.com>'
RESIM = 'resim.jpg'



# ----------------------------------------------
# bu fonksiyon maili gonderiyor
def mail_gonder(kimden, kime, konu, metin, dosya, server="localhost" ):
msg = MIMEMultipart('related' )
msg['Subject'] = konu
msg['From'] = kimden
msg['To'] = kime
#msg.preamble = 'mutlu'

msgAlt = MIMEMultipart('alternative' )
msg.attach(msgAlt )

msgTxt = MIMEText(metin )
msgTxt.add_header('Content-Type', 'text/html; charset="iso-8859-9"' )
msgAlt.attach(msgTxt )

msgTxt = MIMEText('<a href="http://www.domain.com"><img src="cid:image1"></a>', 'html' )
msgAlt.attach(msgTxt )

f = open(dosya, 'rb' )
img = MIMEImage(f.read( ) )
f.close( )
img.add_header('Content-ID', '<image1>' )
msg.attach(img )

ss = smtplib.SMTP(server )
ss.sendmail(kimden, kime, msg.as_string( ) )
ss.close( )



# ----------------------------------------------
# programin calismaya basladigi bolum

kime = 'alici@domain.com'
konu = 'resim gömülü HTML mail'
metin = 'HTML görüntüleyemeyenler için text formatında metin'

mail_gonder(KIMDEN, kime, konu, metin, RESIM )




Yorumlar
Henüz Kimse Yorum Yapmamış, ilk yorumu siz ekleyin!
Yorum Ekleme Aparatı
Yorum Eklemek için lütfen sol menuden giris yapınız..
Toplam 0 yorum listelendi.