前提
Djangoにて、問い合わせフォームを作成しています。
問い合わせフォームからリクエストに合わせて、
問い合わせを受け付けた旨のメールを送信したいと思っているのですが、
自作したメールクラス内のメール送信メソッドの呼び出し時に、引数エラーが発生しました。
実現したいこと
自作のメールクラス内のメール送信メソッドを正常に呼び出したい
発生している問題・エラーメッセージ
TypeError at /contact-complete/ items() missing 1 required positional argument: 'self'
該当のソースコード
views.pyのsend_mailを呼び出している箇所でエラーが発生しています。
views.py
1import datetime 2import logging 3from django.http import HttpResponseServerError 4from django.shortcuts import render 5from django.views import View 6from django.conf import settings 7from myprojectapp.mail import Mail 8from myprojectapp.recaptcha import Recaptcha 9from .forms import ContactForm 10 11# お問い合わせ完了 12class ContactCompleteView(View): 13 template_name = "contact-complete.html" 14 15 def post(self, request): 16 # リクエスト取得 17 if request is not None: 18 form = ContactForm(request.POST) 19 if form.is_valid(): 20 # reCAPCHAチェック 21 recaptcha_result = Recaptcha().recaptcha_check( 22 recaptcha_response = request.POST.get("g-recaptcha-response"), 23 ) 24 if recaptcha_result == "False": 25 logging.error("recaptcha check error.") 26 return HttpResponseServerError 27 28 # フォーム入力データ取得 29 name = form.cleaned_data["name"] 30 email = form.cleaned_data["email"] 31 title = form.cleaned_data["title"] 32 content = form.cleaned_data["content"] 33 else: 34 logging.error("validation check error.") 35 return render(request, '400.html', status=400) 36 37 else: 38 logging.error("There was no request data..") 39 return HttpResponseServerError 40 41 # メールパラメータセット 42 contact_num = datetime.datetime.now().strftime("%Y%m%d%H%M%S") 43 contact_date = datetime.datetime.now().strftime("%Y年%m月%d日 %H:%M") 44 subject = "お問い合わせを受け付けました" 45 message = """ 46 {name} 様 47 48 お問い合わせありがとうございます。 49 50 下記の内容でお問い合わせを受け付けいたしました。 51 回答にはお時間を頂いております。 52 今しばらくお待ちください。 53 54 ------------------ 55 お問い合わせ番号:{contact_num} 56 お問い合わせ日時:{contact_date} 57 件名:{title} 58 お問い合わせ内容: 59 {content} 60 ------------------ 61 """.format(name=name, contact_num=contact_num, contact_date=contact_date, title=title, content=content) 62 from_email = settings.EMAIL_HOST_USER 63 to_email = list(email) 64 65 # メール送信 66 try: 67 Mail().send_mail( 68 subject = subject, 69 message = message, 70 from_email = from_email, 71 to_email = to_email, 72 ) 73 except: 74 logging.error("Email could not be sent.") 75 return HttpResponseServerError 76 77 return render(request, self.template_name)
mail.py
1from django.core.mail import send_mail 2 3class Mail(): 4 def send_mail(self, **kwargs): 5 subject = kwargs["subject"] 6 message = kwargs["message"] 7 from_email = kwargs["from_email"] 8 to = kwargs["to_email"] 9 send_mail(subject, message, from_email, to)
試したこと
Mailクラスをインスタンス化せずに呼び出すことは試しましたが、改善はありませんでした。
補足情報(FW/ツールのバージョンなど)
実行環境:M1 Mac バージョン12.5
Django:4.0.6
Python:3.9.13
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2022/08/16 01:56
2022/08/16 03:51
2022/08/16 10:38