0

i am beginner and used steps for sending email from https://docs.djangoproject.com/en/2.0/topics/email/ but i didn't accomplished to send email.

i want to send an email automatically using django email after a user submit the form . i am having a booking form and having email field in it after user post the form i want to send a email "Thankyou for your booking / your booking is placed ".

for eg

  1. first name : Abubakar
  2. Last name :Afzal
  3. email : any@gmail.com

i want to take any@gmail.com and send email to it . after user post the form .

settings.py

EMAIL_HOST = 'smtp.gmail.com'
EMAIL_PORT = 587
EMAIL_HOST_USER = 'my@gmail.com'
EMAIL_HOST_PASSWORD = 'mypassword'
EMAIL_USE_TLS = True

View.py

     class BookingView(FormView):
         template_name = 'buggy_app/booking.html'
         form_class = BookingForm
         models = Booking
         def form_valid(self, form):
             car_id = self.request.GET.get('car', '')
             car = Car.objects.get(id=car_id)
             car.is_available_car = False
             car.save()
             form.save()
             return super(BookingView, self).form_valid(form)
        success_url = reverse_lazy('index')

Forms.py

      class BookingForm(ModelForm):

         class Meta:
            model = Booking
             widgets = {
             times_pick': TimePickerInput(),  }
             fields = ('first_name','last_name','email','book_car','contact_number','times_pick',)

2 Answers2

0

You can define a function called send_emails (or any name). and call taht function from within form_valid method. It will look something like this

   def form_valid(self, form):
        car_id = self.request.GET.get('car', '')
        car = Car.objects.get(id=car_id)
        car.is_available_car = False
        car.save()
        form.save()
        form.cleaned_data.get('username')
        first_name = form.cleaned_data.get('first_name')
        last_name = form.cleaned_data.get('last_name')
        to_email = form.cleaned_data.get('email')

        #your function here
        send_emails(first_name, last_name, to_email)

and then define function something like this.

def send_emails(first_name, last_name, to_email):
    #get template
    htmly = get_template('email_templates/welcome.html')
    #create a context
    d = {'first_name':first_name, 'last_name':last_name}
    subject, from_email, to = 'Subject line', settings.EMAIL_HOST_USER, to_email
    #pass the context to html template
    html_content = htmly.render(d)
    msg = EmailMultiAlternatives(subject, html_content, from_email, [to])
    msg.attach_alternative(html_content, "text/html")
    msg.send()
Ojas Kale
  • 2,067
  • 2
  • 24
  • 39
0
class BookingView(FormView):
     template_name = 'buggy_app/booking.html'
     form_class = BookingForm
     models = Booking
     def form_valid(self, form):
         car_id = self.request.GET.get('car', '')
         car = Car.objects.get(id=car_id)
         car.is_available_car = False
         car.save()
         form.save()
         subject = 'Thankyou for your booking / your booking is placed'
         message = 'Thankyou for your booking / your booking is placed'
         email_from = settings.EMAIL_HOST_USER
         recipient_list = [any@gmail.com]
         send_mail( subject, message, email_from, recipient_list )
         return super(BookingView, self).form_valid(form)
    success_url = reverse_lazy('index')
Subha ss
  • 1
  • 1
  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Muhammad Dyas Yaskur Dec 18 '21 at 10:01