• Recent blogs

    Linux : How to send mails with attachments using mailx command

    Linux  : How to send mails with attachments using mailx command

    mailx attachment

    Syntax to send emails from linux machine using mailx command is show below :

    # echo "E-mail Body" | mailx -vvv -s "test mail"  someone@address

    -vvv =        Verbosity.
    -s   =        Specifies the subject.

    [root@mail-server ~]# echo "This is atest E-mail" | mailx -vvv -s "test mail"  manas.tri@gmail.com
    Mail Delivery Status Report will be mailed to <root>.
    You have new mail in /var/spool/mail/root
    [root@mail-server ~]#

    If the mailx version is below 12.x, you can use the uuencode command to send mails with attachments.

    # uuencode [path/to/file] [name_of_attachment] | mailx -s "Subject" user@domain.com

    -a switch in mailx command

    Use the new attachment switch (-a) in mailx to send attachments with the mail. The -a options is easier to use than the uuencode command.

    # mailx -a file.txt -s "Subject" user@domain.com

    The above command will print a new blank line. Type the body of the message here and press [ctrl] + [d] or simply a DOT to send. This will attach the file to the outbound email correctly with proper Content-Type and boundary headers.

    [root@mail-server ~]# echo good > file.txt
    [root@mail-server ~]#
    [root@mail-server ~]#
    [root@mail-server ~]# mailx -a file.txt -s "Test" manas.tri@gmail.com
    This is an attachment
    [root@mail-server ~]#

    To make this more “scriptable”, you can send the message without a body with the following:

    # mailx -a file.txt -s "Subject" user@domain.com < /dev/null

    To send mails with a message body, replace /dev/null in above command with your message body file.

    No comments