zogo
zogo

Reputation: 515

sending email when clicking a link

I am relatively new to Django. I am developing my own website where in one portion, I want to use referral code. I have successfully generated unique referral codes. In mention,each referral code is storied in the database with the associated user while the user signing up in my website; that's okay. But I want to send this unique referral code with an email.In the user profile page ,a simple anchor tag will be given to the user and will asked to click the link to get an email.The email will contain the referral code of the associated user.

as example...

{% extends 'base.html' %}
{% block title%}
 Profile page
{% endblock %}
{%block content%}
<!-- ==== Referral Code ==== -->
    <div id="greywrap">

        <a href = {% %}>Click here to get your referral code to your email</a>
    </div>






 {% endblock %}

here you can see that there is a anchor tag where the user is asked for click the link to get the email.Now how can i integrate the send_email() function with the link to send an email.i know that the send_email is used to send email.please help me.

Upvotes: 1

Views: 347

Answers (2)

Kiddo
Kiddo

Reputation: 158

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', '[email protected]' ['[email protected]'], 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.

Upvotes: 0

wavemode
wavemode

Reputation: 2116

You can use the Django function send_mail:

from django.core.mail import send_mail

send_mail('Subject here', 'Here is the message.', '[email protected]',
    ['[email protected]'], fail_silently=False)

Upvotes: 2

Related Questions