How can I import an image from local to my HTML script and utilize the script to send an email using Azure communication Email service via Python.

Srinivasulu, Pavan Kumar 0 Reputation points
2024-06-25T21:32:23.48+00:00

Hi,

I'm working on automating email using Azure communication service, Email. I have a HTML script serving as an email body. I will trigger a python script to grab contents of HTML via python script.

ex:

<!DOCTYPE html>
<html>
<h1>Test Image</h1>
<img src="img1.jpeg" alt="test image" width="400" height="400">
<br>
<p> This is the test email</p>

</html>

I have an image in my local, and I want the image to appear in the email body, rather than as an attachment.

In such a case, how can I paste my image onto my email body?

I highly appreciate someone helping me in this matter.

Thanks,
Pavan

Azure Communication Services
Azure Communication Services
An Azure communication platform for deploying applications across devices and platforms.
903 questions
Azure
Azure
A cloud computing platform and infrastructure for building, deploying and managing applications and services through a worldwide network of Microsoft-managed datacenters.
1,017 questions
Azure Automation
Azure Automation
An Azure service that is used to automate, configure, and install updates across hybrid environments.
1,261 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Grmacjon-MSFT 18,456 Reputation points
    2024-06-28T22:06:14.4833333+00:00

    Hello @Srinivasulu, Pavan Kumar

    To include an image directly in the email body rather than as an attachment when using Azure Communication Services Email, you'll need to embed the image data directly into the HTML using base64 encoding.

    Here's how you can modify your approach:

    1. First, convert your local image to a base64-encoded string.
    2. Then, embed this base64 string directly in your HTML.
    3. Finally, use this HTML as the email body when sending via Azure Communication Services Email.

    Here's a step-by-step guide:

    1. Convert the image to base64:
    import base64
    def image_to_base64(image_path):
        with open(image_path, "rb") as image_file:
            return base64.b64encode(image_file.read()).decode('utf-8')
    # Convert your image
    img_path = "path/to/your/img1.jpeg"
    base64_image = image_to_base64(img_path)
    
    1. Modify your HTML to use the base64-encoded image:
    html_content = f"""
    <!DOCTYPE html>
    <html>
    <h1>Test Image</h1>
    <img src="data:image/jpeg;base64,{base64_image}" alt="test image" width="400" height="400">
    <br>
    <p>This is the test email</p>
    </html>
    """
    
    1. Use this HTML content when sending the email via Azure Communication Services Email:
    from azure.communication.email import EmailClient
    # Your connection string
    connection_string = "your_connection_string_here"
    # Create the EmailClient
    email_client = EmailClient.from_connection_string(connection_string)
    # Prepare the email
    sender = "your_verified_sender@domain.com"
    recipient = "recipient@example.com"
    subject = "Test Email with Embedded Image"
    # Send the email
    poller = email_client.begin_send(
        sender=sender,
        recipients=[recipient],
        subject=subject,
        html_content=html_content
    )
    result = poller.result()
    

    This approach embeds the image directly in the HTML, which means it will appear in the email body rather than as an attachment. The image is included as a Data URL, which most modern email clients support.

    A few things to note:

    The size of your email will increase significantly with embedded images, which might affect delivery speed or trigger spam filters if too large. Some email clients might block embedded images for security reasons, requiring the recipient to explicitly allow them. Make sure your image isn't too large, as there are typically limits on email size.

    Hope that helps.

    -Grace

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.