如何不使用gmail从python发送电子邮件? [英] How does one send an e-mail from python NOT using gmail?

查看:130
本文介绍了如何不使用gmail从python发送电子邮件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经有使用python发送电子邮件的代码:

I already have code for sending e-mails with python:

def send_email_gmail(subject, message, destination):
    """ Send an e-mail using gmail with message to destination email.

    Arguments:
        message {str} -- message string to send.
        destination {str} -- destination email (as string)
    """
    server = smtplib.SMTP('smtp.gmail.com', 587)
    server.starttls()
    # not a real email account nor password, its all ok!
    server.login('me123@gmail.com', 'fakepassword111!!!')

    # craft message
    msg = EmailMessage()

    message = f'{message}\n'
    msg.set_content(message)
    msg['Subject'] = subject
    msg['From'] = 'me123@gmail.com'
    msg['To'] = destination
    # send msg
    server.send_message(msg)

,我已经阅读了多个问题(登录凭据不适用于Gmail SMTP SMTP身份验证错误,发送时使用gmail和python的邮件)解决常见错误:

and I've read the multiple question (Login credentials not working with Gmail SMTP or SMTPAuthenticationError when sending mail using gmail and python) solving the common error:

smtplib.SMTPAuthenticationError: (534, b'5.7.14 <https://accounts.google.com/signin/continue?sadfdgjsfgrp=1&dsfgscc=1dsdfgsfg&pldsfgt=AKsdfsdfggsdfggnsbu\n5.7.14 G0crCr0qSvWTng9xRE_pd3WnK3S2sDMsdfgsdfgX0J-xoetn7aHyFQi2qYrQisdfgsdfgKIwMCcgD7zLB1t7Z\n5.7.14 -OjHjpJqasdftBuTi9wh0sYlNW637SmPLuMnnLGn_WcZX5TGH4sddsfgXYar-Aasdfw0ctWfLhasdffPQV>\n5.7.14 Please log in via your web browser and then try again.\n5.7.14  Learn more at\n5.7.14  https://support.google.com/mail/answer/787521345364524 n21sm17577sadfdsf46qtn.17 - gsmtp')

无论如何,我做了什么这些答案表明,但我仍然遇到错误。因此,我决定我不想再使用gmail 。我从伪造的帐户发送电子邮件只是为了发送电子邮件,因此它的安全性对我来说无关紧要。

Anyway, I did what those answers suggest but I am still getting an error. So I decided I do not want to use gmail anymore for this. I am sending email from a fake account just for sending emails so the security for it doesn't matter to me.

因此,如何更改上面的代码以使其起作用

So how do change the code above so that it works for a different emailing service that is more reliable for sending emails in python/code?

这个想法的答案应该是自我包含的,并且包含一个有效的示例脚本。

The idea answer would contain be self contained and contain a sample script that works.

p>




Edit1:



我当然要检查打开安全性较差的应用程序功能,请复制粘贴该页面上显示的文字:

I've of course check to turn on less secure app feature on my fake gmail, copy paste text of what that page says:

Turn off less secure app access
Your account is vulnerable to malicious activity because you’re allowing apps & devices that use less secure sign-in technology to access your account. You should turn off this type of access. Google will automatically turn this setting OFF if it’s not being used. Learn more

还有一个黄色的感叹号警告我。

there is also a yellow exclamation sign warning me.

EmailMessage():


它按照建议我粘贴了此(空消息)。

it as suggested I paste this (empty message).

推荐答案

对我来说,它的工作原理与我登录时的预期一样使用SSL,如下所示:

For me it works like expected when I login using SSL, like so:

import smtplib, ssl
from email.mime.text import MIMEText


def send_email_gmail(subject, message, destination):
    # First assemble the message
    msg = MIMEText(message, 'plain')
    msg['Subject'] = subject

    # Login and send the message
    port = 465
    my_mail = 'me123@gmail.com'
    my_password = 'fakepassword111!!!'
    context = ssl.create_default_context() 
    with smtplib.SMTP_SSL('smtp.gmail.com', port, context=context) as server:
        server.login(my_mail, my_password)
        server.sendmail(my_mail, destination, msg.as_string())


send_email_gmail('Test subject', 'This is the message', 'to_email@email.com')

编辑:

但是,如果您真的想使用其他smtp服务器,则可以使用Outlook。通过在端口587上连接到smtp-mail.outlook.com,我可以在没有SSL的情况下正常工作,就像这样:

However, if you would really like to use another smtp server you could use outlook for instance. I got this to work without SSL, by connecting to smtp-mail.outlook.com on port 587, like so:

def send_email_gmail(subject, message, destination):
    # First assemble the message
    msg = MIMEText(message, 'plain')
    msg['Subject'] = subject

    # Login and send the message
    port = 587
    my_mail = 'my_mail@hotmail.com'
    my_password = 'my_password'
    with smtplib.SMTP('smtp-mail.outlook.com', port) as server:
        server.starttls()
        server.login(my_mail, my_password)
        server.sendmail(my_mail, destination, msg.as_string())

send_email_gmail('Test', 'Bericht', 'm.drillenburg@gmail.com')

这篇关于如何不使用gmail从python发送电子邮件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆