satchmo password_reset html format mail - django

Ive been requested by a client that his satchmo store should send an html formatted mail when resetting his password.
Aparently satchmo or django's contrib.auth.views.password_reset sends only raw email.
How do I modify this in order to be able to send html formatted mails?
Thank you!

I haven't used Satchmo, but this should get you started.
First of all, subclass the PasswordResetForm, and override the save method to send an html email instead of a plain text email.
from django.contrib.auth.forms import PasswordResetForm
class HTMLPasswordResetForm(PasswordResetForm):
def save(self, domain_override=None, email_template_name='registration/password_reset_email.html',
use_https=False, token_generator=default_token_generator, from_email=None, request=None):
"""
Generates a one-use only link for resetting password and sends to the user
"""
# Left as an exercise to the reader
You can use the existing PasswordResetForm as a guide. You need replace the send_mail call at the end with code to send html emails. The docs about sending html emails should help.
Once you've written your form, you need to include the form in the url pattern for password_reset. As I said, I don't have any experience of Satchmo, but looking at the source code, I think you want to update satchmo_store.accounts.urls, by changing the password_reset_dict.
# You need to import your form, or define it in this module
from myapp.forms import HTMLPasswordResetForm
#Dictionary for authentication views
password_reset_dict = {
'template_name': 'registration/password_reset_form.html',
# You might want the change the email template to .html
'email_template_name': 'registration/password_reset.txt',
'password_reset_form': HTMLPasswordResetForm,
}
# the "from email" in password reset is problematic... it is hard coded as None
urlpatterns += patterns('django.contrib.auth.views',
(r'^password_reset/$', 'password_reset', password_reset_dict, 'auth_password_reset'),
...

Related

Create an email in django with an HTML body and includes an attachment

Can I send an html formatted document as the body of an email and include an attachment?
send_mail() has an option for html_message but the EmailMessage class does not.
My understanding is that to send an attachment, I need to use the EmailMessage class to use the attach_file method.
Am I missing something? I thought send_mail() uses the EmailMessage class, so why do these two features seem mutually exclusive?
Check out EmailMultiAlternatives it has an attach function that you can use for this purpose.
Here's an example of how to use it:
from django.core.mail import EmailMultiAlternatives
subject = request.POST.get('subject', '')
message = request.POST.get('message', '').encode("utf-8")
# Create an e-mail
email_message = EmailMultiAlternatives(
subject=subject,
body=message,
from_email=conn.username,
to=recipents,
bcc=bcc_recipents, # ['bcc#example.com'],
cc=cc_recipents,
headers = {'Reply-To': request.user.email},
connection = connection
)
email_message.attach_alternative(message, "text/html")
for a in matachments:
email_message.attach(a.name, a.document.read())
email_message.send()
I see that to have an html email, I can have html as the body, but I just need to change the content_subtype to html.
msg_body.content_subtype = "html"
Thanks for your help, it got me back to the right page to read in better detail.

Sending mail chimp designed emails using django

I want to send a mail to the subscribers of a django website. So, as soon as someone registers they should be sent a mail. I can do this by the send_mail function that django has but I want to send a designed mail (like with a mail chimp template). Any ideas on how to do this? I read through the api documentation and understood how to send mails but I cannot understand how to integrate its own template and stuff.
Any advice will be helpful. Thanks!
MailChimp templates are just HTML. If you are using Django 1.7+, send_email supports html_message (ref).
You can use the render_to_string (see here) function to output the appropriate HTML, which will be fed into send_email. Combining these two, you can first set up your HTML template:
html_template = render_to_string('templates/my_email.html', {'foo': 'bar'})
Then create my_email.html and put the HTML generated on MailChimp into that file.
You can now call send_mail with the new HTML template:
send_mail(
[...]
html_message=html_template,
)
EDIT: Full example:
from django.core.mail import send_mail
from django.template.loader import render_to_string
html_template = render_to_string('templates/my_email.html', {'foo': 'bar'})
send_mail(
'Title',
'Plaintext Version Goes Here',
'sender#sender.com',
['receiver#receiver.com'],
html_message=html_template,
)

django User registration and authentication by email

I want to make users active by sending them an activation email to click. I guess it is currently not incorporated in Django 1.6.The user-registration app coded in Django seems to serve this purpose. But I have some doubts with regard to the DefaultForm it provides in forms.py. I want to have more fields included in it. How can I achieve that in class RegistrationForm(forms.Form) implemented there. If I install this app, is it a good idea to change include more fields directly there, is there a better way to achieve the same.
In the views.py, I see some methods such as the following are not implemented. I dont have a clear picture of what these methods need to do. should I redirect the url here to the pages?
def register(self, request, **cleaned_data):
raise NotImplementedError
def activate(self, request, *args, **kwargs):
raise NotImplementedError
def get_success_url(self, request, user):
raise NotImplementedError
You need to first let them sign up and mark them as is_active=False for the time being. Something like this:
from django.contrib.auth.models import User
from django.core.mail import send_mail
from django.http import HttpResponseRedirect
def signup(request):
# form to sign up is valid
user = User.objects.create_user('username', 'email', 'password')
user.is_active=False
user.save()
# now send them an email with a link in order to activate their user account
# you can also use an html django email template to send the email instead
# if you want
send_mail('subject', 'msg [include activation link to View here to activate account]', 'from_email', ['to_email'], fail_silently=False)
return HttpResponseRedirect('register_success_view')
Then once they click the link in the email it takes them to the next view (note: you need to put a link in the email so that you know which user it is. This may be 16-digit salt or something. The below view uses the user.pk:
def activate_view(request, pk):
user = User.objects.get(pk=pk)
user.is_active=True
user.save()
return HttpResponseRedirect('activation_success_view')
Hope that helps. Good Luck!
Basically you can use django's user model(https://docs.djangoproject.com/en/1.9/ref/contrib/auth/). However, in user model email is not a required field. You need to modify the model to make email an required field.
In your views, you might need the following method:
1) Sign up: after sign up, set user.is_active=False and call function send_email to include an activation link in the email. In the link, you might want to include the user's information (for example, user.id) so when the user click the link, you know which user to activate.
2) send_email: send a link to user's email address for verification. The link includes user's id. For example:
http://127.0.0.1:8000/activation/?id=4
3) Activate: get the id information from the URL using id=request.GET.get('id'). Query user=user whose id is id. set user.is_active=True.
Actually I implemented an reusable application like your request, check this(https://github.com/JunyiJ/django-register-activate) if you are interested.
Hope that helps. Good luck!
check this out... i Hope it helps out with not only the solution u need but also the explanation.. Because i think django-registration app is meant for default Django User. So if u want to have extra fields in your registration form, Start thinking of customizing ur Django User and Its authentication yourself. You dont need the django-registration app here..
Here are some tutorials thats will help
http://www.caktusgroup.com/blog/2013/08/07/migrating-custom-user-model-django/
and many more...

How can I send an email to user on new user addition to django admin site?

I want to send an email with login details to user emailaddress whenever Admin adds new user to admin site.
I know Django provides send_mail module for that,but I don't know where should I put this code
and override some view to send automatic mails on new user addition.
from django.core.mail import send_mail
send_mail('Subject here', 'Here is the message.', 'from#example.com',
['to#example.com'], fail_silently=False)
How can i do it?
I tried putting this code in my models.py
from django.db.models.signals import post_save
from django.contrib.auth.models import User
def email_new_user(sender, **kwargs):
if kwargs["created"]: # only for new users
new_user = kwargs["instance"]
print new_user.email
#send_mail('Subject here', 'Here is the message.', 'from#example.com',['to#example.com'], fail_silently=False)
post_save.connect(email_new_user, sender=User)
But its not printing anything on command line. I think the function is not getting called whenever i create a new user.Don't know why?
There are multiple problems I can see in the code as I can see it, above.
First, the def email_new_user() is wrongly indented. If that is not a formatting error here, correct it.
new_user = kwargs.["instance"] is wrong syntax. It really should be kwargs["instance"]
And then, do you have an SMTP server running? Can you send email from the shell? If not, configure that and then try it again. It should work.
You want to hook the post_save signal for the User model.
One possible problem was, Django user cretion consists of two steps. First django asks for username, password and password confirmation, and when you press save (or save and continue edit) your save method fired without e-mail information. Since django will accept that request as a newly created record, your signal will fire for a record with no e-mail information...
If django handles admin save diffrently and do not mark a newly created record instance as created you shall not have any problem, but probably django will not do that.
UPDATE: Some possible reasons are:
You need django 1.3, so check your django version with python manage.py version and update it if required...
Best place for your your models.py files
You can put signal handling and registration code anywhere you like. However, you'll need to make sure that the module it's in gets imported early on so that the signal handling gets registered before any signals need to be sent. This makes your app's models.py a good place to put registration of signal handlers.
These are basic problems...

Proper technique for a contact form?

I have a contact form at shantiyoga.ca/contact
With the view
def contact(request):
template = 'contact.html'
form = ContactForm(request.POST or None)
if form.is_valid(): # All validation rules pass
subject = form.cleaned_data['subject']
message = "%s\n\n%s" % (form.cleaned_data['message'], form.cleaned_data['sender'])
cc_myself = form.cleaned_data['cc_myself']
recipients = ['contact#shantiyoga.ca']
sender = form.cleaned_data['sender']
if cc_myself:
recipients.append(sender)
headers = {'Reply-To': form.cleaned_data['sender']}
from django.core.mail import send_mail
send_mail(subject,message,sender,recipients,headers)
return redirect('/thanks/')
return render_to_response(template, {'form': form, 'current':'contact'}, context_instance=RequestContext(request))
Which works fairly well. I'm not terribly sophisticated with Django and my Python skills are not quite up to snuff, so please bear with me if I step through this in a basic fashion.
I would like to clarify that there is no way for the form recipient (contact#shantiyoga.ca) to receive the contact form from the value of the email field (user entered). It will always be sent by the authenticated email in my settings.py, which at this point is my personal email?
A user fills out the contact form and hits submit, an email is sent to contact#shantiyoga.ca from my personal email, and if the user decides to cc themself, a copy of the email is sent to them, also from my personal email.
This is not ideal, should I create an email like contactform#shantiyoga.ca for my settings.py to send the email from?
Also, the headers = {'Reply-To': form.cleaned_data['sender']} does not appear to be doing anything and I can't seem to find documentation describing its proper usage, has anyone had success using this technique?
Thank you for your time,
Noah
I would like to clarify that there is no way for the form recipient (contact#shantiyoga.ca) to receive the contact form from the value of the email field (user entered). It will always be sent by the authenticated email in my settings.py, which at this point is my personal email?
You're setting the sender of the email to sender but using EMAIL_HOST, correct? If this is the case, be careful that your SMTP account will let you send email from users other than your domain. Normally, providing you have an authenticated account on that server, you'll be able to set the From: field to whatever you like.
So in short, this email will hit your inbox appearing to be from the sender variable. So when you hit reply, you'll be able to email them back, which is I think what you want. However, it will be sent using the SMTP server whose authentication details you provide. There is a disconnect between being able to send email (SMTP) and being able to receive it, which I think has got you confused.
I've never tried it, but to add extra headers I believe you need to use the EmailMessage object. According to the docs, you would:
e = EmailMessage(headers={'Reply-To':...
e.send()
Be careful doing this; specifically, be careful to strip out newlines in the reply to field. I do not know if the default clean methods would do this.
Finally, there isn't much wrong with your django/python at all. The only thing I'd say, by way of awareness, is not to use this:
return redirect('/thanks/')
but instead:
return HttpResponseRedirect(reverse('myapp.views.method',args=tuple,kwargs=dict))
This gives you the ability to not hardcode urls into your application. The reverse method will look them up from urls.py for you, so you can move your application around/change urls and it will still work.
Here is signature of send_mail from django documentation:
send_mail(subject, message, from_email, recipient_list, fail_silently=False, auth_user=None, auth_password=None, connection=None)
As you can see it does not have headers argument.
You will need to use EmailMessage object directly.
It would also be nice to remove email-formatting from view. I would write everything something more like this:
from django.core.mail.message import EmailMessage
from django.conf import settings
class ContactsEmailMessage(EmailMessage):
def __init__(self, sender, subject, message, cc_myself):
super(ContactEmailMessage, self).__init__(
subject=subject,
body=self._get_message(self, sender, message),
from=settings.DEFAULT_FROM_EMAIL,
to=settings.CONTACT_RECIPIENTS,
headers=self._get_headers(sender),
cc=(sender, ) if cc_myself else None
)
def _format_message(self, sender, message):
return "%s\n\n%s" % (sender, message)
def _get_headers(self, sender):
return {
'reply-to': sender
}
Now you can write clean and easy to read view:
from myproject.mail.message import ContactsEmailMessage, BadHeaderError
from django.core.exceptions import SuspiciousOperation
def contact(request):
...
form = ContactForm(request.POST or None)
if form.is_valid(): # All validation rules pass
try:
message = ContactsEmailMessage(**form.cleaned_data)
message.send()
except BadHeaderError:
# django will raise this error if user will try to pass suspicious new-line
# characters to "sender" or other fields. This is safe-guard from
# header injections
raise SuspiciousOperation()
return redirect('/thanks/')
...