:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
from typing import Optional, cast
from products.warehouse_sources.sources.mailersend._config import MailerSendSourceConfig
from products.warehouse_sources.sources.mailersend.mailersend import (
MailerSendResumeConfig,
check_credentials,
mailersend_source,
)
from products.warehouse_sources.sources.mailersend.settings import ENDPOINTS, INCREMENTAL_FIELDS, MAILERSEND_ENDPOINTS
from products.warehouse_sources.sources.sdk import (
CanonicalDescriptions,
DataWarehouseSourceCategory,
ExternalDataSourceType,
FieldType,
ReleaseStatus,
ResumableSource,
ResumableSourceManager,
SourceConfig,
SourceFieldInputConfig,
SourceFieldInputConfigType,
SourceInputs,
SourceRegistry,
SourceResponse,
SourceSchema,
detect,
)
@SourceRegistry.register
class MailerSendSource(ResumableSource[MailerSendSourceConfig, MailerSendResumeConfig]):
codebase_detection = detect(
env=("^MAILERSEND_",),
npm=("mailersend",),
pypi=("mailersend",),
composer=("mailersend/mailersend", "mailersend/laravel-driver"),
go=("github.com/mailersend/mailersend-go",),
maven=("com.mailersend:java-sdk",),
rubygems=("mailersend-ruby",),
)
api_docs_url = "https://developers.mailersend.com/api/v1/"
@property
def source_type(self) -> ExternalDataSourceType:
return ExternalDataSourceType.MAILERSEND
@property
def get_source_config(self) -> SourceConfig:
return SourceConfig(
name=ExternalDataSourceType.MAILERSEND,
category=DataWarehouseSourceCategory.MARKETING___EMAIL,
label="MailerSend",
releaseStatus=ReleaseStatus.GA,
caption="""Enter your MailerSend API token to automatically pull your MailerSend data into the PostHog Data warehouse.
You can create an API token in your [MailerSend domain settings](https://app.mailersend.com/api-tokens).
Grant the token read access to the data you want to sync:
- Email (for **activity** events)
- Domains
- Recipients
- Templates
- Suppressions (for **hard_bounces**, **spam_complaints** and **unsubscribes**)
- Analytics (for **analytics_by_date**)
Note: MailerSend retains email activity for 1-30 days depending on your plan, so the **activity** table only backfills as far as that retention window on its first sync.
""",
iconPath="/static/services/mailersend.png",
docsUrl="https://posthog.com/docs/cdp/sources/mailersend",
fields=cast(
list[FieldType],
[
SourceFieldInputConfig(
name="api_token",
label="API token",
type=SourceFieldInputConfigType.PASSWORD,
required=True,
placeholder="mlsn....",
secret=True,
),
],
),
)
def get_non_retryable_errors(self) -> dict[str, str | None]:
return {
# A revoked or insufficiently-scoped token surfaces as an HTTPError when `_fetch_page`
# calls `raise_for_status()`. Retrying can never satisfy a credential problem. Match the
# stable status text and base host, not the per-request path/query.
"401 Client Error: Unauthorized for url: https://api.mailersend.com": "Your MailerSend API token is invalid or has been revoked. Create a new token in your MailerSend domain settings, then reconnect.",
"403 Client Error: Forbidden for url: https://api.mailersend.com": "Your MailerSend API token is missing the read permissions needed to sync this data. Grant the required read scopes in your MailerSend domain settings, then reconnect.",
}
def get_schemas(
self,
config: MailerSendSourceConfig,
team_id: int,
with_counts: bool = False,
names: list[str] | None = None,
force_refresh: bool = False,
api_version: str | None = None,
) -> list[SourceSchema]:
def _description(endpoint: str) -> str | None:
if endpoint == "activity":
return "Email activity events. Only backfills as far as your plan's data retention window (1-30 days) on initial sync."
if endpoint == "analytics_by_date":
return "Daily email counts (sent, delivered, opened, clicked, bounced, and more). MailerSend keeps analytics for 6 months, so the first sync backfills up to 6 months."
return None
def _build_schema(endpoint: str) -> SourceSchema:
endpoint_config = MAILERSEND_ENDPOINTS[endpoint]
return SourceSchema(
name=endpoint,
supports_incremental=endpoint_config.supports_incremental,
supports_append=endpoint_config.supports_append,
incremental_fields=INCREMENTAL_FIELDS.get(endpoint, []),
should_sync_default=endpoint_config.should_sync_default,
description=_description(endpoint),
)
schemas = [_build_schema(endpoint) for endpoint in ENDPOINTS]
if names is not None:
names_set = set(names)
schemas = [s for s in schemas if s.name in names_set]
return schemas
def validate_credentials(
self,
config: MailerSendSourceConfig,
team_id: int,
schema_name: Optional[str] = None,
api_version: str | None = None,
) -> tuple[bool, str | None]:
return check_credentials(config.api_token, schema_name)
def get_canonical_descriptions(self) -> CanonicalDescriptions:
from products.warehouse_sources.sources.mailersend.canonical_descriptions import CANONICAL_DESCRIPTIONS
return CANONICAL_DESCRIPTIONS
def get_resumable_source_manager(self, inputs: SourceInputs) -> ResumableSourceManager[MailerSendResumeConfig]:
return ResumableSourceManager[MailerSendResumeConfig](inputs, MailerSendResumeConfig)
def source_for_pipeline(
self,
config: MailerSendSourceConfig,
resumable_source_manager: ResumableSourceManager[MailerSendResumeConfig],
inputs: SourceInputs,
) -> SourceResponse:
return mailersend_source(
api_token=config.api_token,
endpoint=inputs.schema_name,
team_id=inputs.team_id,
job_id=inputs.job_id,
resumable_source_manager=resumable_source_manager,
should_use_incremental_field=inputs.should_use_incremental_field,
db_incremental_field_last_value=inputs.db_incremental_field_last_value
if inputs.should_use_incremental_field
else None,
)