def create_contact(username, email, name, blacklisted):
    configuration = setup_sendinblue_configuration()

    if configuration:
        api_instance = sib_api_v3_sdk.ContactsApi(
            sib_api_v3_sdk.ApiClient(configuration))
        contact = sib_api_v3_sdk.CreateContact(
            email=email,
            attributes={"FULL_NAME": name},
            email_blacklisted=blacklisted,
            list_ids=[
                settings.EDRAAK_SENDINBLUE_LISTID,
            ],
            update_enabled=True)

        try:
            response = api_instance.create_contact(contact)
            log.info(
                'SendInBlue contact created with response text {text}'.format(
                    text=response))
        except ApiException as e:
            log.exception(
                "Exception when calling SendInBlue for email ({}) ContactsApi->create_contact: {}\n"
                .format(email, e))


# TODO: update contact attributes upon completion of profile
# TODO: delete a contact and integrate with user retirement
def make_update_request(payload: UpdateSendinblueContactRequest) -> bool:
    if settings.IS_RUNNING_TESTS:
        testing.sendinblue_requests.append({
            "email":
            payload.email,
            "attributes":
            payload.attributes,
            "emailBlacklisted":
            payload.emailBlacklisted
        })
        return True

    if settings.IS_DEV:
        logger.info(
            "A request to Sendinblue Contact API would be sent for user %s with attributes %s emailBlacklisted: %s",
            payload.email,
            payload.attributes,
            payload.emailBlacklisted,
        )
        return True

    configuration = sib_api_v3_sdk.Configuration()
    configuration.api_key["api-key"] = settings.SENDINBLUE_API_KEY
    api_instance = sib_api_v3_sdk.ContactsApi(
        sib_api_v3_sdk.ApiClient(configuration))
    create_contact = sib_api_v3_sdk.CreateContact(
        email=payload.email,
        attributes=payload.attributes,
        list_ids=payload.contact_list_ids,
        update_enabled=True,
        email_blacklisted=payload.emailBlacklisted,
    )

    try:
        api_instance.create_contact(create_contact)
        return True

    except SendinblueApiException as exception:
        if exception.status == 524:
            logger.warning(
                "Timeout when calling ContactsApi->create_contact: %s",
                exception,
                extra={
                    "email": payload.email,
                    "attributes": payload.attributes,
                    "emailBlacklisted": payload.emailBlacklisted,
                },
            )
        else:
            logger.exception(
                "Exception when calling ContactsApi->create_contact: %s",
                exception,
                extra={
                    "email": payload.email,
                    "attributes": payload.attributes,
                    "emailBlacklisted": payload.emailBlacklisted,
                },
            )
        return False
示例#3
0
def create_compaign_user(user_mail):
    configuration = sib_api_v3_sdk.Configuration()
    configuration.api_key['api-key'] = TRANSACTIONAL_MAIL_KEY

    api_instance = sib_api_v3_sdk.ContactsApi(
        sib_api_v3_sdk.ApiClient(configuration))
    create_contact = sib_api_v3_sdk.CreateContact(email=user_mail,
                                                  list_ids=[COMPAIGN_ID])

    try:
        api_response = api_instance.create_contact(create_contact)
    except ApiException as e:
        print("Exception when calling ContactsApi->create_contact: %s\n" % e)
        return False
    return True
示例#4
0
    def post(self, request):
        try:
            email = request.data.get("email")
            validate_email(email)

            list_id = settings.NEWSLETTER_SENDINBLUE_LIST_ID
            configuration = sib_api_v3_sdk.Configuration()
            configuration.api_key["api-key"] = settings.ANYMAIL.get(
                "SENDINBLUE_API_KEY")
            api_instance = sib_api_v3_sdk.ContactsApi(
                sib_api_v3_sdk.ApiClient(configuration))
            create_contact = sib_api_v3_sdk.CreateContact(email=email)
            create_contact.list_ids = [int(list_id)]
            create_contact.update_enabled = True
            api_instance.create_contact(create_contact)
            return JsonResponse({}, status=status.HTTP_200_OK)
        except sib_api_v3_sdk.rest.ApiException as e:
            contact_exists = json.loads(
                e.body).get("message") == "Contact already exist"
            if contact_exists:
                logger.error(f"Beta-tester already exists: {email}")
                return JsonResponse({}, status=status.HTTP_200_OK)
            logger.error("SIB API error in beta-tester subsription :")
            logger.exception(e)
            return JsonResponse(
                {"error": "Error calling SendInBlue API"},
                status=status.HTTP_400_BAD_REQUEST,
            )
        except ValidationError as e:
            logger.error(f"Invalid email on beta-tester subscription: {email}")
            logger.exception(e)
            return JsonResponse({"error": "Invalid email"},
                                status=status.HTTP_400_BAD_REQUEST)
        except Exception as e:
            logger.error("Error on newsletter subscription")
            logger.exception(e)
            return JsonResponse(
                {"error": "An error has ocurred"},
                status=status.HTTP_500_INTERNAL_SERVER_ERROR,
            )
    def subscribe(self, account_id, list_id, email_address):
        """API call to create a contact and subscribe it to a list"""
        self.initialize()
        client = self.connect(account_id)
        api_contact = sib_api_v3_sdk.ContactsApi(client)
        user = sib_api_v3_sdk.CreateContact(email=email_address)
        try:
            api_contact.create_contact(user)
        except ApiException:
            logger.exception("Exception creating user %s" % email_address)
            return

        api_lists = sib_api_v3_sdk.ListsApi(client)
        user_in_list = sib_api_v3_sdk.AddContactToList()
        user_in_list.emails = [email_address]
        try:
            response = api_lists.add_contact_to_list(list_id, user_in_list)
        except ApiException:
            logger.exception("Exception subscribing %s" % email_address)
            return
        if len(response.contacts.success) == 1:
            return True
        else:
            return False
from __future__ import print_function
import time
import datetime
import sib_api_v3_sdk
from sib_api_v3_sdk.rest import ApiException
from pprint import pprint

configuration = sib_api_v3_sdk.Configuration()
configuration.api_key['api-key'] = 1

# create an instance of the API class
api_instance = sib_api_v3_sdk.ContactsApi(
    sib_api_v3_sdk.ApiClient(configuration))
create_contact = sib_api_v3_sdk.CreateContact(
    email='*****@*****.**',
    list_ids=[3])  # CreateContact | Values to create a contact

try:
    # Create a contact
    api_response = api_instance.create_contact(create_contact)
    pprint(api_response)
except ApiException as e:
    print("Exception when calling ContactsApi->create_contact: %s\n" % e)

configuration = sib_api_v3_sdk.Configuration()
configuration.api_key[
    'api-key'] = "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"

api_instance = sib_api_v3_sdk.EmailCampaignsApi(
    sib_api_v3_sdk.ApiClient(configuration))
campaign_id = 1