Press "Enter" to skip to content

标签: 邮件

python写的简单发送邮件的脚本【转】

近来有些东西需要监控报警发邮件,然后在网上找了点材料,自己写了一个简单发送邮件的脚本,主要就是运用python的smtplib模块,分享给大家看一下:

#!/usr/bin/env python 
# -*- coding: utf-8 -*- 
#导入smtplib和MIMEText 
import smtplib,sys 
from email.mime.text import MIMEText 
 
def send_mail(sub,content): 
    ############# 
    #要发给谁,这里发给1个人 
    mailto_list=["wangwei03@gyyx.cn"] 
    ##################### 
    #设置服务器,用户名、口令以及邮箱的后缀 
    mail_host="mail.gyyx.cn" 
    mail_user="wangwei03@gyyx.cn" 
    mail_pass="123456677890" 
    mail_postfix="gyyx.cn" 
    ###################### 
    ''''' 
    to_list:发给谁 
    sub:主题 
    content:内容 
    send_mail("aaa@126.com","sub","content") 
    ''' 
    me=mail_user+"<"+mail_user+"@"+mail_postfix+">" 
    msg = MIMEText(content,_charset='gbk') 
    msg['Subject'] = sub 
    msg['From'] = me 
    msg['To'] = ";".join(mailto_list) 
    try: 
        s = smtplib.SMTP() 
        s.connect(mail_host) 
        s.login(mail_user,mail_pass) 
        s.sendmail(me, mailto_list, msg.as_string()) 
        s.close() 
        return True 
    except Exception, e: 
        print str(e) 
        return False 
if __name__ == '__main__': 
    if send_mail(u'这是python测试邮件',u'python发送邮件'): 
        print u'发送成功' 
    else: 
        print u'发送失败' 

转自:http://wangwei007.blog.51cto.com/68019/978743

Leave a Comment

CentOS 5.6 X86_64系统下Postfix邮件系统配置【pop3,smtp】

硬件环境:
服务器型号:IBM X336
CPU型号: Intel(R) Xeon(TM) CPU 3.80GHz *2颗
内存型号: 2G*4根
硬盘型号:SCSI 73G*2块

软件配置:
系统:CentOS5.6 x86_64
软件:Nginx1.0.4+PHP5.3.6(FPM-FCGI)+Mysql5.5.3

Linux系统最小安装,在此就不多说了。此机器主要功能是对外进行图片访问,由于公司需要邮件系统进行邮件发送和接收,考虑Qmail配置太复杂,sendmail配置也麻烦所以就选择postfix作为邮件处理系统。

1.PostFix安装配置
2.dovecot安装配置
3.防火墙配置
4.测试邮件发送

1.PostFix安装配置
第一步:首先进行安装postfix

yum -y install postfix

第二步:配置main.cf文件

[root@kiccleaf ~]# vim /etc/postfix/main.cf
myhostname = mail.kiccleaf.com
mydomain = kiccleaf.com
myorigin=$mydomain
inet_interfaces = all
mydestination = $myhostname, localhost.$mydomain, localhost,$mydomain
relay_domains = $mydestination
mynetworks = 192.168.1.0/28, 127.0.0.0/8,60.191.49.228/24 #填写自己的公网ip
home_mailbox = Maildir/
#smtpd_banner = $myhostname ESMTP $mail_name ($mail_version)  ← 找到这一行,接此行添加如下行:
smtpd_banner = $myhostname ESMTP unknow ← 添加这一行,不显示SMTP服务器的相关信息

#Add end
smtpd_sasl_auth_enable = yes
smtpd_sasl_local_domain = $myhostname
smtpd_sasl_security_options = noanonymous
smtpd_recipient_restrictions = permit_mynetworks, permit_sasl_authenticated, reject_unauth_destination
message_size_limit = 15728640

第三步:配置smtpd.conf

[root@kiccleaf ~]# vim /usr/lib64/sasl2/smtpd.conf

pwcheck_method: saslauthd
改成
pwcheck_method: auxprop

第四步:配置postfix随系统启动

[root@kiccleaf ~]# chkconfig saslauthd on
[root@kiccleaf ~]# chkconfig --list saslauthd
saslauthd      	0:off	1:off	2:on	3:on	4:on	5:on	6:off

[root@kiccleaf ~]# chkconfig postfix on
[root@kiccleaf ~]# chkconfig --list postfix
postfix        	0:off	1:off	2:on	3:on	4:on	5:on	6:off

第五步:启动postfix服务

[root@kiccleaf ~]# /etc/rc.d/init.d/saslauthd start
Starting saslauthd:                                        [  OK  ]

[root@kiccleaf ~]# /etc/rc.d/init.d/postfix start
Starting postfix:                                          [  OK  ]

第六步:对postfix进行测试

telnet localhost 25
Trying 127.0.0.1...
Connected to localhost.localdomain (127.0.0.1).
Escape character is '^]'.
220 kiccleaf.com ESMTP unknow
ehlo localhost
250-kiccleaf.com
250-PIPELINING
250-SIZE 15728640
250-VRFY
250-ETRN
250-AUTH PLAIN LOGIN
250-ENHANCEDSTATUSCODES
250-8BITMIME
250 DSN
quit
221 2.0.0 Bye
Connection closed by foreign host.

第七步:添加配置,用户和密码

 [root@kiccleaf ~]# mkdir /data/Maildir
 [root@kiccleaf ~]# chmod 700 /data/Maildir
 [root@kiccleaf ~]# mkdir /home/leaf/Maildir
 [root@kiccleaf ~]# chmod 700 /home/leaf/Maildir
 [root@kiccleaf ~]# chown leaf. /home/leaf/Maildir
 [root@kiccleaf ~]# saslpasswd2 -u mail.kiccleaf.com -c leaf  #添加帐号leaf
 Password:          #输入密码
 Again (for verification):    #输入重复密码
 [root@kiccleaf ~]# chgrp postfix /etc/sasldb2
 [root@kiccleaf ~]# chmod 640 /etc/sasldb2
 [root@kiccleaf ~]# alternatives --config mta

There are 2 programs which provide 'mta'.

Selection Command
 -----------------------------------------------
 *+ 1 /usr/sbin/sendmail.sendmail
 2 /usr/sbin/sendmail.postfix

Enter to keep the current selection[+], or type selection number: 2      #选择postfix作为邮件服务
 

2.接下去安装pop3接收服务dovecot
第一步:dovecot服务安装

yum -y install dovecot

第二步:编辑dovecot.conf文件

找到#protocols = imap imaps pop3 pop3s行添加或是修改:
protocols = imap pop3
老版本CentOS5.5版本是以default_mail_env作为配置
mail_location = maildir:~/Maildir

第三步:配置dovecot随系统启动

[root@kiccleaf ~]# chkconfig dovecot on
[root@kiccleaf ~]# chkconfig --list dovecot
dovecot        	0:off	1:off	2:on	3:on	4:on	5:on	6:off

第四步:启动dovecot服务

[root@kiccleaf ~]# /etc/rc.d/init.d/dovecot start
Starting Dovecot Imap:                                     [  OK  ]

第五步:对dovecot进行测试

telnet localhost 110
Trying 127.0.0.1...
Connected to localhost.localdomain (127.0.0.1).
Escape character is '^]'.
+OK Dovecot ready.
quit
+OK Logging out
Connection closed by foreign host.

3.防火墙配置

[root@kiccleaf ~]# vim /etc/sysconfig/iptables
#添加以下三个端口25,110,143
-A INPUT -m state --state NEW -m tcp -p tcp --dport 25 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 110 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 143 -j ACCEPT

#重启防火墙
[root@kiccleaf ~]# service iptables restart
Flushing firewall rules:                                   [  OK  ]
Setting chains to policy ACCEPT: filter                    [  OK  ]
Unloading iptables modules:                                [  OK  ]
Applying iptables firewall rules:                          [  OK  ]
Loading additional iptables modules: ip_conntrack_netbios_n[  OK  ]
#查看监听端口列表
[root@kiccleaf ~]# netstat -an
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address               Foreign Address             State
tcp        0      0 127.0.0.1:9000              0.0.0.0:*                   LISTEN
tcp        0      0 0.0.0.0:2188                0.0.0.0:*                   LISTEN
tcp        0      0 0.0.0.0:110                 0.0.0.0:*                   LISTEN
tcp        0      0 0.0.0.0:143                 0.0.0.0:*                   LISTEN
tcp        0      0 0.0.0.0:80                  0.0.0.0:*                   LISTEN
tcp        0      0 0.0.0.0:58008               0.0.0.0:*                   LISTEN
tcp        0      0 127.0.0.1:25                0.0.0.0:*                   LISTEN
tcp        0      0 192.168.1.9:22           192.168.1.22:2850           ESTABLISHED

4.测试邮件发送
为了方便Foxmail作为客户端进行配置,填写ip为pop3,smtp,填写帐号名称及密码,进行发送测试,在此用php编写简要的发送代码进行测试。

<?php
$to      = '53045936@qq.com';
$subject = 'the subject test';
$message = 'hello!测试用例!';
$headers = 'From: leaf@kiccleaf.com' . "\r\n" .
'Reply-To:  leaf@kiccleaf.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();

mail($to, $subject, $message, $headers);
?>

至此邮件系统的发送和接收都已经配置完成

2 Comments

postfix 删除队列中的所有邮件

邮件系统被其他人非法利用转发,所以队列中太多垃圾邮件
删除所有队列中的邮件

先执行mailq命令查看邮件队列
[root@kiccleaf ]# mailq
B0B1E511DC7*    5260 Mon Aug  1 15:18:14  rztdmmcrfn@yahoo.com
                                         macrothere@yahoo.com.tw
                                         lock90460@yahoo.com.tw
                                         liuhsinni@yahoo.com.tw
ACB765D7A8B*     344 Wed Aug  3 02:59:13  bnuga@ms54.hinet.net
                                         a560909a@yahoo.com.tw
                                         a7115968@yahoo.com.tw
#我们发现有很多这样的垃圾邮件在队列中,可以采用Crtl+C进行终止列表
#执行邮件删除操作命令,记住参数ALL一定要大写
[root@kiccleaf ]# postsuper -d ALL
postsuper: Deleted: 148468 messages
#总共删除了148468条数据
Leave a Comment

【原创】CentOS5.5下postfix邮件系统安装配置

CentOS5.5下PostFix邮件系统配置,采用比较简单的yum来进行安装,当然也可以用源码进行编译安装。

邮件postfix发送安装配置

[root@dyq ~]# yum -y install postfix #输入安装postfix
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
* addons: centos.ustc.edu.cn
* base: centos.ustc.edu.cn
* extras: centos.ustc.edu.cn
* updates: centos.ustc.edu.cn
addons                                                              |  951 B     00:00
base                                                                | 2.1 kB     00:00
extras                                                              | 2.1 kB     00:00
updates                                                             |  951 B     00:00
Setting up Install Process
Resolving Dependencies
--&gt; Running transaction check
---&gt; Package postfix.i386 2:2.3.3-2.1.el5_2 set to be updated
--&gt; Finished Dependency Resolution

Dependencies Resolved

====================================================
Package             Arch             Version                       Repository        Size
====================================================
Installing:
postfix             i386             2:2.3.3-2.1.el5_2             base             3.6 M

Transaction Summary
====================================================
Install       1 Package(s)
Upgrade       0 Package(s)

Total download size: 3.6 M
Downloading Packages:
postfix-2.3.3-2.1.el5_2.i386.rpm                                    | 3.6 MB     00:27
Running rpm_check_debug
Running Transaction Test
Finished Transaction Test
Transaction Test Succeeded
Running Transaction
Installing     : postfix                                                             1/1

Installed:
postfix.i386 2:2.3.3-2.1.el5_2

Complete!
[root@dyq ~]# vim /etc/postfix/main.cf

myhostname = mail.kiccleaf.com
mydomain = kiccleaf.com
myorigin=$mydomain
inet_interfaces = all
mydestination = $myhostname, localhost.$mydomain, localhost,$mydomain
relay_domains = $mydestination
mynetworks = 192.168.1.0/28, 127.0.0.0/8,60.191.49.228/24 #填写自己的公网ip
home_mailbox = Maildir/
#smtpd_banner = $myhostname ESMTP $mail_name ($mail_version)  ← 找到这一行,接此行添加如下行:
smtpd_banner = $myhostname ESMTP unknow ← 添加这一行,不显示SMTP服务器的相关信息

在配置文件的文尾,添加如下行:

smtpd_sasl_auth_enable = yes  ← 服务器使用SMTP认证
smtpd_sasl_local_domain = $myhostname  ← 指定SMTP认证的本地域名(主机名)
smtpd_sasl_security_options = noanonymous   ← 不允许匿名的方式认证
smtpd_recipient_restrictions = permit_mynetworks, permit_sasl_authenticated, reject_unauth_destination
message_size_limit = 15728640

[root@dyq ~]# vi /usr/lib/sasl2/smtpd.conf
pwcheck_method: saslauthd  ← 找到此行,将“saslauthd”改为“auxprop”
↓
pwcheck_method: auxprop  ← 不使用系统用户密码作为用户的SMTP认证密码

[root@dyq ~]# vi /etc/sysconfig/saslauthd
MECH=shadow  ← 找到这一行,在前面加#
↓
#MECH=shadow  ← 不使用shadow机制

FLAGS=  ← 找到此行,在等号后面添加“sasldb”
↓
FLAGS=sasldb   ← 定义认证方式为sasldb2

[root@dyq ~]# mkdir /etc/skel/Maildir
[root@dyq ~]# chmod 700 /etc/skel/Maildir
[root@dyq ~]# ll /home/
total 8
drwx------ 2 mysql mysql 4096 Mar 18 00:51 mysql
drwx------ 2 www   www   4096 Mar 18 01:52 www
[root@dyq ~]# adduser leaf #添加一个用户leaf
[root@dyq ~]# ll /home/ #查看用户是否创建
total 12
drwx------ 3 leaf  leaf  4096 Mar 21 11:02 leaf
drwx------ 2 mysql mysql 4096 Mar 18 00:51 mysql
drwx------ 2 www   www   4096 Mar 18 01:52 www
[root@dyq ~]# passwd leaf #配置leaf用户的密码
Changing password for user leaf.
New UNIX password:                         #输入leaf用户密码
BAD PASSWORD: it is too simplistic/systematic
Retype new UNIX password:       #再次输入leaf用户密码
passwd: all authentication tokens updated successfully.

[root@dyq ~]# mkdir /home/leaf/Maildir (默认情况下已经创建)
mkdir: cannot create directory `/home/leaf/Maildir': File exists

[root@dyq ~]# chmod 700 /home/leaf/Maildir
[root@dyq ~]# chown leaf. /home/leaf/Maildir
[root@dyq ~]# saslpasswd2 -u mail.kiccleaf.com -c leaf
Password: #输入leaf用户密码
Again (for verification):#再次输入leaf用户密码

[root@dyq ~]# chgrp postfix /etc/sasldb2
[root@dyq ~]# chmod 640 /etc/sasldb2
[root@dyq ~]# alternatives --config mta

There are 2 programs which provide 'mta'.

Selection    Command
-----------------------------------------------
*+ 1           /usr/sbin/sendmail.sendmail
2           /usr/sbin/sendmail.postfix

Enter to keep the current selection[+], or type selection number: 2 #选择2

[root@dyq ~]# chkconfig saslauthd on #添加到服务,以后可以直接用service saslauthd [start/stop/restart]进行操作
[root@dyq ~]# chkconfig --list saslauthd #查看是否为系统自动启动 2~5 为on
saslauthd          0:off    1:off    2:on    3:on    4:on    5:on    6:off
[root@dyq ~]# /etc/rc.d/init.d/saslauthd start #启动服务
Starting saslauthd:                                        [  OK  ]
[root@dyq ~]# chkconfig postfix on #添加到服务,以后可以直接用service postfix [start/stop/restart]进行操作
[root@dyq ~]# chkconfig --list postfix #查看是否为系统自动启动 2~5 为on
postfix            0:off    1:off    2:on    3:on    4:on    5:on    6:off
[root@dyq ~]# /etc/rc.d/init.d/postfix start #启动服务
Starting postfix:                                          [  OK  ]

POP / IMAP 服务器的构建( Dovecot )

[root@dyq ~]# yum -y install dovecot
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
* addons: centos.ustc.edu.cn
* base: centos.ustc.edu.cn
* extras: centos.ustc.edu.cn
* updates: centos.ustc.edu.cn
addons                                                              |  951 B     00:00
base                                                                | 2.1 kB     00:00
extras                                                              | 2.1 kB     00:00
updates                                                             |  951 B     00:00
Setting up Install Process
Resolving Dependencies
--&gt; Running transaction check
---&gt; Package dovecot.i386 0:1.0.7-7.el5 set to be updated
--&gt; Processing Dependency: libmysqlclient.so.15 for package: dovecot
--&gt; Processing Dependency: libmysqlclient.so.15(libmysqlclient_15) for package: dovecot
--&gt; Processing Dependency: libpq.so.4 for package: dovecot
--&gt; Running transaction check
---&gt; Package mysql.i386 0:5.0.77-4.el5_5.4 set to be updated
--&gt; Processing Dependency: perl(DBI) for package: mysql
---&gt; Package postgresql-libs.i386 0:8.1.22-1.el5_5.1 set to be updated
--&gt; Running transaction check
---&gt; Package perl-DBI.i386 0:1.52-2.el5 set to be updated
--&gt; Finished Dependency Resolution

Dependencies Resolved

===================================================
Package                  Arch          Version                     Repository        Size
===================================================
Installing:
dovecot                  i386          1.0.7-7.el5                 base             1.6 M
Installing for dependencies:
mysql                    i386          5.0.77-4.el5_5.4            updates          4.8 M
perl-DBI                 i386          1.52-2.el5                  base             600 k
postgresql-libs          i386          8.1.22-1.el5_5.1            updates          196 k

Transaction Summary
==================================================
Install       4 Package(s)
Upgrade       0 Package(s)

Total download size: 7.2 M
Downloading Packages:
(1/4): postgresql-libs-8.1.22-1.el5_5.1.i386.rpm                    | 196 kB     00:01
(2/4): perl-DBI-1.52-2.el5.i386.rpm                                 | 600 kB     00:03
(3/4): dovecot-1.0.7-7.el5.i386.rpm                                 | 1.6 MB     00:09
(4/4): mysql-5.0.77-4.el5_5.4.i386.rpm                              | 4.8 MB     00:34
-------------------------------------------------------------------------------------------
Total                                                      151 kB/s | 7.2 MB     00:48
Running rpm_check_debug
Running Transaction Test
Finished Transaction Test
Transaction Test Succeeded
Running Transaction
Installing     : perl-DBI                                                            1/4
Installing     : mysql                                                               2/4
warning: /etc/my.cnf created as /etc/my.cnf.rpmnew
Installing     : postgresql-libs                                                     3/4
Installing     : dovecot                                                             4/4

Installed:
dovecot.i386 0:1.0.7-7.el5

Dependency Installed:
mysql.i386 0:5.0.77-4.el5_5.4                      perl-DBI.i386 0:1.52-2.el5
postgresql-libs.i386 0:8.1.22-1.el5_5.1

Complete!

[root@dyq ~]# vi /etc/dovecot.conf
protocols = imap pop3
default_mail_env = maildir:~/Maildir

#如果开启防火墙Iptables 请添加以下端口

[root@dyq ~]# iptables -A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp--dport 25 -j ACCEPT
[root@dyq ~]# iptables -A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 110 -j ACCEPT
[root@dyq ~]# iptables -A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 143 -j ACCEPT
[root@dyq ~]# /etc/rc.d/init.d/iptables save #保存刚添加的记录
Saving firewall rules to /etc/sysconfig/iptables:          [  OK  ]
[root@dyq ~]# /etc/init.d/iptables restart #重启防火墙
Flushing firewall rules:                                   [  OK  ]
Setting chains to policy ACCEPT: filter                    [  OK  ]
Unloading iptables modules:                                [  OK  ]
Applying iptables firewall rules:                          [  OK  ]
Loading additional iptables modules: ip_conntrack_netbios_n[  OK  ]
[root@dyq ~]# chkconfig dovecot on #添加到服务,以后可以直接用service dovecot [start/stop/restart]进行操作
[root@dyq ~]# chkconfig --list dovecot #查看是否为系统自动启动 2~5 为on
dovecot            0:off    1:off    2:on    3:on    4:on    5:on    6:off
[root@dyq ~]# /etc/rc.d/init.d/dovecot start #启动服务
Starting Dovecot Imap:                                     [  OK  ]

测试php邮件发送

[root@dyq www]# vim mail.php

<?php
$to      = 'kiccleaf@163.com';
$subject = 'the subject test';
$message = 'hello!测试用例!';
$headers = 'From: leaf@kiccleaf.com' . "\r\n" .
'Reply-To:  leaf@kiccleaf.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();

mail($to, $subject, $message, $headers);
?>

打开浏览器http://youip/mail.php   再查看邮件是否已经收到邮件

也可以用foxmail客户端进行测试。

Leave a Comment