Skip to main content

Hello,

 

I want to extract an image that’s embedded in a Gmail. When I look up the attachment tag in “New Email Matching Search” it comes up empty, but I found the HTML <img> tag with the associated image. Is there any way to extract the image and then send it as an SMS?

Hi ​@RobbyG 

Options for email parsing:

 


Thank you! I’ll give these a try.


If you sent the email html to a code block you’ll be able to grab the img src/href which you can the send on. I’m uncertain if you can download the actual image though.

I would use something like this:

 

import re

# Example input data
input_data = {
"html": """
<p>Here is an image:</p>
<img alt="Example Image" src="https://example.com/image1.jpg">
<p>Another image:</p>
<img src="https://example.com/image2.png" alt="Second Image">
"""
}

# Regex pattern to handle flexible attribute order and spacing
pattern = r'<imgt^>]*alt=*"\']("^"\']*)\"\'][^>]*src=*"\']("^"\']+)\"\']|<imgt^>]*src=*"\']("^"\']+)\"\'][^>]*alt=*"\']("^"\']*)\"\']'

# Find all matches in the HTML input
matches = re.findall(pattern, input_data_"html"])

# Process matches to handle flexible groups
key_value_pairs = {}
for match in matches:
# Each match is a tuple of up to 4 elements due to the alternate regex groups
alt = matchm0] or matchm3] # First group's alt or second group's alt
src = matchm1] or matchm2] # First group's src or second group's src
key_value_pairspalt.strip()] = src.strip()

# Output the key-value pairs
output = key_value_pairs


Hope it’s useful​​​


Reply