Embed tweets in an email with python

Viewed 29

I am trying to embed tweets in an email. For that, I get the oembed tweet using the tweepy API and then insert it as HTML. However, when I send the email, I notice that I only receive the body text of the tweet. Is there a way to insert the full tweet (that is including images/vide/gif...) in an email using python?

Here is my code:


    import smtplib, ssl
    from email.mime.text import MIMEText
    from email.mime.multipart import MIMEMultipart
    
    sender_email = ""
    receiver_email = ""
    password = ''
    
    message = MIMEMultipart("alternative")
    message["Subject"] = "Email test"
    message["From"] = sender_email
    message["To"] = receiver_email
    
    # Create the plain-text and HTML version of your message
    text = "hello world"
    
    # Here is the HTML directly that I mine via tweepy.api.get_oembed():
    html = """\
    <html>
      <body>
        <blockquote class="twitter-tweet">
            <p lang="en" dir="ltr">you good?</p>&mdash; Twitter (@Twitter) <a
                href="https://twitter.com/Twitter/status/1509206476874784769?ref_src=twsrc%5Etfw">March 30, 2022</a>
        </blockquote>
        <script async src="https://platform.twitter.com/widgets.js" charset="utf-8"></script>
    </html>
    """
    # Turn these into plain/html MIMEText objects
    part1 = MIMEText(text, "plain")
    part2 = MIMEText(html, "html")
    
    # Add HTML/plain-text parts to MIMEMultipart message
    # The email client will try to render the last part first
    message.attach(part1)
    message.attach(part2)
    
    # Create secure connection with server and send email
    server = smtplib.SMTP('smtp.office365.com', 587)  ### put your relevant SMTP here
    server.ehlo()
    server.starttls()
    server.ehlo()
    server.login(sender_email, password)  ### if applicable
    server.send_message(message)
    server.quit()

0 Answers
Related