예제 #1
0
def send_email(receive_name, email, send_type="comment"):

    random_str = generate_code(4)
    if send_type == "comment":
        random_str = generate_code(4)

    if send_type == "comment":
        email_title = "Diomedes博客评论-验证邮箱,验证码:{0}".format(random_str)
        email_content = "您的验证码是:{0}".format(random_str)
        email_body = loader.render_to_string(
            'emailMessage.html', {
                'base_url': 'https://blog.coderap.com',
                'receive_name': receive_name,
                'email_context': email_content
            })

        message = EmailMessage(email_title, email_body, EMAIL_FROM, [email])
        message.content_subtype = "html"  # Main content is now text/html
        send_status = message.send()
        if int(send_status) == 1:
            # 保存验证码
            email_record = EmailVerifyRecord()
            email_record.code = random_str
            email_record.email = email
            email_record.send_type = send_type
            email_record.send_time = datetime.utcnow()
            email_record.save()
            return int(send_status)
        else:
            return 0
def send_code_email(email,
                    referee_email='',
                    send_type="register",
                    real_name='',
                    is_artist=False):
    """
    :param is_artist: check the register is for artist or not
    :param referee_email: the email of referee's email
    :param real_name: the real name of user when verify as artist
    :param email: the email address used for sending email
    :param send_type: retrieve or verification
    :return: True/False
    """
    email_record = EmailVerifyRecord()
    # store these info into database
    code = random_str(16)
    while models.EmailVerifyRecord.objects.filter(code=code):
        code = random_str(16)
    email_record.code = code
    email_record.email = email
    email_record.send_type = send_type
    email_record.send_time = datetime.datetime.now()
    email_record.save()

    if send_type == "register":
        if is_artist:
            email_title = "register verification for " + real_name
            email_body = "Register email is {registered_email}, use the blow link to activate {artist}'s " \
                         "account: http://127.0.0.1:8000/active/{random_code}".format(random_code=code,
                                                                                      artist=real_name,
                                                                                      registered_email=email)
            send_status = send_mail(email_title, email_body,
                                    settings.EMAIL_FROM, [referee_email])
        else:
            email_title = "register verification"
            email_body = "use the blow link to activate your account: http://127.0.0.1:8000/active/{0}".format(
                code)
            send_status = send_mail(email_title, email_body,
                                    settings.EMAIL_FROM, [email])
        if not send_status:
            return False
    if send_type == "reset":
        email_title = "reset password"
        email_body = "The retrieve password link is: http://127.0.0.1:8000/reset/{0}".format(
            code)
        send_status = send_mail(email_title, email_body, settings.EMAIL_FROM,
                                [email])
        if not send_status:
            return False
    return True
예제 #3
0
def send_email(email_info, email, send_type="comment"):
    if send_type == "comment":
        random_str = generate_code(4)
        email_title = "Diomedes博客评论-验证邮箱,验证码:{0}".format(random_str)
        context = {
            'email_info': {
                'base_url': SITE_BASE_URL,
                'receive_name': email_info['receive_name'],
                'code': random_str
            }
        }
        email_body = loader.render_to_string('CommentCodeEmail.html', context)

        message = EmailMessage(email_title, email_body, EMAIL_FROM, [email])
        message.content_subtype = "html"  # Main content is now text/html
        send_status = message.send()
        if int(send_status) == 1:
            # 保存验证码
            email_record = EmailVerifyRecord()
            email_record.code = random_str
            email_record.email = email
            email_record.send_type = send_type
            email_record.send_time = datetime.utcnow()
            email_record.save()
            return int(send_status)
        else:
            return 0

    if send_type == 'reply_comment':
        email_title = "Diomedes博客评论-收到回复"
        context = {
            'email_info': email_info
        }
        email_body = loader.render_to_string('ReplyCommentEmail.html', context)

        message = EmailMessage(email_title, email_body, EMAIL_FROM, [email])
        message.content_subtype = "html"  # Main content is now text/html
        return message.send()