[Answered ]-Sending email when clicking a link

0πŸ‘

βœ…

In order to properly send the e-mail by clicking on a link in the template, you should do the following:

  1. Create a method in your view.

    In this method you may need to do something to obtain the user ID as an input (from the HTTP request, probably) and get the referral code from the database; then send the e-mail with the code provided by wavemode. This would mean something similar to:

    def send_referral_code(request):
    user = request.user
    # Do some work to obtain the referral code from the request.user object
    # Do some work to obtain the e-mail from the request.user object
    from django.core.mail import send_mail
    send_mail('Subject here', 'Message with referral code', 'from@example.com' ['to@example.com'], fail_silently=False)

  2. Link this method from the urls file in your app.

    The following is untested, but should give you an idea: url(r'^send_referral_code/$', 'user.views.send_referral_code', name='send_referral_code'). This means that the method send_referral_code can be found in the views module, inside the user app.

  3. Refer the previous url from your template.

    This is done with a simple {% url send_referral_code %}, this time inside the href of your link.

Note that this assumes that no input parameter is passed. If you needed some, be sure to add it to the view and urls and finally pass it as the argument in the template, right after the method name (between the %β€˜s)

There are several basic tutorials, e.g.: https://docs.djangoproject.com/en/dev/intro/tutorial03/#write-your-first-view. You may find examples of url’s defined with parameters in there.

πŸ‘€Kiddo

2πŸ‘

You can use the Django function send_mail:

from django.core.mail import send_mail

send_mail('Subject here', 'Here is the message.', 'from@example.com',
    ['to@example.com'], fail_silently=False)
πŸ‘€wavemode

Leave a comment