sendmail命令中邮件主题未显示的问题求助
Hey there! Let's break down what's happening with your command and get that subject line working properly.
The Root Cause
Your current command:
echo "Subject: hello" | sendmail -v test@example.com < mail.txt
has a conflict in how it feeds input to sendmail. When you use < mail.txt, you're telling sendmail to read only the content of mail.txt as the email data. The echo "Subject: hello" piped into sendmail gets completely ignored because file redirection takes priority over standard input from the pipe.
Fixes to Try
1. Add the Subject directly to mail.txt (Simplest Approach)
Edit your mail.txt to start with the Subject line, followed by a blank line (critical for separating email headers from the body):
Subject: hello [Your existing mail content here]
Then run the simplified command:
sendmail -v test@example.com < mail.txt
Email clients rely on that blank line to distinguish headers (like Subject) from the message body, so don't skip it!
2. Combine the Subject and mail.txt content without editing the file
If you don't want to modify mail.txt, concatenate the Subject line and the file content before passing it to sendmail. Use one of these commands:
Option A (using curly braces for grouping):
{ echo "Subject: hello"; echo ""; cat mail.txt; } | sendmail -v test@example.com
Option B (using cat to merge standard input and the file):
echo -e "Subject: hello\n\n" | cat - mail.txt | sendmail -v test@example.com
The -e flag in echo lets us use \n to add newlines, creating that required blank line between the Subject header and the body.
3. Use the -s flag (if your system's sendmail supports it)
Some versions of sendmail (like the one bundled with Postfix) support a -s flag to specify the subject directly. If available on your system, you can run:
sendmail -v -s "hello" test@example.com < mail.txt
Note: This flag isn't universal across all sendmail implementations, so test it first.
内容的提问来源于stack exchange,提问作者Bhavith Balakrishnan

