邮件发送服务器安装脚本 (亲测)

kevin.Zhu 发布于:2013-11-23 14:08 分类:邮件  有 27 人浏览,获得评论 0 条  

#!/bin/bash -

#更改远程机ssh端口
sed -i "/^Port*/ c Port 60181" /etc/ssh/sshd_config
sed -i "/^#Port*/ c Port 60181" /etc/ssh/sshd_config

echo "*    soft    nofile    409600"  >> /etc/security/limits.conf
echo "*    hard    nofile    409600"  >> /etc/security/limits.conf

#主机名
set_hostname=$1

#操作名
op_name=$2

if [ -z "$set_hostname" ] || [ -z "$op_name" ] ; then
    echo "需要参数 : 主机名, 操作名"  ; exit ;
fi 

# 更新 安装
#yum update -y 
#yum install epel-release -y 
#rpm -Uvh http://mirrors.ustc.edu.cn/fedora/epel/6/x86_64/epel-release-6-8.noarch.rpm
#yum update -y 
yum install -y postfix mail 
yum install rsyslog -y 

#配置postfix 
sed -i 's/#myhostname.*/myhostname = '$set_hostname'/'  /etc/postfix/main.cf
sed -i '0,/#mydomain.*/s//mydomain = '$set_hostname'/'  /etc/postfix/main.cf
sed -i 's/^#myorigin.*mydomain/myorigin = $mydomain/' /etc/postfix/main.cf
#sed -i 's/^inet_interfaces.*/#&/' /etc/postfix/main.cf
#sed -i '0,/#inet_interfaces.*/s//inet_interfaces = all/'  /etc/postfix/main.cf
sed -i 's/^#inet_protocols/inet_protocols/' /etc/postfix/main.cf
sed -i 's/^mydestination/#mydestination/'  /etc/postfix/main.cf
sed -i '/^#mydestination.*mydomain$/s/^#//'   /etc/postfix/main.cf
sed -i 's/^#mynetworks.*127\.0.*/mynetworks = 127.0.0.0\/8/'   /etc/postfix/main.cf
sed -i 's/^#relay_domains.*/relay_domains =  home_mailbox = Maildir\//' /etc/postfix/main.cf
/etc/init.d/postfix restart


#下面安装DKIM 
yum install opendkim -y 

cat <<EOF   >> /etc/opendkim.conf
Canonicalization        relaxed/relaxed
ExternalIgnoreList      refile:/etc/opendkim/TrustedHosts
InternalHosts           refile:/etc/opendkim/TrustedHosts
KeyTable                refile:/etc/opendkim/KeyTable
LogWhy                  Yes
MinimumKeyBits          1024
Mode                    sv
PidFile                 /var/run/opendkim/opendkim.pid
SigningTable            refile:/etc/opendkim/SigningTable
Socket                  inet:8891@127.0.0.1
Syslog                  Yes
SyslogSuccess           Yes
TemporaryDirectory      /var/tmp
UMask                   022
UserID                  opendkim:opendkim
EOF

if [ ! -d "/etc/opendkim/keys" ]; then
    mkdir /etc/opendkim/keys/
fi

#生成秘钥对
opendkim-genkey -D /etc/opendkim/keys/ -d $set_hostname -s default
chown opendkim  /etc/opendkim/keys/default.private

#设置opendkim
echo "default._domainkey.${set_hostname} ${set_hostname}:default:/etc/opendkim/keys/default.private" >>  /etc/opendkim/KeyTable
echo "*@${set_hostname} default._domainkey.${set_hostname}" >> /etc/opendkim/SigningTable 


#让postfix扶持 opendkim 
cat <<EOF >> /etc/postfix/main.cf
smtpd_milters           = inet:127.0.0.1:8891
non_smtpd_milters       = inet:127.0.0.1:8891
milter_protocol         = 2
milter_default_action   = accept
EOF

#启动服务
service opendkim restart
service postfix restart
service rsyslog start

#安装lamp
yum install httpd httpd-devel -y 
yum install php php-devel -y 
/etc/init.d/httpd restart

#自启动所有服务
chkconfig postfix on 
chkconfig opendkim on 
chkconfig httpd on 
chkconfig rsyslog on 


#写入PHP邮件客户端代码

cat <<EOF  > /var/www/html/service.php
<?php
/**
 * 接收post数据,发送邮件 zkf
 * 本页面运行于每台邮件服务器之上、 接收任务服务器发来的邮件内容 并发送
 */
//data数组 ,包含发邮件需要的所有信息
$data = $_POST['data']; 
//解密数据 key值需要与任务机加密用的key一样
$key = 'Zhukefenghao2015@#$' ; 
$data = my_encrypt($data, 'D', $key ) ; 
if(!$data)  exit ;
$data = unserialize($data) ; 

//发件参数
$to        = $data['to'] ;
$subject   = $data['subject'] ;
$message   = $data['message'] ;
$from_man  = $data['from_man'] ; 
$host_name = $data['host_name'] ; 
$mail_name = $data['mail_name'] ; 
$headers = "From: $from_man <{$mail_name}@mail.{$host_name}>\n";
$headers .= "Return-Path: <{$mail_name}@{$host_name}>\n";
//发送html邮件
$headers .= "MIME-Version: 1.0\n";
$headers .= "Content-type: text/html; charset=utf-8\n";
$headers .= "Content-Transfer-Encoding: 8bit\r\n";
ini_set('sendmail_from', "{$mail_name}@{$host_name}");
$message =  wordwrap($message, 70);
//发送
mail($to , $subject, $message , $headers);


//加减密函数
function my_encrypt($string,$operation,$key=''){
     $key=md5($key);
     $key_length=strlen($key);
       $string=$operation=='D'?base64_decode($string):substr(md5($string.$key),0,8).$string;
     $string_length=strlen($string);
     $rndkey=$box=array();
     $result='';
     for($i=0;$i<=255;$i++){
            $rndkey[$i]=ord($key[$i%$key_length]);
         $box[$i]=$i;
     }
     for($j=$i=0;$i<256;$i++){
         $j=($j+$box[$i]+$rndkey[$i])%256;
         $tmp=$box[$i];
         $box[$i]=$box[$j];
         $box[$j]=$tmp;
     }
     for($a=$j=$i=0;$i<$string_length;$i++){
         $a=($a+1)%256;
         $j=($j+$box[$a])%256;
         $tmp=$box[$a];
         $box[$a]=$box[$j];
         $box[$j]=$tmp;
         $result.=chr(ord($string[$i])^($box[($box[$a]+$box[$j])%256]));
     }
     if($operation=='D'){
         if(substr($result,0,8)==substr(md5(substr($result,8).$key),0,8)){
             return substr($result,8);
         }else{
             return'';
         }
     }else{
         return str_replace('=','',base64_encode($result));
     }
}

EOF


#安装完成,重启
echo "All jobs done. Please reboot host ... " ;
echo -n "Auto reboot on 10 sec " ; 
for((i=10 ; i>0 ; i--)) ; do 
    echo -n ". $i";
    sleep 1 ;
done 
  
reboot