emailrelay-set-envelope-from.jsΒΆ

//
// SPDX-FileCopyrightText: 2026 Graeme Walker <graeme_walker@users.sourceforge.net>
// SPDX-License-Identifier: FSFAP
//
// Copyright (c) 2026 Graeme Walker <graeme_walker@users.sourceforge.net>
//
// Copying and distribution of this file, with or without modification,
// are permitted in any medium without royalty provided the copyright
// notice and this notice are preserved.  This file is offered as-is,
// without any warranty.
// ===
//
// emailrelay-set-envelope-from.js
//
// An example E-MailRelay filter script for Windows that
// sets the message's envelope "from" field to a fixed
// value.
//
try
{
    var new_from = 'noreply@example.com' ;

    // parse the command-line to get the envelope filename
    var content = WScript.Arguments(0) ;
    var envelope = WScript.Arguments(1) ;

    // open the envelope file
    var fs = WScript.CreateObject( "Scripting.FileSystemObject" ) ;
    var ts = fs.OpenTextFile( envelope , 1 , false ) ;

    // read the contents of the envelope file
    var txt = ts.ReadAll() ;
    ts.Close() ;

    // update the from address
    var re = new RegExp( "X-MailRelay-From: *\\S*" ) ;
    txt = txt.replace( re , "X-MailRelay-From: " + new_from ) ;

    // write the envelope file back out
    ts = fs.OpenTextFile( envelope , 2 , false ) ;
    ts.Write( txt ) ;
    ts.Close() ;

    // successful exit
    WScript.Quit( 0 ) ;
}
catch( e )
{
    // report errors using the special <<...>> markers
    WScript.StdOut.WriteLine( "<<edit failed>>" ) ;
    WScript.StdOut.WriteLine( "<<" + e.name + ": " + e.message + ">>" ) ;
    WScript.Quit( 1 ) ;
}