Email file script

Status
Not open for further replies.

kcgoatboy

Dabbler
Joined
Mar 17, 2014
Messages
10
Hello, I've been using freenas for awhile now but have not gotten into any user defined scripts. FreeBSD or any type of Unix/Linux commands are not my strong suite at all. I've tried the trusty google but haven't had much luck making sense of what I found. Can someone help me with a simple script that I can use to create a cron job to automatically send an email once a week. Basically I'm emailing my printer to print a test page once a week to prevent the print head from drying out. I think I can figure out setting up the cron job but the script to send the email is what I mostly need help with.

Goal: Email a file (word doc) once a week to a specified email address.

Thank you for any help.
 

Bidule0hm

Server Electronics Sorcerer
Joined
Aug 5, 2013
Messages
3,710
Yeah that's what I do in one of my scripts (http://pastebin.com/syF2JeAU) but it's not compatible with some mail clients. It works with gmail but not with outlook IIRC. And you can't send text at the same time as the email body needs to be only the file for this to work.

It's not the best thing ever but it's the only way I found I could send mails with an attachment without installing things on the system.
 
Last edited:

kcgoatboy

Dabbler
Joined
Mar 17, 2014
Messages
10
Haven't tried this myself yet (just about to head out), but perhaps:

Reference: http://unix.stackexchange.com/questions/102092/mail-send-email-with-attachment-from-commandline
It didn't work for me. It just sits there and if I kill the command then it send an email with a blank body but not the attachment.

uuencode /mnt/zbpool1/zbdocs/testpage2.docx | mail -s subject "my email"

I've tried mutt but it's not installed. I've tried mailx -a but it says -a is an illegal option.
 

Mirfster

Doesn't know what he's talking about
Joined
Oct 2, 2015
Messages
3,215
Command would be like this; just replace "%EMailSubject%" and "%YourE-MailAddress%":

uuencode "/mnt/zbpool1/zbdocs/testpage2.docx" "testpage2.docx" | mail -s "%EMailSubject%" "%YourE-MailAddress%"

My Outlook client client didn't like the e-mail too much though (as noted earlier by Bidule0hm). Is there a particular reason you have to send an attachment?
 

kcgoatboy

Dabbler
Joined
Mar 17, 2014
Messages
10
Command would be like this; just replace "%EMailSubject%" and "%YourE-MailAddress%":

uuencode "/mnt/zbpool1/zbdocs/testpage2.docx" "testpage2.docx" | mail -s "%EMailSubject%" "%YourE-MailAddress%"

My Outlook client client didn't like the e-mail too much though (as noted earlier by Bidule0hm). Is there a particular reason you have to send an attachment?

It didn't quite work. It sent the email and attached a file called testpage2.docx but it was only 1kb instead of 1117kb and wouldn't open, seemed corrupted.

I have an Epson printer with Epson connect, it has it's own email address so you can email it and it will print whatever you email it. The testpage2.docx is a printer test page I made. My goal is to set up a cron job that will email this doc to the printer once a week and print so that it keeps the ink in the print head fresh and hopefully prevent it from drying out. I've email from gmail the doc in question as an attachment to the printer and it prints. Currently with my freenas box I am just testing seeing if I can get it to email my gmail account before I try to set up the cron job to the printer. The reason why I need to send it as an attachment it to print all the colors, a text email printing out would only use my black ink.

*edit* Well I went and looked in the folder and there was a file with the same name and extension but only 1kb. I don't know how it got there, must have been something that I did when trying to email it. I deleted the file and the above command now reports that there is no such file or dir location. It then sends the email with a blank body and no attachment.
 
Last edited:

jgreco

Resident Grinch
Joined
May 29, 2011
Messages
18,680

jgreco

Resident Grinch
Joined
May 29, 2011
Messages
18,680
It didn't quite work. It sent the email and attached a file called testpage2.docx but it was only 1kb instead of 1117kb and wouldn't open, seemed corrupted.

The problem you're going to face is that you need a properly formatted mail message to send. Your printer isn't going to decode random crap sent to it. It's totally possible to create the right incantation, but the thought occurs...

Is there a reason you want to do this via e-mail rather than just connecting to the printer and sending the data directly?

Anyways, to get the email thing to work, mail yourself a copy of the thing that "works right." That specifically means that if you "cc" it to your printer that the printer happily prints it. Then see if you can save a copy of the message to a file in "mbox" format. Not a copy of the attachment... a copy of the message. Headers and all.

The trick here is that you're forcing a mail client to do the encoding and formatting for you. Then remove superfluous headers on the saved message, twiddle the "From" and "To" accordingly, make sure you've left the MIME headers alone, and you can redirect that file into /usr/sbin/sendmail -t and that has a good chance of working without requiring twitchy shell scripting.
 

Mirfster

Doesn't know what he's talking about
Joined
Oct 2, 2015
Messages
3,215
This is why stackexchange is often useless. That's not "with attachment". That's "with binary content inline". Attachments are a part of MIME, so you need the MIME encoding. The second example is much closer to correct.
Yeah, discovering that as well. It is not an attachment in the true sense; but more in the sense of in the message body.

OP may be better off thinking of a different route. Could even look at possibly scheduling a task in Windows that would simply send a print job or seeing if g-mail has a method of sending scheduled e-mails itself (don't know since I don't use g-mail).
 

jgreco

Resident Grinch
Joined
May 29, 2011
Messages
18,680
Yeah, discovering that as well. It is not an attachment in the true sense; but more in the sense of in the message body.

Well, MIME needs both body and header to be reliably handled.

As an e-mail admin, I fondly, fondly, FONDLY remember the old days where e-mail was text and MIME didn't exist. But then I still read e-mail on a shell box.

OP may be better off thinking of a different route. Could even look at possibly scheduling a task in Windows that would simply send a print job or seeing if g-mail has a method of sending scheduled e-mails itself (don't know since I don't use g-mail).

Yeah, I thought about that too, which is why I asked the question about sending the data directly. The possible problem there is that I don't know anything about the "Epson connect" stuff. The early "email-to-print" gateways relied on remote servers (what we'd call "cloud" today) that took the message, ran formatting, and then sent it off to your printer, kinda like a CUPS-on-steroids with additional format conversions. Today's printers have CPU's and resources that are big enough that they should be able to do this "onboard" if they so choose, but who knows if they actually do. Sending a Word file directly to the printer isn't going to do anything good if the printer can't process it locally, so I went for the middle ground of trying to find the most straightforward solution.

Getting that preformatted e-mail message and then submitting it to sendmail is probably the most promising strategy.
 

kcgoatboy

Dabbler
Joined
Mar 17, 2014
Messages
10
Yeah, discovering that as well. It is not an attachment in the true sense; but more in the sense of in the message body.

OP may be better off thinking of a different route. Could even look at possibly scheduling a task in Windows that would simply send a print job or seeing if g-mail has a method of sending scheduled e-mails itself (don't know since I don't use g-mail).
I'd prefer not to schedule something in windows so I don't have to keep my pc on all the time. I need a color patern to print out to utilize the color nozzles so a text based email probably won't get that done.

Command would be like this; just replace "%EMailSubject%" and "%YourE-MailAddress%":

uuencode "/mnt/zbpool1/zbdocs/testpage2.docx" "testpage2.docx" | mail -s "%EMailSubject%" "%YourE-MailAddress%"

My Outlook client client didn't like the e-mail too much though (as noted earlier by Bidule0hm). Is there a particular reason you have to send an attachment?

Testing this i am able to send it to my gmail and it looks correct and the attachment opens. But when I send it to the printer it prints out the attachment as if it were lines of code instead of a picture. If i'm in gmail i can create an email with a blank body and just attach the doc and the printer prints just fine.
 

kcgoatboy

Dabbler
Joined
Mar 17, 2014
Messages
10
Take the full text of that message and use it, then.
I'm a little confused with the sugestion. The .docx file is not a text document, it is a series of pictures/test patterns. I can save it a a jpg or png and would still need to print the attachment in the email (a picture).
 

jgreco

Resident Grinch
Joined
May 29, 2011
Messages
18,680
Your file is not a message. Your message is a message. Follow the instructions I previously suggested:

Then see if you can save a copy of the message to a file in "mbox" format. Not a copy of the attachment... a copy of the message. Headers and all.

If you have no fscking clue what I'm talking about, see your forum inbox (upper right hand corner where the red number says Inbox[1]) and I'll give you a more direct fix.
 
Status
Not open for further replies.
Top