Mihai
Mihai

Reputation: 41

Can't send hyperlink on email with Python

I paste the link in the content message of my email but the link doesn't appear in the email sent.

try:
        content = "https://www.google.com/"
        mail = smtplib.SMTP('smtp.gmail.com', 587)
        mail.ehlo()
        mail.starttls()
        mail.login('my_emailgmail.com','password')
        mail.sendmail('my_email.gmail.com', "receiver.gmail.com", content)
        mail.quit()
        print("Successfully sent email")
    except:
        print("Nah")

Upvotes: 1

Views: 1445

Answers (1)

Mihai
Mihai

Reputation: 41

I solved the problem using an other method. Here is the code modified:

def send_email(recipient, reservation_data):
    sender_email = "sender.gmail.com"
    receiver_email = "reciever.gmail.com"
    password = "password"

    message = MIMEMultipart("alternative")
    message["Subject"] = "multipart test"
    message["From"] = sender_email
    message["To"] = receiver_email

    text = """\
    Confirm paying here www.Museums.com"""
    html = """\
    <html>
      <body>
        <p>Hi,<br>
           How are you?<br>
           <a href="www.google.com">Real Python</a> 
           has many great tutorials.
        </p>
      </body>
    </html>
    """
    
    part1 = MIMEText(text, "plain")
    part2 = MIMEText(html, "html")

    message.attach(part1)
    message.attach(part2)

    context = ssl.create_default_context()
    with smtplib.SMTP_SSL("smtp.gmail.com", 465, context=context) as server:
        server.login(sender_email, password)
        server.sendmail(
            sender_email, receiver_email, message.as_string()
        )

Upvotes: 2

Related Questions