Skip navigation.

极湖

无不用其“极”

Posts tagged with "Mail"

删除 Postfix 邮件队列中的特定邮件

, ,

用一个命令组合即可删除 Postfix 邮件队列中的特定邮件。

例:
# mailq | awk '/bad_address@bad_domain/ {print $1}' | tr -d '*' | xargs -n 1 postsuper -d

其中 bad_address@bad_domain 这个字符串根据实际情况修改。

这个命令用到了 awk,tr, xargs 这些命令,很有代表性,因此记录一下。

PHP: 带附件的邮件发送(转发)

, ,

以下代码,用到了Pear::Mail_Mime。
<?php
require_once 'Mail.php'; 
require_once 'Mail/mime.php'; 

/**
 * 
 * 邮件发送或转发(可带附件)*
 * @param        $mail_info     :邮件内容*
 * @param        $forward_to    :转发地址*
 * @return       发送结果*
 * @author       Jlake OU   2007/12/05
**/
function MAIL_forward($mail_info, $forward_to)
{
    $mailObject = Mail::factory("mail");

    $mimeObject = new Mail_Mime("\n");
    $mimeObject->setTxtBody(mb_convert_encoding($mail_info['body'], "ISO-2022-JP", "auto"));

    foreach($mail_info['attachment'] as $path) {
        $mimeObject->addAttachment($path);
    }

    $bodyParam = array(
      "head_charset" => "ISO-2022-JP",
      "text_charset" => "ISO-2022-JP"
    );

    $body = $mimeObject -> get($bodyParam);

    $addHeaders = array(
      "To" => $mail_info['to'],
      "From" => $mail_info['from'],
      "Subject" => mb_encode_mimeheader($mail_info['subject'])
    );

    $headers = $mimeObject->headers($addHeaders);

    return $mailObject->send($forward_to, $headers, $body);
}

//测试
$mail_info = array(
    'from' => 'user@fromdomain.com',
    'to' => 'user@todomain.com',
    'subject' => 'Mail forward test',
    'body' => 'This is a test',
    'attachment' => array(
         '/tmp/test1.jpg'
        ,'/tmp/test2.jpg'
    )
);

MAIL_forward($mail_info, 'user@forwarddomain.com');
?>

以上代码中的邮件编码对应的是日文,若发送中文或是其他语言的邮件,须作相应的更改。

通过管道(stdin)收取邮件并解析的 PHP 代码

, , , ...

本文介绍怎样通过 PEAR::Mail 的 mimeDecode.php 解析收到的邮件(包括附件的处理)

首先,根据邮件系统进行别名设置,如:
diary:      "|/usr/local/bin/php /***/****/diary.php"


如果是qmail,可在收取邮件账号的 .qmail 文件里作如下设置:
|/usr/local/bin/php /***/****/diary.php


diary.php 的内容:
<?php 
  require_once 'Mail/mimeDecode.php'; 
  // 取得邮件内容
  $params['include_bodies'] = true; 
  $params['decode_bodies']  = true; 
  $params['decode_headers'] = true; 
  $params['input'] = file_get_contents("php://stdin"); 
  $params['crlf'] = "\r\n"; 
  $structure = Mail_mimeDecode::decode($params); 

  //取得发送邮件地址
  if(preg_match("/<(.*)>/", $mail_from, $matches)) {
    $mail_from = $matches[1]; 
  }
  //取得目标邮件地址
  $mail_to = $structure->headers['to']; 
  if(preg_match("/<(.*)>/", $mail_to, $matches)) {
    $mail_to = $matches[1]; 
  }

  // 取得邮件标题
  $mail_subject = $structure->headers['subject']; 
   
  switch(strtolower($structure->ctype_primary)){ 
    case "text": // 只有一个Part(文本邮件) 
      $mail_body = $structure->body; 
      break; 
    case "multipart":  // 有多个Part(图片等) 
      foreach($structure->parts as $part){ 
        switch(strtolower($part->ctype_primary)){ 
          case "text": // 正文
            $mail_body = $part->body; 
            break; 
          case "image": // 图片
            //取得图片文件扩展名并转为小写
            $type = strtolower($part->ctype_secondary); 
            //检查是否为JPEG格式(同样可以检查是否为GIF或者PNG格式) 
            if($type != "jpeg" and $type != "jpg"){ 
              continue; 
            } 
            //把附件保存到文件
            $file_mame = $part->ctype_parameters['name'];
            $fp = fopen("/tmp/$file_mame", "w" );
            $length = strlen( $part->body ); 
            fwrite( $fp, $part->body, $length ); 
            fclose( $fp ); 
            break; 
        } 
      } 
    break; 
    default: 
    $mail_body = ""; 
  } 
  /* 
   * 其他处理,如:把邮件地址、标题、正文、图片等信息保存到数据库
   */ 
?>


以上代码只是个框架,可以根据自己的需要修改和扩充。运行该代码需要 Pear 的 Mail 及 Mail_MIME 包,安装方法如下:
$ pear install -a Mail
$ pear install -a Mail_MIME


补充:
邮件内容可能有不同的编码,很多情况下都时候需要转码,方法如下(目标编码根据需要修改):
mb_detect_order("ASCII, JIS, UTF-8, EUC-JP, SJIS");
$mail_subject = mb_convert_encoding($structure->headers['subject'], 'EUC-JP', 'auto');


本文翻译整理自: stdinからのメール処理でメール情報を取得する

通过Shell发送邮件的方法

, ,

一,用mail命令发送

echo "Body" | mail -s "Subject" addr@some.domain

二,用sendmail命令发送

email="to@toaddress.com"
emailname="Test Mail"
messagebody="tmp.tmp"
from="from@fromaddress.com"
fromname="MailReport"
subject="Mail Report"

echo -e "To: \"${emailname}\" <${email}>\nFrom: \"${fromname}\" <${from}>\nSubject: ${subject}\n\n`cat ${messagebody}`" | /usr/sbin/sendmail -t


三,借助‘email’工具发送

echo "<h1>你好</h1><p style=\"color:red; background-color:gray; height:10em;\">html 邮件测试</p>" | email --html --smtp-user 发送邮件所用的帐号 --smtp-pass 发送邮件所用的密码 --subject "这里是标题" --attach 文件1.zip,文件2.jpg,文件3.tar --cc 抄送到邮件地址 -bcc 暗送到邮件地址 收件人邮件地址


VBScript 发送邮件的方法

, ,

1. IIS SMTP + CDONTS.NewMail
Sub SendMailCDONTS(aTo, Subject, TextBody, aFrom)
  Const CdoBodyFormatText = 1
  Const CdoBodyFormatHTML = 0
  Const CdoMailFormatMime = 0
  Const CdoMailFormatText = 1
  Dim Message 'As New cdonts.NewMail
  
  'Create CDO message object
  Set Message = CreateObject("CDONTS.NewMail")
  With Message
    
    'Set email adress, subject And body
    .To = aTo
    .Subject = Subject
    .Body = TextBody
    
    'set mail And body format
    .MailFormat = CdoMailFormatText
    .BodyFormat = CdoBodyFormatText
    
    'Set sender address If specified.
    If Len(aFrom) > 0 Then .From = aFrom
    
    'Send the message
    .Send
  End With
End Sub

2. IIS SMTP + CDO.Message
Sub SendMailCDO(aTo, Subject, TextBody, aFrom)
  Const cdoOutlookExvbsss = 2
  Const cdoIIS = 1
  
  Dim Message 'As New CDO.Message
  
  'Create CDO message object
  Set Message = CreateObject("CDO.Message")
  With Message
    'Load IIS configuration
    .Configuration.Load cdoIIS
    
    'Set email adress, subject And body
    .To = aTo
    .Subject = Subject
    .TextBody = TextBody
    
    'Set sender address If specified.
    If Len(aFrom) > 0 Then .From = aFrom
    
    'Send the message
    .Send
  End With
End Sub

Sub SendMailCDOCacheConf(aTo, Subject, TextBody, aFrom)
  'cached configuration  
  Dim Conf ' As New CDO.Configuration
  If IsEmpty(Conf) Then
    Const cdoOutlookExvbsss = 2
    Const cdoIIS = 1
    Set Conf = CreateObject("CDO.Configuration")
    Conf.Load cdoIIS
  End If
  
  Dim Message 'As New CDO.Message
  
  'Create CDO message object
  Set Message = CreateObject("CDO.Message")
  With Message
    'Set cached configuration
    Set .Configuration = Conf
    
    'Set email adress, subject And body
    .To = aTo
    .Subject = Subject
    .TextBody = TextBody
    
    'Set sender address If specified.
    If Len(aFrom) > 0 Then .From = aFrom
    
    'Send the message
    .Send
  End With
End Sub

3. 远程 SMTP + CDO.Message
修改以上列出函数 SendMailCDOCacheConf 的代码,如下
Sub SendMailCDOCacheConf(aTo, Subject, TextBody, aFrom)
  'cached configuration  
  Dim Conf  'As New CDO.Configuration
  If IsEmpty(Conf) Then
    Const cdoSendUsingPort = 2
    
    Set Conf = CreateObject("CDO.Configuration")
    
    With Conf.Fields
      .Item("http://schemas.microsoft.com/cdo/configuration/sendusing") =   cdoSendUsingPort
      .Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "your.smtp.server"
      .Update
    End With
  End If
  
  Dim Message 'As New CDO.Message
  
  'Create CDO message object
  Set Message = CreateObject("CDO.Message")
  With Message
    'Set cached configuration
    Set .Configuration = Conf
    
    'Set email adress, subject And body
    .To = aTo
    .Subject = Subject
    .TextBody = TextBody
    
    'Set sender address If specified.
    If Len(aFrom) > 0 Then .From = aFrom
    
    'Send the message
    .Send
  End With
End Sub

4. Outlook
Sub SendMailOutlook(aTo, Subject, TextBody, aFrom)
  
  'Create an Outlook object
  Dim Outlook 'As New Outlook.Application
  Set Outlook = CreateObject("Outlook.Application")
  
  'Create e new message
  Dim Message 'As Outlook.MailItem
  Set Message = Outlook.CreateItem(olMailItem)
  With Message
    'You can display the message To debug And see state
    '.Display
    
    .Subject = Subject
    .Body = TextBody
    
    'Set destination email address
    .Recipients.Add (aTo)
    
    'Set sender address If specified.
    Const olOriginator = 0
    If Len(aFrom) > 0 Then .Recipients.Add(aFrom).Type = olOriginator
    
    'Send the message
    .Send
  End With
End Sub
December 2009
S M T W T F S
November 2009January 2010
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31