Skip to content

Allow "sender" to be set per-message #230

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions fastapi_mail/fastmail.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ async def __prepare_message(
message, template
)
msg = MailMsg(message)
sender = await self.__sender()
sender = await self.__sender(message)
return await msg._message(sender)

async def __template_message_builder(
Expand All @@ -85,10 +85,10 @@ async def __template_message_builder(
template_data = self.check_data(message.template_body)
return template.render(**template_data)

async def __sender(self) -> Union[EmailStr, str]:
sender = self.config.MAIL_FROM
if self.config.MAIL_FROM_NAME is not None:
return formataddr((self.config.MAIL_FROM_NAME, self.config.MAIL_FROM))
async def __sender(self, message: MessageSchema) -> Union[EmailStr, str]:
sender = message.from_email or self.config.MAIL_FROM
if (from_name := message.from_name or self.config.MAIL_FROM_NAME) is not None:
return formataddr((from_name, sender))
return sender

async def send_message(
Expand Down
2 changes: 2 additions & 0 deletions fastapi_mail/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ class MessageSchema(BaseModel):
cc: List[EmailStr] = []
bcc: List[EmailStr] = []
reply_to: List[EmailStr] = []
from_email: Optional[EmailStr] = None
from_name: Optional[str] = None
charset: str = "utf-8"
subtype: MessageType
multipart_subtype: MultipartSubtypeEnum = MultipartSubtypeEnum.mixed
Expand Down
22 changes: 22 additions & 0 deletions tests/test_message.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,28 @@ def test_replyto():
assert msg.reply_to == ["[email protected]"]


def test_from_email():
msg = MessageSchema(
subject="subject",
recipients=[],
from_email="[email protected]",
subtype=MessageType.plain,
)

assert msg.from_email == "[email protected]"


def test_from_name():
msg = MessageSchema(
subject="subject",
recipients=[],
from_name="No Reply",
subtype=MessageType.plain,
)

assert msg.from_name == "No Reply"


def test_cc():
msg = MessageSchema(
subject="subject",
Expand Down