EmEditorみんなでまとめサイト

sendmailマクロ

最終更新:

匿名ユーザー

- view
だれでも歓迎! 編集

sendmailマクロ

元ネタ
ttp://muumoo.jp/news/2006/09/13/0sakuramailmacro.html
あんまり検証してないけどつかえるよ。

// デフォルトのメールヘッダ
var defaultHeader = {
    from: "hoge@hoge.jp",
    to: "hoge@hoge.jp",
    cc: "",
    bcc: "",
    subject: "hoge"
};

// SMTP サーバ設定
var smtp = {
    host: "smtp.example.com",
    port: "25",
    ssl: false,
    auth: false,
    user: "",
    pass: ""
};

//*for Gmail
 var smtp = {
 host: "smtp.gmail.com",
 port: "465",
 ssl: true,
 auth: true,
 user: "your@gmail.com",
 pass: "pass"
 }
*/
// 選択範囲 (なければ全選択) の文字列を取得し、改行で分解
if(document.selection.Text == ""){
    document.selection.selectAll();
    }
var text = document.selection.Text;
var lineCode = "\r\n";
var lines = text.split(lineCode);

// ヘッダとボディを message に格納
var message = new Object();
var bodyFound = false;
for(var i = 0; i < lines.length; i++)
{
    if(!bodyFound)
    {
        if(lines[i].match(/^from: (.*)$/i)) message["from"] = RegExp.$1;
        if(lines[i].match(/^to: (.*)$/i)) message["to"] = RegExp.$1;
        if(lines[i].match(/^cc: (.*)$/i)) message["cc"] = RegExp.$1;
        if(lines[i].match(/^bcc: (.*)$/i)) message["bcc"] = RegExp.$1;
        if(lines[i].match(/^subject: (.*)$/i)) message["subject"] = RegExp.$1;
        if(lines[i] == ""){ bodyFound = true; message["body"] = ""; }
    }
    else
    {
        message["body"] += lines[i] + lineCode;
    }
}

if(5 <= getLength(message))
{
    send_mail(message);
    document.selection.Collapse();
    alert("メールを送信しました。");
}
else
{
    // ヘッダが足りない場合、デフォルトのヘッダを挿入
    var mailHeader =
        "From: " + defaultHeader["from"] + lineCode +
        "To: " + defaultHeader["to"] + lineCode +
        "Cc: " + defaultHeader["cc"] + lineCode +
        "Bcc: " + defaultHeader["bcc"] + lineCode +
        "Subject: " + defaultHeader["subject"] + lineCode +
        lineCode;
    document.write(mailHeader + text);
}

// メール送信
function send_mail(message)
{
    var mail = new ActiveXObject("CDO.Message");
    
    // message
    mail.From = message["from"];
    mail.To = message["to"];
    mail.Cc = message["cc"];
    mail.Bcc = message["bcc"];
    mail.Subject = message["subject"];
    mail.TextBody = message["body"];
    mail.TextBodyPart.Charset = "ISO-2022-JP";
    
    var schemas = "http://schemas.microsoft.com/cdo/configuration/";
    var fields = mail.Configuration.Fields;
    
    // config
    fields.Item(schemas + "sendusing") = 2;
    fields.Item(schemas + "smtpconnectiontimeout") = 30;
    
    // server
    fields.Item(schemas + "smtpserver") = smtp["host"];
    fields.Item(schemas + "smtpserverport") = smtp["port"];
    fields.Item(schemas + "smtpusessl") = smtp["ssl"];
    
    if(smtp["auth"])
    {
        // authentication
        fields.Item(schemas + "smtpauthenticate") = true;
        fields.Item(schemas + "sendusername") = smtp["user"];
        fields.Item(schemas + "sendpassword") = smtp["pass"];
    }
    
    // send
    mail.Configuration.Fields.Update();
    mail.Send();
}

// 連想配列の要素数を取得
function getLength(hash)
{
    var count = 0;
    for(key in hash){
        count++;
    }
    return count;
}

// メッセージボックス
function alert(message) {
    var shell = new ActiveXObject("WScript.Shell");
    shell.Popup(message, 0, "", 0);
}

タグ:

+ タグ編集
  • タグ:

このサイトはreCAPTCHAによって保護されており、Googleの プライバシーポリシー利用規約 が適用されます。

添付ファイル
目安箱バナー