in reply to content-length first line
Your approach is rather fragile and might be a source of trouble, for example if $subject or $emailid are not properly escaped.