emails.py 2.7 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
from django.core import mail
from django.template import loader
from django.utils import translation
from django.utils.translation import ugettext as _

from members import models
from thaliawebsite.settings import settings


def send_membership_announcement(dry_run=False):
    members = (models.Member.active_members
               .filter(user__membership__until__isnull=True))

    with mail.get_connection() as connection:
        for member in members:
            print("Send email to {} ({})".format(member.get_full_name(),
                                                 member.user.email))
18
19
20
21
22
23
24
25
26
27
28
29
30
            if not dry_run:
                with translation.override(member.language):
                    email_body = loader.render_to_string(
                        'members/email/membership_announcement.txt',
                        {'name': member.get_full_name()})
                    mail.EmailMessage(
                        _('Membership announcement'),
                        email_body,
                        settings.WEBSITE_FROM_ADDRESS,
                        [member.user.email],
                        bcc=[settings.BOARD_NOTIFICATION_ADDRESS],
                        connection=connection
                    ).send()
31
32
33
34
35
36
37
38
39

        if not dry_run:
            mail.mail_managers(
                _('Membership announcement sent'),
                loader.render_to_string(
                    'members/email/membership_announcement_notification.txt',
                    {'members': members}),
                connection=connection,
            )
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69


def send_information_request(dry_run=False):
    members = models.Member.active_members.all()

    with mail.get_connection() as connection:
        for member in members:
            print("Send email to {} ({})".format(member.get_full_name(),
                                                 member.user.email))
            with translation.override(member.language):
                email_body = loader.render_to_string(
                    'members/email/information_request.txt',
                    {'name': member.get_full_name()})
                mail.EmailMessage(
                    _('Membership announcement'),
                    email_body,
                    settings.WEBSITE_FROM_ADDRESS,
                    [member.user.email],
                    bcc=[settings.BOARD_NOTIFICATION_ADDRESS],
                    connection=connection
                ).send()

        if not dry_run:
            mail.mail_managers(
                _('Membership announcement sent'),
                loader.render_to_string(
                    'members/email/information_request_notification.txt',
                    {'members': members}),
                connection=connection,
            )