//
// 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-from.js
//
// An example E-MailRelay filter script that edits the
// content originator fields (ie. From, Sender and Reply-To)
// to a fixed value.
//
// This does not set the envelope-from field in the envelope
// file by default. If the remote server expects the SMTP
// MAIL-FROM address to match the "From:" header then set
// 'edit_envelope' to true.
//
// See also: emailrelay-set-from.pl, RFC-2822
//
try
{
var edit_envelope = false ; // <= set true to also edit the envelope
var new_from = 'noreply@example.com' ; // <= edit this
var new_sender = '' ;
var new_reply_to = new_from ;
var content = WScript.Arguments( 0 ) ;
var envelope = WScript.Arguments( 1 ) ;
var fs = WScript.CreateObject( "Scripting.FileSystemObject" ) ;
var in_ = fs.OpenTextFile( content , 1 , false ) ;
var out_ = fs.OpenTextFile( content + ".tmp" , 8 , true ) ;
var re_from = /^From:/i ;
var re_sender = /^Sender:/i ;
var re_reply_to = /^Reply-To:/i ;
var re_fold = /^[ \t]/ ;
var in_edit = 0 ;
while( !in_.AtEndOfStream )
{
var line = in_.ReadLine() ;
if( line === "" )
{
out_.WriteLine( line ) ;
break ;
}
if( line.match(re_from) && new_from !== null )
{
in_edit = 1 ;
line = "From: " + new_from ;
out_.WriteLine( line ) ;
}
else if( line.match(re_sender) && new_sender !== null )
{
in_edit = 1 ;
line = "Sender: " + new_sender ;
if( new_sender !== "" )
{
out_.WriteLine( line ) ;
}
}
else if( line.match(re_reply_to) && new_reply_to !== null )
{
in_edit = 1 ;
line = "Reply-To: " + new_reply_to ;
out_.WriteLine( line ) ;
}
else if( in_edit && line.match(re_fold) )
{
}
else
{
in_edit = 0 ;
out_.WriteLine( line ) ;
}
}
while( !in_.AtEndOfStream )
{
var body_line = in_.ReadLine() ;
out_.WriteLine( body_line ) ;
}
in_.Close() ;
out_.Close() ;
fs.DeleteFile( content ) ;
fs.MoveFile( content + ".tmp" , content ) ;
if( edit_envelope )
{
var ts = fs.OpenTextFile( envelope , 1 , false ) ;
var txt = ts.ReadAll() ;
ts.Close() ;
var re = new RegExp( "X-MailRelay-From: *\\S*" ) ;
txt = txt.replace( re , "X-MailRelay-From: " + new_from ) ;
ts = fs.OpenTextFile( envelope , 2 , false ) ;
ts.Write( txt ) ;
ts.Close() ;
}
WScript.Quit( 0 ) ;
}
catch( e )
{
WScript.StdOut.WriteLine( "<<edit failed>>" ) ;
WScript.StdOut.WriteLine( "<<" + e + ">>" ) ;
WScript.Quit( 1 ) ;
}