Mail: Difference between revisions
Jump to navigation
Jump to search
No edit summary |
|||
| (21 intermediate revisions by the same user not shown) | |||
| Line 1: | Line 1: | ||
[[Category:Linux/Unix]] | |||
[[Category:python]] | |||
=Sending mail with sendmail= | |||
;<code>sendmail -t < <messagefile></code> | ;<code>sendmail -t < <messagefile></code> | ||
:Send the email as in <messagefile> | :Send the email as in <messagefile> | ||
The email headers are in <messagefile>. That should look like: | :The email headers are in <messagefile>. That should look like: | ||
<syntaxhighlight lang=text> | <syntaxhighlight lang=text> | ||
From: me@mydomain.tld | From: me@mydomain.tld | ||
| Line 10: | Line 12: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
;<code>mailx -s Subject -a <attachmentfile> email@domain.tld</code> | |||
:Send email with attachment after entering optional text and an EOT (CTRL-D). | |||
=Sending mail with python= | |||
<syntaxhighlight lang=python> | |||
#!/usr/bin/env python3 | |||
def usage(): | |||
print ("Usage: "+__file__) | |||
print("Send email, include what you need in your program") | |||
version = '0.1, 19700101, Initial release' | |||
print("Version: "+version) | |||
sys.exit(1) | |||
def sendmail(mailto,mailfrom = 'NN',mailsubject = '',mailtext = '',attachments = ''): | |||
import smtplib | |||
from email import encoders | |||
from email.message import Message | |||
from email.mime.base import MIMEBase | |||
from email.mime.multipart import MIMEMultipart | |||
from email.mime.text import MIMEText | |||
from email.utils import formatdate | |||
outer = MIMEMultipart() | |||
outer['Subject'] = mailsubject | |||
outer['To'] = ", ".join(mailto) | |||
outer['From'] = mailfrom | |||
outer['Date'] = formatdate(localtime=True) | |||
msg = MIMEText(mailtext,'plain') | |||
outer.attach(msg) | |||
if attachments != '': | |||
for attachment in attachments: | |||
filename = attachment.split('/')[-1] | |||
ctype = 'application/octet-stream' | |||
maintype, subtype = ctype.split('/', 1) | |||
fp = open(attachment, 'rb') | |||
msg = MIMEBase(maintype, subtype) | |||
msg.set_payload(fp.read()) | |||
fp.close() | |||
# Encode the payload using Base64 | |||
encoders.encode_base64(msg) | |||
# Set the filename parameter | |||
msg.add_header('Content-Disposition', 'attachment', filename=filename) | |||
outer.attach(msg) | |||
composed = outer.as_string() | |||
print(composed) | |||
s = smtplib.SMTP('localhost') | |||
print('Sending mail') | |||
s.sendmail(mailfrom, mailto , composed) | |||
s.quit() | |||
return | |||
</syntaxhighlight> | |||
For Python 2 it was | |||
<syntaxhighlight lang=python> | <syntaxhighlight lang=python> | ||
#!/usr/bin/env python2 | |||
def usage(): | |||
print ("Usage: "+__file__) | |||
print("Send email, include what you need in your program") | |||
version = '0.1, 19700101, Initial release' | |||
print("Version: "+version) | |||
sys.exit(1) | |||
# USER CONFIGURABLE ITEMS | |||
maxmailsize = 10000000 | |||
# END USER CONFIGURABLE ITEMS, DO NOT CHANGE ANYTHING BELOW THIS LINE | |||
def main(): | def main(): | ||
attachement1 = filepath | |||
mailto = you@domain.tld | attachement2 = filepath | ||
mailto = ['you@domain.tld'] | |||
mailcc = ['someoneelse@anotherdomain.tld','andmore@domain.tld'] | |||
mailbcc = ['secret@domain.tld'] # Only include this in the final sendmail call, not in the headers. | |||
mailfrom = 'me@domain.tld' # Some systems do not like this, just remove it from the sendmail call | |||
mailsubject = 'What this is about' | mailsubject = 'What this is about' | ||
mailtext = "Hi,\n\nI'd like to send you an email.\n\nBest Regards\n\nMe." | mailtext = "Hi,\n\nI'd like to send you an email.\n\nBest Regards\n\nMe." | ||
sendmail(mailto,mailsubject,mailtext, | sendmail(mailto, mailcc=mailcc, mailbcc=mailbcc, mailfrom=mailfrom, mailsubject=mailsubject, mailtext=mailtext, attachments=[attachment1,attachment2]) | ||
return | return | ||
def sendmail(mailto,mailcc = '',mailbcc = '',mailfrom = 'Sender',mailsubject = '',mailtext = '',attachments = ''): | |||
import smtplib | |||
from email import encoders | |||
from email.message import Message | |||
from email.mime.base import MIMEBase | |||
from email.mime.multipart import MIMEMultipart | |||
from email.mime.text import MIMEText | |||
outer = MIMEMultipart() | outer = MIMEMultipart() | ||
outer['Subject'] = mailsubject | outer['Subject'] = mailsubject | ||
outer['To'] = | outer['To'] = ",".join(mailto) | ||
outer['CC'] = ",".join(mailcc) | |||
outer['From'] = mailfrom | |||
msg = MIMEText(mailtext,'plain') | |||
msg = | |||
outer.attach(msg) | outer.attach(msg) | ||
if attachments != '': | |||
attachtotsize = 0 | |||
for attachment in attachments: | |||
attachsize = os.stat(attachment).st_size | |||
if attachtotsize + attachsize > maxmailsize: | |||
mailtext += attachment+" makes the message bigger than "+maxmailsize+", it is not attached.\n" | |||
else: | |||
attachtotsize += attachsize | |||
filename = attachment.split('/')[-1] | |||
ctype = 'application/octet-stream' | |||
maintype, subtype = ctype.split('/', 1) | |||
fp = open(attachment, 'rb') | |||
msg = MIMEBase(maintype, subtype) | |||
msg.set_payload(fp.read()) | |||
fp.close() | |||
# Encode the payload using Base64 | |||
encoders.encode_base64(msg) | |||
# Set the filename parameter | |||
msg.add_header('Content-Disposition', 'attachment', filename=filename) | |||
outer.attach(msg) | |||
composed = outer.as_string() | composed = outer.as_string() | ||
s = smtplib.SMTP('localhost') | s = smtplib.SMTP('localhost') | ||
s.sendmail( | s.sendmail(mailfrom, mailto+list(mailcc)+list(mailbcc) , composed) | ||
s.quit() | s.quit() | ||
return | return | ||
main() | main() | ||
</syntaxhighlight> | </syntaxhighlight> | ||
=Reading mail with mailx= | |||
The web knows more[https://www.computerhope.com/unix/umailx.htm]. | |||
;mailx | |||
:Start mailx client and show 1 pages or messages. The Mailx prompt is a '&' | |||
:The current message is marked with a'>' at the start of the line. | |||
;& quit | |||
:Submit the changes you made and exit the client | |||
;& exit | |||
:Exit the client without submitting the changes | |||
;& <RET> | |||
:Read the current message | |||
;& d | |||
:Delete the current message | |||
;&d<id1> <id3> | |||
:Delete the messages with id <id1> and <id2> | |||
;&d<id1>-<id3> | |||
:Delete the messages with id <id1> up to and including <id2> | |||
;& h | |||
:Show a page of headers | |||
;& z | |||
:Show next page of headers | |||
;& z$ | |||
:Show last page of headers | |||
;& + | |||
:Go to next message | |||
;& - | |||
:Go to previous message | |||
Latest revision as of 07:14, 17 September 2026
Sending mail with sendmail
sendmail -t < <messagefile>- Send the email as in <messagefile>
- The email headers are in <messagefile>. That should look like:
From: me@mydomain.tld
To: user@domain.tld
Subject: Subject of your mail
email text
mailx -s Subject -a <attachmentfile> email@domain.tld- Send email with attachment after entering optional text and an EOT (CTRL-D).
Sending mail with python
#!/usr/bin/env python3
def usage():
print ("Usage: "+__file__)
print("Send email, include what you need in your program")
version = '0.1, 19700101, Initial release'
print("Version: "+version)
sys.exit(1)
def sendmail(mailto,mailfrom = 'NN',mailsubject = '',mailtext = '',attachments = ''):
import smtplib
from email import encoders
from email.message import Message
from email.mime.base import MIMEBase
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.utils import formatdate
outer = MIMEMultipart()
outer['Subject'] = mailsubject
outer['To'] = ", ".join(mailto)
outer['From'] = mailfrom
outer['Date'] = formatdate(localtime=True)
msg = MIMEText(mailtext,'plain')
outer.attach(msg)
if attachments != '':
for attachment in attachments:
filename = attachment.split('/')[-1]
ctype = 'application/octet-stream'
maintype, subtype = ctype.split('/', 1)
fp = open(attachment, 'rb')
msg = MIMEBase(maintype, subtype)
msg.set_payload(fp.read())
fp.close()
# Encode the payload using Base64
encoders.encode_base64(msg)
# Set the filename parameter
msg.add_header('Content-Disposition', 'attachment', filename=filename)
outer.attach(msg)
composed = outer.as_string()
print(composed)
s = smtplib.SMTP('localhost')
print('Sending mail')
s.sendmail(mailfrom, mailto , composed)
s.quit()
return
For Python 2 it was
#!/usr/bin/env python2
def usage():
print ("Usage: "+__file__)
print("Send email, include what you need in your program")
version = '0.1, 19700101, Initial release'
print("Version: "+version)
sys.exit(1)
# USER CONFIGURABLE ITEMS
maxmailsize = 10000000
# END USER CONFIGURABLE ITEMS, DO NOT CHANGE ANYTHING BELOW THIS LINE
def main():
attachement1 = filepath
attachement2 = filepath
mailto = ['you@domain.tld']
mailcc = ['someoneelse@anotherdomain.tld','andmore@domain.tld']
mailbcc = ['secret@domain.tld'] # Only include this in the final sendmail call, not in the headers.
mailfrom = 'me@domain.tld' # Some systems do not like this, just remove it from the sendmail call
mailsubject = 'What this is about'
mailtext = "Hi,\n\nI'd like to send you an email.\n\nBest Regards\n\nMe."
sendmail(mailto, mailcc=mailcc, mailbcc=mailbcc, mailfrom=mailfrom, mailsubject=mailsubject, mailtext=mailtext, attachments=[attachment1,attachment2])
return
def sendmail(mailto,mailcc = '',mailbcc = '',mailfrom = 'Sender',mailsubject = '',mailtext = '',attachments = ''):
import smtplib
from email import encoders
from email.message import Message
from email.mime.base import MIMEBase
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
outer = MIMEMultipart()
outer['Subject'] = mailsubject
outer['To'] = ",".join(mailto)
outer['CC'] = ",".join(mailcc)
outer['From'] = mailfrom
msg = MIMEText(mailtext,'plain')
outer.attach(msg)
if attachments != '':
attachtotsize = 0
for attachment in attachments:
attachsize = os.stat(attachment).st_size
if attachtotsize + attachsize > maxmailsize:
mailtext += attachment+" makes the message bigger than "+maxmailsize+", it is not attached.\n"
else:
attachtotsize += attachsize
filename = attachment.split('/')[-1]
ctype = 'application/octet-stream'
maintype, subtype = ctype.split('/', 1)
fp = open(attachment, 'rb')
msg = MIMEBase(maintype, subtype)
msg.set_payload(fp.read())
fp.close()
# Encode the payload using Base64
encoders.encode_base64(msg)
# Set the filename parameter
msg.add_header('Content-Disposition', 'attachment', filename=filename)
outer.attach(msg)
composed = outer.as_string()
s = smtplib.SMTP('localhost')
s.sendmail(mailfrom, mailto+list(mailcc)+list(mailbcc) , composed)
s.quit()
return
main()
Reading mail with mailx
The web knows more[1].
- mailx
- Start mailx client and show 1 pages or messages. The Mailx prompt is a '&'
- The current message is marked with a'>' at the start of the line.
- & quit
- Submit the changes you made and exit the client
- & exit
- Exit the client without submitting the changes
- & <RET>
- Read the current message
- & d
- Delete the current message
- &d<id1> <id3>
- Delete the messages with id <id1> and <id2>
- &d<id1>-<id3>
- Delete the messages with id <id1> up to and including <id2>
- & h
- Show a page of headers
- & z
- Show next page of headers
- & z$
- Show last page of headers
- & +
- Go to next message
- & -
- Go to previous message