Python鍙戦€侀偖浠讹細smtplib銆乻endmail

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python鍙戦€侀偖浠讹細smtplib銆乻endmail相关的知识,希望对你有一定的参考价值。

鏍囩锛?a href='http://www.mamicode.com/so/1/%e5%b8%b8%e7%94%a8' title='甯哥敤'>甯哥敤   鏈湴   鍏跺畠   lca   lis   澶氭   閰嶇疆鏂囦欢   lan   鐢ㄦ埛   

鏈湴Ubuntu 18.04锛?strong>鏈湴Python 3.6.5锛?/strong>

闃块噷浜慤buntu 16.04锛岄樋閲屼簯Python 3.5.2锛?/strong>

smtplib锛宻endmail 8.15.2锛?/strong>

 

浠婂ぉ锛屾墦绠楀疄鐜伴€氳繃鐢靛瓙閭欢鍙戦€?娉ㄥ唽鐢ㄦ埛婵€娲诲姛鑳斤紝鍘熶互涓轰竴澶╂槸澶熷鐨勪簡锛岃皝鐭ワ紝韪╁埌 鍧戜簡锛氫竴涓蒋浠跺潙锛屼竴涓狤CS鍧戙€?/p>

 

鏃╀笂涓€鏉ワ紝渚挎寜鐓ф暀绋?a href="http://www.runoob.com/python/python-email.html" target="_blank">Python SMTP鍙戦€侀偖浠?/a>鍋氭祴璇曪紝寰堢畝鍗曪紝浣跨敤smtplib妯″潡 鍦?nbsp;瀹夎浜唖endmail鐨勭數鑴戜笂灏卞彲浠ユ墽琛岄偖浠跺彂閫佸姛鑳戒簡銆?/p>

鍙槸锛宻endmail鏄粈涔堬紵鍏堢敤璧锋潵鍐嶈锛?/p>

 

鎷疯礉浜嗘暀绋嬩腑绗竴娈典唬鐮侊紝骞舵洿鏀逛簡鍏朵腑鐨剆ender鍜宺eceivers锛屽涓嬶細

# -*- coding: UTF-8 -*-
鈥樷€樷€?/span>
Created on 2018骞?鏈?5鏃?
@author: log
鈥樷€樷€?/span>

import smtplib
from email.mime.text import MIMEText
from email.header import Header

import traceback

sender = 鈥?a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="f185948285b19c88959e9c90989fdf929e9c">[email protected]鈥?receivers = [鈥?a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="6c01155d5a5f010d05002c5d5a5f420f0301">[email protected]鈥? 鈥?a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="90fde9e1e1fdf1f9fcd0e1e1bef3fffd">[email protected]鈥榏

# 涓変釜鍙傛暟
message = MIMEText(鈥?/span>鏉ヨ嚜Python smtplib妯″潡鍙戦€佺殑娴嬭瘯閭欢...璇峰嬁鍥炲锛圲buntu锛?/span>鈥?/span>, 鈥?/span>plain鈥?/span>, 鈥?/span>utf-8鈥?/span>)
message[鈥?/span>From鈥?/span>] = Header(鈥?/span>娴嬭瘯閭欢鈥?/span>, 鈥?/span>utf-8鈥?/span>)
message[鈥?/span>To鈥?/span>] = Header(鈥?/span>鑷繁鈥?/span>, 鈥?/span>utf-8鈥?/span>)

subject = 鈥?/span>Python smtplib閭欢鍙戦€佹祴璇?/span>鈥?/span>
message[鈥?/span>Subject鈥?/span>] = Header(subject, 鈥?/span>utf-8鈥?/span>)

try:
    smtpObj = smtplib.SMTP(鈥?/span>localhost鈥?/span>)
    smtpObj.sendmail(sender, receivers, message.as_string())
    print(鈥?/span>娌℃湁寮傚父鍙戠敓锛岄偖浠跺彂閫佹垚鍔?..鈥?/span>)
except:
    traceback.print_exc()
    print(鈥?/span>Error: 閭欢鍙戦€佸け璐?/span>鈥?/span>)

 

sendmail瀹夎

鏈湴鍜岄樋閲屼簯Ubuntu涓兘娌℃湁瀹夎sendmail锛屼娇鐢ㄤ笅闈㈢殑鍛戒护瀹夎锛?/p>

sudo apt install sendmail

sendmail瀹夎瀹屾瘯锛屽紑濮嬫祴璇曠▼搴忋€?/p>

sendmail瀹夎浣嶇疆鏌ヨ锛?/p>

$ whereis sendmail
sendmail: /usr/sbin/sendmail /usr/lib/sendmail /usr/share/sendmail /usr/share/man/man8/sendmail.8.gz

閰嶇疆鏂囦欢浣嶄簬/etc/mail/鐩綍涓紝鏂囦欢寰堝锛屾尯澶嶆潅鐨勩€傚紑濮嬮亣鍒伴棶棰樻椂锛屼慨鏀硅繃鍏朵腑鐨剆endmail.mc銆乻endmail.cf锛屽悗鏉ユ墠鐭ラ亾锛屽叾瀹炰笉闇€瑕佹洿鏀圭殑銆?/p>

鏃ュ織鏂囦欢浣嶄簬/var/log/鐩綍涓紝mail.log銆乵ail.err锛屽悗鑰呬负閿欒璁板綍鏂囦欢锛屾煡鐪嬩袱涓俊鎭€佸畾浣嶉棶棰橈紝寰堝叧閿€?/p>

鎶€鏈垎浜浘鐗? src=

鎶€鏈垎浜浘鐗? src=

鎶€鏈垎浜浘鐗? src=

 

甯哥敤鍛戒护mail銆乵ailq锛宮ail鏌ョ湅 鎺ユ敹鍒扮殑閭欢锛宮ailq鏌ョ湅 寰呭彂閫侀偖浠躲€?/p>

鏇村璇存槑锛?/p>

mail鍛戒护鍙互鏌ョ湅鏀跺埌鐨勯偖浠讹紝浠婂ぉ鍦ㄦ湰鍦版祴璇曟椂锛屾湁鏀跺埌涓€浜涢偖浠讹紝鍏蜂綋鎿嶄綔锛岃繕闇€鐪嬭捣鏂囨。銆佽祫鏂欙紙褰撶劧锛岃嚜宸卞彂閫佺殑閭欢鏄褰撲綔鍨冨溇閭欢鐨剆pam锛夈€?/p>

鎶€鏈垎浜浘鐗? src=

mailq鐨勫緟鍙戦€侀偖浠跺瓨鍦ㄤ簬/var/spool/mqueue-client銆?var/spool/mqueue涓や釜鐩綍涓紝鐩存帴鍒犻櫎鍏朵腑鐨勬枃浠讹紝鍗冲彲鍒犻櫎鎵€鏈夊緟鍙戦€侀偖浠讹紝浣嗚娉ㄦ剰锛岄噷闈㈠瓨鍌ㄧ殑鍙笉涓€瀹氭槸鑷繁杩欎釜鐢ㄦ埛鐨勫緟鍙戦€侀偖浠讹紝鏇村ソ鐨勬柟娉曟槸浣跨敤涓€涓彨鍋歱ostsuper锛堥渶瑕佸畨瑁卲ostfix锛夊懡浠ゆ寜鐢ㄦ埛鍒犻櫎鈥斺€斿娌℃湁瀹夎瀹冿紝鐩存帴鍘荤洰褰曚腑鍒犻櫎鐨勩€?/p>

 

娴嬭瘯绋嬪簭

鍥犱负鏈夋湰鍦癠buntu锛屾墍浠ワ紝灏嗕笂闈㈢殑浠g爜鎷疯礉鍒版湰鍦癠buntu涓繍琛屸€斺€攙irtualenv涓紝Python 3.6.5銆?/p>

缁撴灉锛岄亣鍒颁簡绗竴涓棶棰?/span>锛?/p>

smtplib.SMTPRecipientsRefused: {鈥?a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="167b6f2720257b777f7a562720253875797b">[email protected]鈥? (553, b鈥?.1.8 <[email protected]>... Domain of sender address [email protected] does not exist鈥?, 鈥?a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="2f42565e5e424e46436f5e5e014c4042">[email protected]鈥? (553, b鈥?.1.8 <[email protected]>... Domain of sender address [email protected] does not exist鈥?}

鍙戦€佽€卻ender鐨勫煙鍚?a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="483c2d3b3c0825312c2725292126662b2725">[email protected]涓嶅瓨鍦紒

 

鏇存敼sender涓簊ender = 鈥?a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="67130214132716164904080a">[email protected]鈥橈紝娴嬭瘯绋嬪簭锛屼粛鐒跺け璐ワ紒

 

鏇存敼sender涓?a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="761b0f07071b171f1a3607075815191b">[email protected]锛宺eceivers涓篬鈥?a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="c7aabef6f1f4aaa6aeab87f6f1f4e9a4a8aa">[email protected]鈥榏锛屾兂瀹炵幇浠?a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="e88591999985898184a89999c68b8785">[email protected]鍙戦€侀偖浠跺埌[email protected]锛屽綋鐒讹紝澶辫触浜嗭紒鐨勭‘涔熷簲璇ュけ璐ワ紝閮戒笉鐧诲綍灏辨兂娉曢偖浠讹紵涓嶅彲鑳界殑锛?/p>

 

鏃ュ織鍒嗘瀽

鍦╯endmail鐨勬棩蹇楁枃浠朵腑鎵惧埌涓€浜涜褰曪細

My unqualified host name (myhostname) unknown; sleeping for retry

unable to qualify my own domain name (myhostname) -- using short name

--

w7F2vjwT000424: to=<[email protected]>, delay=00:08:00, xdelay=00:08:00, mailer=esmtp, pri=120327, relay=mx1.qq.com. [183.57.48.35], dsn=4.0.0, stat=Deferred: Connection timed out with mx1.qq.com.

 

涓嬮潰鏄樋閲屼簯涓婃祴璇曠殑鏃ュ織璁板綍锛?/p>

sm-mta[1076]: w7F3lErF001076: from=<[email protected]myhostname>, size=327, class="0", nrcpts=2, msgid=<[email protected]myhostname>, proto=ESMTP, daemon=MTA-v4, relay=localhost [127.0.0.1]

sm-mta[1118]: w7F3nI5V001118: from=<[email protected]localhost>, size=327, class="0", nrcpts=2, msgid=<[email protected]myhostname>, proto=ESMTP, daemon=MTA-v4, relay=localhost [127.0.0.1]

sm-mta[1134]: w7F3p4ua001134: from=<myqqmail@qq.com>, size=327, class="0", nrcpts=1, msgid=<[email protected]myhostname>, proto=ESMTP, daemon=MTA-v4, relay=localhost [127.0.0.1]

--

My unqualified host name (myhostname) unknown; sleeping for retry

unable to qualify my own domain name (myhostname) -- using short name

 

P.S.涓€涓笂鍗堝氨杩欎箞杩囧幓浜嗭紝鍝庡憖锛?/p>

 

鎯充簡涓€涓嬶紝鏈湴鍜岄樋閲屼簯閮芥湁涓嬮潰鐨勬棩蹇楄褰曪細

My unqualified host name (myhostname) unknown; sleeping for retry

unable to qualify my own domain name (myhostname) -- using short name

閭d箞锛屽厛瑙e喅杩欎釜闂鍏堛€?/p>

瑙e喅鏂规锛?span style="color: #ff00ff">鏇存敼/etc/hosts鏂囦欢

 

鎬庝箞淇敼锛?/p>

闃块噷浜慐CS 鏇存敼鍓?nbsp;hosts鏂囦欢涓瓨鍦ㄤ笅闈㈢殑璇彞锛?/p>

ipv4_addr myhostname myhostname

鏇存敼鍚庯細

ipv4_addr myhostname domain.name.com

灏嗙浜屼釜鏇存敼涓哄煙鍚嶅嵆鍙紝褰撶劧锛岃繖涓煙鍚嶅簲璇ヤ笉鑳戒娇qq.com銆?63.com绛夊惂锛熻繖閲屽涓嶆竻妤氾紝杩橀渶dig銆?/p>

 

娉ㄦ剰锛氬湪淇敼涔嬪悗锛孍CS閲嶅惎鍚庝細鍦ㄦ洿鏀瑰悗鐨勯厤缃笅闈㈡坊鍔犲師鏉ョ殑ipv4_addr myhostname myhostname锛屽嵆鍑虹幇涓よ銆備负浣曞姝ゅ憿锛?/p>

 

鏈湴瀹夎鍦ㄨ櫄鎷熸満涓殑Ubuntu鐨刪osts涓儴鍒嗛厤缃細

127.0.0.1 localhost

127.0.1.1 localhost

鏇存敼鍚庯細

127.0.0.1 localhost

127.0.1.1 localhost some_name.me

涓轰綍娣诲姞涓€涓猻omename.me鍛紵杩欎釜some_name.me鏄竴涓煙鍚嶏紝杩樺皾璇曚簡閰嶇疆涓鸿嚜宸辫喘涔扮殑鍩熷悕锛屼互鍙婂叾瀹冨煙鍚嶏紝鍦ㄦ湰鍦版祴璇曟椂閮芥垚鍔熷彂閫佷簡閭欢銆?/p>

瀵逛簡锛屾€庝箞涓嶉厤缃湪绗竴琛?27.0.0.1鍚庨潰鍛紵鎴栬鍙互锛屾湭灏濊瘯锛岄渶dig锛?/p>

 

P.S.閮芥槸Ubuntu锛岀増鏈笉鍚岋紝浣唄osts鏂囦欢鐨勫樊鍒尯澶х殑鍛紒

 

璇存槑锛氭洿鏀筯osts鏂囦欢鍚庯紝sendmail鐨?span style="color: #ff0000">杞欢鍧?/span>灏辩畻瑙e喅浜嗭紝娌℃湁涓婇潰鐨勨€渦nknown; sleeping for retry...unable to qualify my own domain name鈥濋敊璇彁绀轰簡锛屼笅闈?span style="color: #ff0000">ECS鐨勫潙绱ф帴鐫€灏辨潵浜嗭紝鑰岃繖涓狤CS鐨勫潙鐨勫悕瀛楀彨鍋?span style="font-size: 16px; color: #ff0000">闃块噷浜戦粯璁ゅ皝绂乀CP 25绔彛鍑烘柟鍚戠殑璁块棶娴侀噺銆?/p>

 

P.S.涓嬮潰鐨勬祴璇曢兘鏄湪淇敼浜唄osts鏂囦欢鍚?/span>杩涜鐨勬祴璇曘€?/span>

 

澶ч敊鐗归敊鐨勬槸锛氳嚜宸遍鍏堢敤闃块噷浜戣繘琛屼簡娴嬭瘯锛岀粨鏋滐紝閭欢涓€鐩村彂閫佸け璐ャ€傜粨鏋滃氨鏄紝mailq閲岄潰鏈夎秺鏉ヨ秺澶氱殑 寰呭彂閫侀偖浠讹紝杩涚▼閲岄潰鏈夊緢澶氱洰鐨勪富鏈虹鍙d负25鐨勭姸鎬佷负SYN_SENT鐨勮繛鎺ュ瓨鍦?/span>銆?/p>

$ mailq
MSP Queue status...
/var/spool/mqueue-client is empty
                Total requests: 0
MTA Queue status...
                /var/spool/mqueue (18 requests)
-----Q-ID----- --Size-- -----Q-Time----- ------------Sender/Recipient-----------
w7F6USWQ002743*       4 Wed Aug 15 14:30 <[email protected]>
                                         <[email protected]>
w7F6N0hd002648*       4 Wed Aug 15 14:23 <[email protected]>
                 (Deferred: Connection timed out with mx1.qq.com.)
                                         <[email protected]>
w7F1Z5w4032186*     102 Wed Aug 15 09:35 <[email protected]>
                 (Deferred: Connection timed out with 163mx00.mxmail.netease.c)
                                         <[email protected]>
w7F1VIFh032131      102 Wed Aug 15 09:31 <[email protected]>
                 (Deferred: Connection timed out with 163mx00.mxmail.netease.c)
                                         <[email protected]>

netstat -ano | grep :25锛氶偖浠跺彂閫佹垚鍚庯紝鍙細瀛樺湪涓€涓殑锛屽嵆鐘舵€佷负LISTEN閭d釜杩涚▼锛岃繖浜汼YN_SENT琛ㄧず鍦ㄩ噸鍙憁ailq涓殑 寰呭彂閫侀偖浠躲€?/span>

$ netstat -ano | grep :25
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      off (0.00/0/0)
tcp        0      1 172.18.232.21:45698     220.181.14.145:25       SYN_SENT    on (12.06/4/0)
tcp        0      1 172.18.232.21:57766     220.181.14.144:25       SYN_SENT    on (59.02/6/0)
tcp        0      1 172.18.232.21:39008     123.125.50.139:25       SYN_SENT    on (57.35/6/0)
tcp        0      1 172.18.232.21:36952     14.17.41.170:25         SYN_SENT    on (3.37/4/0)
tcp6       0      1 172.18.232.21:40978     220.181.14.135:25       SYN_SENT    on (5.96/5/0)
tcp6       0      1 172.18.232.21:52616     220.181.14.149:25       SYN_SENT    on (21.32/6/0)

 

灏变笉澶氳浜嗭紝姝ら棶棰樼敱ECS鍧戦€犳垚鐨勶紝杩樺ソ锛岃嚦灏戞湰鍦伴偖浠跺彂閫佹垚鍔熶簡锛屽惁鍒欙紝浠婂ぉ涓€瀹氳閮侀椃鍒板悙琛€鍟婏紒

涓嶈繃鑷繁瀹氫綅闂銆佽В鍐抽棶棰樼殑鏁堢巼鎸轰綆鐨勶紝涓嶅涓撴敞锛屽娆¤鍏跺畠涓滆タ骞叉壈锛?/p>

 

灞曠ず涓嬫垚鏋溾€斺€斿叏鏉ヨ嚜鏈湴Ubuntu锛?/p>

-163.com鏀跺埌鐨勯偖浠?/p>

鎶€鏈垎浜浘鐗? src=

鍙戜欢浜哄煙鍚嶄负log-u18.me锛?/p>

鎶€鏈垎浜浘鐗? src=

鍙戜欢浜哄煙鍚嶄负鑷繁璐拱鐨勫煙鍚?/span>锛?/p>

鎶€鏈垎浜浘鐗? src=

 

-qq.com鏀跺埌鐨勯偖浠?/p>

QQ閭浼氬睆钄借繖浜涙湰鍦癠buntu鍙戞潵鐨勯偖浠讹紝闇€瑕?nbsp;棣栭〉--鎴戠殑淇℃伅--鑷姪鏌ヨ 涓皢瀹冧滑 鍙栧洖鎵嶅彲浠ャ€?/p>

鎶€鏈垎浜浘鐗? src=

涓婂浘绱壊鏂规涓殑閭欢寰堟湁鎰忔€濓紝鍐呭鍏ヤ笅鍥锯€斺€斿彂浠朵汉灏辨槸绠€鍗曠殑log锛屽悗闈㈠眳鐒舵病鏈夎窡鐫€鍩熷悕锛?/p>

棰犺浜嗚嚜宸辩殑璁ょ煡鍟婏紒

涓嶈繃锛屽湪缃戞槗閭涓病鏈夊彂鐜拌繖灏侀偖浠讹紒

鎶€鏈垎浜浘鐗? src=

鎶€鏈垎浜浘鐗? src=

鎶€鏈垎浜浘鐗? src=

鎶€鏈垎浜浘鐗? src=

 

鎬荤粨

闃块噷浜慐CS鐨勮繖涓潙鍙互鐢宠浣跨敤25绔彛鏉ユ彁渚涢偖浠舵湇鍔★紝鎹鍙1澶╁鏍告椂闂达紱

浣嗚嚜宸变笉鍑嗗杩欎箞鍋氫簡锛岀幇鍦ㄦ湁缃戞槗閭銆丵Q閭锛岃€屼笖閮藉彲浠ュ紑鍚疭MTP鏈嶅姟锛岄偅涔堝氨鏄娇鐢ㄥ畠浠殑鏈嶅姟鍣ㄦ潵鍙戦€侀偖浠跺槢锛屼釜浜虹綉绔欙紝鍙戦€佺殑閭欢鍙堜笉澶氾紱

鎵€浠ワ紝杩橀渶瑕佺敤娉ㄥ唽鎵嬫満寮€鍙戠綉鏄撻偖绠便€丵Q閭鐨凷MTP鏈嶅姟鎵嶅彲浠ャ€?/p>

 

鑷充粖锛岃嚜宸卞sendmail鎵€鐭ョ敋灏戯紝鎬庝箞閰嶇疆銆佹€庝箞寮€鍙戠殑銆佹簮鐮佸垎鏋愶紝浼拌鏄笉浼氬仛鏇村鐮旂┒浜嗭紝瀹夎浜嗭紝灏卞彲浠ヤ簡銆?/p>

鑷充簬Python鏍囧噯搴撲腑鐨勫叾瀹冧竴浜涢偖浠舵搷浣滃簱锛屾殏鏃朵篃鏄病鏈夌簿鏃跺幓浜嗚В鐮旂┒浜嗭細

The Python Standard Library
https://docs.python.org/3.5/library/index.html
21. Internet Protocols and Support
21.14. poplib 鈥?POP3 protocol client
21.15. imaplib 鈥?IMAP4 protocol client
21.17. smtplib 鈥?SMTP protocol client
21.18. smtpd 鈥?SMTP Server

19. Internet Data Handling
19.1. email 鈥?An email and MIME handling package
19.3. mailcap 鈥?Mailcap file handling
19.4. mailbox 鈥?Manipulate mailboxes in various formats

 

鎶婃彙閲嶇偣锛岀敤鍒板灏戯紝灏卞澶氬皯锛屽氨閰憋紒鍒媴蹇冩病瀛﹀埌鐨勪細鎬庢牱鎬庢牱锛佹病瀛﹀埌锛屽彲鏄紝鑷繁杩樻槸鎶婄郴缁熸惌寤鸿捣鏉ヤ簡鍟婏紒

鍝堝搱鍝堝搱~瀛ょ殑绯荤粺锛屽揩蹇笂绾垮惂锛亊

 

鍙傝€冭祫鏂欙細

Python SMTP鍙戦€侀偖浠?/a>

The Python 3.5 Standard Library

smtplib 鈥?SMTP protocol client

闃块噷浜慐CS锛?TCP 25绔彛瑙e皝鐢宠

Sendmail Open Source

sendmail瀹夎浣跨敤

linux涔嬪彂閫侀偖浠?-sendmail鏈嶅姟閰嶇疆

TCP鍗忚绔彛鐘舵€佽鏄?nbsp;by jessezeng

sendmail 鎶ラ敊锛歶nable to qualify my own domain name

 


以上是关于Python鍙戦€侀偖浠讹細smtplib銆乻endmail的主要内容,如果未能解决你的问题,请参考以下文章

楂橀樁鍑芥暟涓庡嚱鏁板紡缂栫▼

python鍩虹瀛︿範6-mongodb銆乻ys銆佹帴鍙e紑鍙戙€佹搷浣渆xcel

涓€涓猘jax瀹炵幇琛ㄥ崟涓婁紶鏂囦欢鐨勭鍣?formdata

瀛楃涓瞫lice銆乻ubstring銆乻ubstr

Python 发邮件用 smtplib & email

python内建模块:smtplib