2017-01-04 6 views
0

電子メール通知を送信しようとしています。動的データを送信したいのです(テスト目的のために辞書は静的です)。どのように私は私の辞書の中で1つの値だけを電子メールに表示トラフを繰り返す。どんな助けでも大歓迎です!!!ここで pythonでdictを繰り返してHTMLメールで使用することは可能ですか?

は私が得る電子メール私のemail.pyここ

import smtplib 

from email.mime.multipart import MIMEMultipart 
from email.mime.text import MIMEText 

# me == my email address 
# you == recipient's email address 
me = "[email protected]" 
you = "[email protected]" 

msg = MIMEMultipart('alternative') 
msg['Subject'] = "Link" 
msg['From'] = me 
msg['To'] = you 

justin = "Justin" 

p = {u'Contracts that need signed by: Justin': [[u'/contracts/26203', u'/contracts/26194', u'/contracts/26199', u'/contracts/26173']]} 


for a in p.keys(): 
    b = p['Contracts that need signed by: Justin'] 
    for c, d in enumerate(b): 
    e = d 
    for f in e: 
     html = """\ 
     <html> 
     <head></head> 
     <body> 
      <p>Contracts that need signed by """ + justin + """<br> 
      How are you?<br> 
      Here is the <a href="http://contract.mydomain.com/""" + f + """">link</a> you wanted. 
      </p> 
     </body> 
     </html> 
     """ 

part2 = MIMEText(html, 'html') 

msg.attach(part2) 

s = smtplib.SMTP('localhost') 
s.sendmail(me, you, msg.as_string()) 
s.quit() 

です^私はそれは問題があなたにあるこの

Contracts that need signed by Justin 
How are you? 
#EACH OF THE LINKS WOULD BE THE NEXT VALUE IN MY DICTONARY. 
Here is the link1 you wanted. 
Here is the link2 you wanted. 
Here is the link3 you wanted. 
Here is the link4 you wanted. 
+0

それがpをループに少し奇妙であると、常に各反復( 'B =のp [用のpで同じエントリを使用します。.. 。])。 – Moberg

答えて

2

あなたは何度も何度もHTMLの変数を上書きしています。 次のようなループのために常に1行を追加する必要があります。

justin = "Justin" 

p = {u'Contracts that need signed by: Justin': [[u'/contracts/26203', u'/contracts/26194', u'/contracts/26199', u'/contracts/26173']]} 


for a in p.keys(): 
    b = p['Contracts that need signed by: Justin'] 
    for c, d in enumerate(b): 
     e = d 
     html = """\ 
<html> 
<head></head> 
<body> 
<p>Contracts that need signed by """ + justin + """<br> 
How are you?<br> 
""" 
     for f in e: 
      html = html + """Here is the <a href="http://contract.mydomain.com/""" + f + """">link</a> you wanted. \n""" 
     html = html + """</p> 
</body> 
</html> 
""" 


part2 = MIMEText(html, 'html') 

print part2 
+0

ありがとう@ mudo121 !!! – Snowman08

+0

プロブレムはありません。おそらく、私の下のanwserのようにコードをもっときれいにすることができます:D – mudo121

1

に見えるようにしたい

Contracts that need signed by Justin 
How are you? 
Here is the link you wanted. 

各反復で変数htmlを上書きします。 あなたの問題のためにstring formattingを使用することができます。

for c, d in enumerate(b): 
    e = d 
    html = """\ 
     <html> 
     <head></head> 
     <body> 
      <p>Contracts that need signed by """ + justin + """<br> 
      How are you?<br> 
      {0} 
      </p> 
     </body> 
     </html> 
     """ 
    s = [] 
    for f in e: 
     print(f) 
     s.append('Here is the <a href="http://contract.mydomain.com/{0}>link</a> you wanted.'.format(f)) 
    print(s) 
html = html.format('\n'.join(s)) 

print(html) 

出力:

<html> 
    <head></head> 
    <body> 
     <p>Contracts that need signed by aaa<br> 
     How are you?<br> 
     Here is the <a href="http://contract.mydomain.com//contracts/26203>link</a> you wanted. 
     Here is the <a href="http://contract.mydomain.com//contracts/26194>link</a> you wanted. 
     Here is the <a href="http://contract.mydomain.com//contracts/26199>link</a> you wanted. 
     Here is the <a href="http://contract.mydomain.com//contracts/26173>link</a> you wanted. 
     </p> 
    </body> 
    </html> 

関連する問題