我们从Python开源项目中,提取了以下5个代码示例,用于说明如何使用django.conf.settings.MAILGUN_API_KEY。
def post_complaint(request): text = request.POST['stripped-text'] token = request.POST['token'] timestamp = request.POST['timestamp'] signature = request.POST['signature'] if not verify(settings.MAILGUN_API_KEY, token, timestamp, signature): return HttpResponse('Signature failed.') is_complaint = False if u'opyright' in text or u'??' in text: is_complaint = True urls = re.findall(ur'/info/(\d+)', text) for pk in urls: if not Extra.objects.filter(hash_id=int(pk)).first(): Extra.objects.create(hash_id=int(pk), status='reviewing') ContactEmail.objects.create(subject=request.POST['subject'], mail_from=request.POST['from'], text=request.POST['stripped-text'], is_complaint=is_complaint ) return HttpResponse('Success')
def send_email(sender, receiver, subject, html): response = requests.post( settings.MAILGUN_API_MESSAGE_URL, auth=("api", settings.MAILGUN_API_KEY), data={ "from": sender, "to": [ receiver, ], "subject": subject, "html": html, }) return response
def send_signup_email(email, link_text): domain = settings.MAILGUN_DOMAIN result = requests.post( "https://api.mailgun.net/v3/" + domain + "/messages", auth=("api", settings.MAILGUN_API_KEY), data={"from": "OpenHouse API <" + settings.MAILGUN_API_FROM + ">", "to": [email], "subject": "OpenHouse API Account Confirmation", "text": "Click to Verify your account: " + link_text}) print(result.text)
def post(self, request): token = request.POST['token'] signature = request.POST['signature'] timestamp = request.POST['timestamp'] if not settings.MAILGUN_API_KEY: logging.error('MAILGUN_API_KEY is not set') return HttpResponse(status=500) if not self.verify(settings.MAILGUN_API_KEY, token, timestamp, signature): logging.info('Unable to verify signature for mailgun request') return HttpResponse(status=403) to_email = parseaddr(request.POST['To'])[1] from_email = parseaddr(request.POST['From'])[1] try: group_id = email_to_group_id(to_email) except Exception: logging.info('%r is not a valid email address', to_email) return HttpResponse(status=500) payload = EmailReplyParser.parse_reply(request.POST['body-plain']).strip() if not payload: # If there's no body, we don't need to go any further return HttpResponse(status=200) process_inbound_email.delay(from_email, group_id, payload) return HttpResponse(status=201)
def get_anymail_setting(name, default=UNSET, esp_name=None, kwargs=None, allow_bare=False): """Returns an Anymail option from kwargs or Django settings. Returns first of: - kwargs[name] -- e.g., kwargs['api_key'] -- and name key will be popped from kwargs - settings.ANYMAIL['<ESP_NAME>_<NAME>'] -- e.g., settings.ANYMAIL['MAILGUN_API_KEY'] - settings.ANYMAIL_<ESP_NAME>_<NAME> -- e.g., settings.ANYMAIL_MAILGUN_API_KEY - settings.<ESP_NAME>_<NAME> (only if allow_bare) -- e.g., settings.MAILGUN_API_KEY - default if provided; else raises AnymailConfigurationError If allow_bare, allows settings.<ESP_NAME>_<NAME> without the ANYMAIL_ prefix: ANYMAIL = { "MAILGUN_API_KEY": "xyz", ... } ANYMAIL_MAILGUN_API_KEY = "xyz" MAILGUN_API_KEY = "xyz" """ try: value = kwargs.pop(name) if name in ['username', 'password']: # Work around a problem in django.core.mail.send_mail, which calls # get_connection(... username=None, password=None) by default. # We need to ignore those None defaults (else settings like # 'SENDGRID_USERNAME' get unintentionally overridden from kwargs). if value is not None: return value else: return value except (AttributeError, KeyError): pass if esp_name is not None: setting = "{}_{}".format(esp_name.upper(), name.upper()) else: setting = name.upper() anymail_setting = "ANYMAIL_%s" % setting try: return settings.ANYMAIL[setting] except (AttributeError, KeyError): try: return getattr(settings, anymail_setting) except AttributeError: if allow_bare: try: return getattr(settings, setting) except AttributeError: pass if default is UNSET: message = "You must set %s or ANYMAIL = {'%s': ...}" % (anymail_setting, setting) if allow_bare: message += " or %s" % setting message += " in your Django settings" raise AnymailConfigurationError(message) else: return default