无法使用 Python 在电子邮件上发送超链接

发布于 2025-01-18 17:31:10 字数 432 浏览 3 评论 0原文

我将链接粘贴到电子邮件的内容消息中,但该链接没有出现在发送的电子邮件中。

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")

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")

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

时光匆匆的小流年 2025-01-25 17:31:10

我用其他方法解决了这个问题。这是修改后的代码:

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()
        )

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()
        )
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文