2016-09-23 6 views
0

Django EmailMultiAlternativesでメールを送信しようとしているときに、このエラー:linkが発生しました。私はこのエラーを検索しようとしましたが運がない、電子メールのすべての変数を削除または変更しようとしましたが、運はありませんでした。電子メールを送信中に文字列インデックスが範囲外にある

これはコードです:

def spremembapodatkovproc(request): 
    if request.method == 'POST': 
     req_id = request.POST.get('req_num', 'Neznan ID zahtevka') 
     old_email = request.user.email 
     old_name = request.user.get_full_name 
     new_email = request.POST.get('email_new', 'Nov e-mail ni znan') 
     new_fname = request.POST.get('fname_new', 'Novo ime ni znano') 
     dokument = request.FILES.get('doc_file') 
     komentar = request.POST.get('comment', 'Ni komentarja') 
     # try: 
     plaintext = get_template('email/usr-data-change.txt') 
     htmly = get_template('email/usr-data-change.html') 

     d = Context(
      { 
       'old_email': old_email, 
       'old_fname': old_name, 
       'new_email': new_email, 
       'new_fname': new_fname, 
       'req_id': req_id, 
       'komentar': komentar, 
       'user_ip': request.META.get('REMOTE_ADDR', 'IP Naslova ni mogoče pridobiti.') 
      } 
     ) 

     subject, from_email, to = 'eBlagajna Sprememba podatkov', '[email protected]', ["[email protected]"] 
     text_content = plaintext.render(d) 
     html_content = htmly.render(d) 
     print(text_content) 
     msg = EmailMultiAlternatives(subject, text_content, from_email, [to]) 
     msg.attach_alternative(html_content, "text/html") 

     msg.mixed_subtype = 'related' 

     for f in ["templates\\email\\img1.png"]: 
      fp = open(os.path.join(BASE_DIR, f), 'rb') 
      msg_img = MIMEImage(fp.read()) 
      fp.close() 
      msg_img.add_header('Content-ID', '<{}>'.format(f)) 
      msg.attach(msg_img) 
     msg.send() 

はあなたの助けをいただき、ありがとうございます。

+0

'EmailMultiAlternatives'インスタンスを初期化している間は、' to = ["[email protected]"] 'と' [] 'カッコで囲んでみようとしています。 'EmailMultiAlternatives(subject、text_content、from_email、[to])'を 'EmailMultiAlternatives(subject、text_content、from_email、to) 'に変更しようとしてください。 –

+0

ありがとう!できます。 – AndyTemple

答えて

3

問題は、別のリストにある電子メールの重複するリストをラップすることで問題でした。ラインの実行

msg = EmailMultiAlternatives(subject, text_content, from_email, [to]) 

はそれが[ ]ブラケットとto 1より多くの時間を包んだ後、したがって、基本的変数to = ["[email protected]"]

、。 [to] = [["[email protected]"]]ですが、単純なリストであると考えられています。だから、問題行を

に変更すると、
msg = EmailMultiAlternatives(subject, text_content, from_email, to) 

すべてが機能しました。

関連する問題