简体   繁体   English

从服务器python发送电子邮件

[英]Send email from server python

I am trying to send an email form my server with python, I asked my server provider what port to use for this and they said "choose SMTP Ports 465 (Secure SSL/TLS outgoing server) , 25 ( Non-SSL outgoing server)." 我试图用python从服务器发送电子邮件,我问服务器提供商为此使用哪个端口,他们说“选择SMTP端口465(安全SSL / TLS传出服务器),25(非SSL传出服务器)。 ” Not sure what this exactly means but currently I am using 25, here is my code 不知道这到底是什么意思,但是目前我正在使用25,这是我的代码

#! /usr/bin/python

import smtplib

import smtplib
server = smtplib.SMTP('smtp.gmail.com', 25)

#Next, log in to the server
server.login("youremailusername", "password")

#Send the mail
msg = "\nHello!" # The /n separates the message from the headers
server.sendmail("you@gmail.com", "target@example.com", msg)

I filled in my username (which is my email address right) and password,a dn the target but it is not working, when I try to navigate to the url where my py script is, it just doesn't load. 我填写了我的用户名(这是我的电子邮件地址)和密码,这是一个目标,但它不起作用,当我尝试导航到我的py脚本所在的url时,它只是无法加载。 I have an internet connection cause I am loving other things, and go to other pages on my server. 我有Internet连接,因为我爱其他东西,并转到服务器上的其他页面。 I have also tried running with cron jobs but that also doesn't work. 我也尝试过执行cron作业,但这也行不通。

The permissions on the script are 0755, is there a problem with the script? 脚本的权限为0755,脚本是否有问题?

When i ran with cron jobs here is the traceback 当我与cron工作一起运行时,这里是回溯

Traceback (most recent call last):
 File "/home/spencerf/public_html/cgi-bin/send_email.py", line 6, in <module>
   server = smtplib.SMTP('smtp.gmail.com', 25)
 File "/usr/lib64/python2.6/smtplib.py", line 239, in __init__
   (code, msg) = self.connect(host, port)
 File "/usr/lib64/python2.6/smtplib.py", line 295, in connect
   self.sock = self._get_socket(host, port, self.timeout)
 File "/usr/lib64/python2.6/smtplib.py", line 273, in _get_socket
   return socket.create_connection((port, host), timeout)
 File "/usr/lib64/python2.6/socket.py", line 567, in create_connection
   raise error, msg
socket.error: [Errno 101] Network is unreachable

Thanks for the help in advance. 我在这里先向您的帮助表示感谢。

EDIT 编辑

here is updated error log with the port at 587 这是更新的错误日志,端口为587

Traceback (most recent call last):
 File "/home/spencerf/public_html/cgi-bin/send_email.py", line 7, in <module>
   server = smtplib.SMTP('smtp.gmail.com', 587)
 File "/usr/lib64/python2.6/smtplib.py", line 239, in __init__
   (code, msg) = self.connect(host, port)
 File "/usr/lib64/python2.6/smtplib.py", line 295, in connect
   self.sock = self._get_socket(host, port, self.timeout)
 File "/usr/lib64/python2.6/smtplib.py", line 273, in _get_socket
   return socket.create_connection((port, host), timeout)
 File "/usr/lib64/python2.6/socket.py", line 567, in create_connection
   raise error, msg
socket.error: [Errno 101] Network is unreachable

EDIT 2 编辑2

When I had server = smtplib.SMTP('telnet smtp.gmail.com', 587) 当我有server = smtplib.SMTP('telnet smtp.gmail.com', 587)

I got this error 我得到这个错误

Traceback (most recent call last):
 File "/home/spencerf/public_html/cgi-bin/send_email.py", line 8, in <module>
   server = smtplib.SMTP('telnet smtp.gmail.com', 587)
 File "/usr/lib64/python2.6/smtplib.py", line 239, in __init__
   (code, msg) = self.connect(host, port)
 File "/usr/lib64/python2.6/smtplib.py", line 295, in connect
   self.sock = self._get_socket(host, port, self.timeout)
 File "/usr/lib64/python2.6/smtplib.py", line 273, in _get_socket
   return socket.create_connection((port, host), timeout)
 File "/usr/lib64/python2.6/socket.py", line 553, in create_connection
   for res in getaddrinfo(host, port, 0, SOCK_STREAM):
socket.gaierror: [Errno -2] Name or service not known

BUG: 错误:

First of all you are using gmail server not your company server to send mail .For gmail server the output port is 587 首先,您使用的是gmail服务器而不是您的公司服务器发送邮件。对于gmail服务器,输出端口为587

The code: 编码:

Due to security issues gmail blocks accessing mail via code or program

But still you can use gmail to send mail via code if you do the following things 但是,如果您执行以下操作,仍然可以使用gmail通过代码发送邮件

What i have done in code : 我在代码中做了什么:

1.Added a error object to get the error message 1,添加了一个错误对象以获取错误信息

import smtplib    

try:

    server = smtplib.SMTP('smtp.gmail.com', 587)

    #Next, log in to the server
    server.login("youremailusername", "password")

    #Send the mail
    msg = "\nHello!" # The /n separates the message from the headers
    server.sendmail("you@gmail.com", "target@example.com", msg)
    print "Successfully sent email"
except smtplib.SMTPException,error:
    print str(error)
    print "Error: unable to send email"

If u ran this code u would see a error message like this stating that google is not allowing u to login via code 如果您运行此代码,您会看到一条错误消息,指出Google不允许您通过代码登录

Things to change in gmail: gmail发生的变化:

1.Login to gmail 1.登录gmail

2.Go to this link https://www.google.com/settings/security/lesssecureapps 2.转到此链接https://www.google.com/settings/security/lesssecureapps

3.Click enable then retry the code 3.单击启用,然后重试代码

Hopes it help :) 希望对您有所帮助:)

But there are security threats if u enable it 但是,如果您启用它,则会存在安全威胁

Updated 更新

import smtplib       
try:
    content = 'test'

    mail = smtplib.SMTP('smtp.gmail.com',587)

    mail.ehlo()
    mail.starttls()
    mail.login("ABC@gmail.com", "password")
    mail.sendmail("ABC@gmail.com", "recivermailaddress", content)
    mail.quit
    print "Successfully sent email"
except smtplib.SMTPException,error:
    print str(error)

In python, please download yagmail (disclaimer: I'm the developer): 在python中,请下载yagmail (免责声明:我是开发人员):

pip install yagmail

It is then simply a matter of: 那么,这仅仅是一个问题:

import yagmail
yag = yagmail.SMTP("you@gmail.com", "password")
yag.send("target@example.com", 'This is the subject', 'Hello!')

I guess it mostly just has to do with the fact that you gave a wrong port (smtp.gmail.com is not at 25). 我猜这主要与您输入了错误的端口(smtp.gmail.com不是25)有关。

On the github you can also see a common list of errors . 在github上,您还可以看到常见的错误列表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM