(20241127) Google API OAuth2.0 support ready!

This commit is contained in:
2024-11-27 16:54:08 +05:30
parent 535f998272
commit 485a8bd486
8 changed files with 177 additions and 634 deletions
+17 -3
View File
@@ -180,12 +180,19 @@ class GMailMessage:
self.message.attach(MIMEText(html_text, "html"))
def add_inline_image(self, image_file, content_id = None):
def add_inline_image(
self,
image_file: str | io.BytesIO,
file_name: str = None,
content_id: str = None
):
"""
Add an inline image to the body of the mail.
NOTE: This is NOT the same as sending an image as an attachment.
:param image_file: The image data to attach to the mail body.
:param file_name: The name of the file. This is the same name by which it will be downloaded. You need not
specify this if the input file is specified as a path. Needed when you give the input file as a buffer.
:param content_id: Inline images are inserted via HTML bocks. This field identifies the image resource. If not
specified, I will generate a random string. You may write a custom value here if you know what you are
doing. For most use cases, please ignore this field.
@@ -195,6 +202,7 @@ class GMailMessage:
# Read the image as bytes:
image_bytes = None
if type(image_file) is str:
file_name = file_name or os.path.split(image_file)[-1]
with open(image_file, "rb") as opened_image_file:
image_bytes = opened_image_file.read()
if type(image_file) is io.BytesIO:
@@ -217,7 +225,13 @@ class GMailMessage:
# Then add the image:
image_part = MIMEImage(image_bytes)
image_part.add_header("Content-ID", f"<{content_id}>")
image_part.add_header(
"Content-ID", f"<{content_id}>"
)
image_part.add_header(
"Content-Disposition",
f"inline; filename=\"{file_name}\"",
)
self.message.attach(image_part)
def add_attachment(
@@ -253,7 +267,7 @@ class GMailMessage:
encoders.encode_base64(part)
part.add_header(
"Content-Disposition",
f"attachment; filename= {file_name}",
f"attachment; filename=\"{file_name}\"",
)
self.message.attach(part)