Django我想发送电子邮件给已注册的驱动程序?

时间:2018-08-05 17:21:20

标签: django django-forms django-views

我是新手,请参阅发送邮件的文档。我想给司机发电子邮件。在查看他们的详细信息是否符合条件后,我希望他们使用其电子邮件地址发送电子邮件。 driver_email。请告诉我如何发送电子邮件。我已经设置了settings.py电子邮件主机用户的所有需要​​,以发送电子邮件在其他应用程序上的工作。但无法在此应用上正常工作。

this is my page where i want to send an email to the driver with his id Views.py

@csrf_protect
def rentacar_carapp_sendmail(request):
    if request.POST:
        try:
            args['carapp'] = carapp = CarApp.objects.get(id=request.POST.get('carapp_id'))
            subject = "Please Register Your Car"
            from_email = settings.EMAIL_HOST_USER
            to_email = carapp.get('car_app_driver_id.driver_email')
            join_message = """Your booking is placed we will contact you later """
            send_mail(subject=subject, from_email=from_email, recipient_list=[to_email], message=join_message,
                    fail_silently=False)

            carapp.save()
        except:
            pass
        return HttpResponseRedirect('/mega-admin/rentacar/carapp-manager/')

models.py

class Driver(models.Model):
    class Meta():
        db_table = "driver"
        verbose_name = "Driver"
        verbose_name_plural = "Drivers"
        ordering = ['driver_firstname', 'driver_lastname']

    driver_firstname = models.CharField(
        max_length=64,
        blank=False,
        null=False
    )
    driver_lastname = models.CharField(
        max_length=64,
        blank=False,
        null=False
    )
    driver_email = models.EmailField(
        blank=False
    )

class CarApp(models.Model):
    class Meta():
        db_table = "carapp"
        verbose_name = "Carapp"
        verbose_name_plural = "Carapps"

    car_app_driver_id = models.ForeignKey(
        Driver,
        on_delete=models.CASCADE,
        related_name='car_app_driver_key'
    )

    car_app_type_of_car_wanted = models.TextField(
        blank = True,
        null = True
    )

0 个答案:

没有答案