'Mac Terminal Sending Email With Attachment

I'm trying to make a bash script that will send an email to all contacts which will contain a message and an attachment. This is not for malicious purposes.

How could I do this? Is this possible? Thanks in advance.



Solution 1:[1]

I have previously used uuencode to accomplish this:

uuencode source.txt destination.txt | mail -s "subject of mail" [email protected]

You can use this in your bash script. Sample:

uuencode /usr/bin/xxx.c MyFile.c | mail -s "mailing my c file" [email protected]

http://publib.boulder.ibm.com/infocenter/pseries/v5r3/index.jsp?topic=/com.ibm.aix.cmds/doc/aixcmds5/uuencode.htm

Solution 2:[2]

You might also use AppleScript:

tell application "Mail"
    tell (make new outgoing message)
        set subject to "subject"
        set content to "content"
        -- set visible to true
        make new to recipient at end of to recipients with properties {address:"[email protected]", name:"Name"}
        make new attachment with properties {file name:(POSIX file "/tmp/test.txt")} at after the last paragraph
        send
    end tell
end tell

You can use an explicit run handler to pass arguments from a shell:

osascript -e 'on run {a}
    set text item delimiters to ";"
    repeat with l in paragraphs of a
        set {contact, address} to text items of l
    end repeat
end run' "Name1;[email protected]
Name2;[email protected]"

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Lri