0

How can the remote attachment file be specified to be included in SMTP mail? Attachement file is located on different server (own username/password access)

import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.base import MIMEBase
from email import encoders
mail_content = """ This is body content """

sender_addr = "sender_addr@server.com"
sender_pass = "apassword"
receiver_addr = "receiver_addr@server.com"

# Create MIME header
msg = MIMEMultipart()
msg['From'] = sender_addr
msg['To'] = receiver_addr
msg['Subject'] = 'A test mail subject sent by Python'
msg.attach(MIMEText(mail_content, 'plain'))

fname = "doc_1.pdf"
attach_file = open(fname, 'rb')  # **<- How can I specify the remote path here?**
payload = MIMEBase('application', 'octet-stream')
# Attach an attachment to payload
payload.set_payload((attach_file).read())
encoders.encode_base64(payload)  

# Add payload header with filename
payload.add_header('Content-Disposition', 'attachment', filename=fname)
msg.attach(payload)

# Create SMTP client
client = smtplib.SMTP('smtp.gmail.com', 587) 
client.starttls()                              
client.login(sender_addr, sender_pass)      
text = msg.as_string()                     

client.sendmail(sender_addr, receiver_addr, text)
client.quit()
print('Mail sent!')
tripleee
  • 175,061
  • 34
  • 275
  • 318
user1972031
  • 547
  • 1
  • 5
  • 19
  • I rolled back your latest edit. Adding stuff to the question would be fine but changing it so it asks about something else than the answers reply to is not acceptable. – tripleee Aug 23 '19 at 06:00

1 Answers1

0

https://www.rfc-editor.org/rfc/rfc2110#section-4.1 explains the Content-Location: header.

Assuming the content you want to link to is in the variable attach_uri, something like

# Create MIME header
msg = MIMEMultipart()
msg['From'] = sender_addr
msg['To'] = receiver_addr
msg['Subject'] = 'A test mail subject sent by Python'
msg.attach(MIMEText(mail_content, 'plain'))

payload = MIMEBase('application', 'octet-stream')
payload.add_header('Content-Location', attach_uri)
msg.attach(payload)

If you are asking how to retrieve stuff from a remote location and include it in an email, that's a pretty broad topic; but assuming you have the content on an HTTP server, try something like

import requests

r = requests.get(attachment_uri)

payload.set_payload(r.content)

before you msg.attach(payload).

With this, there should be no need to set the Content-Location: to the original URI any longer (why would the user care where it came from? And you don't want to reveal the password etc).

Community
  • 1
  • 1
tripleee
  • 175,061
  • 34
  • 275
  • 318
  • Simply link to an existing resource. – user1972031 Aug 23 '19 at 04:51
  • Then how does your script receive the link to the resource? – tripleee Aug 23 '19 at 04:52
  • The attachment file is located at a remote server like this: username@192.168.10.100:/path/to/dir/remote_file.txt (password is also known) And I want to include this remote attachement file in the SMTP mail as well. – user1972031 Aug 23 '19 at 04:55
  • You mean your actual question is how to *retrieve* content from a remote location? Use `requests` or similar just like you would for downloading the content to a local disk. – tripleee Aug 23 '19 at 05:01
  • Should I use some means to 1) download the remote file to local server first, then 2) include the downloaded file in the SMPT mail? – user1972031 Aug 23 '19 at 05:04
  • You can fetch it to memory. See updated answer just now. – tripleee Aug 23 '19 at 05:04
  • You meant to use "requests.get('path/to/remote/file.txt') to download the remote file and then open/include it in the SMTP mail? – user1972031 Aug 23 '19 at 05:10
  • That doesn't look like a valid URI but other than that, yeah. – tripleee Aug 23 '19 at 05:13
  • It sent an empty attachment file instead. I modified the original code to reflect the latest code. Am I missing something? – user1972031 Aug 23 '19 at 05:56
  • Because that's what the changed code does; that's how I originally interpreted your question (send `Content-Location:` *instead of* embedding the actual attachment). You really should not edit your question to move the goalposts. And the URI is still wrong; a valid URI has a protocol specifier like `http://` in front of the host name. – tripleee Aug 23 '19 at 05:59
  • Question 1) Can I embed an attachment file (located remotely) to SMTP email by specifying its remote path? Question 2) If remote path is like root_user@192.168.100.10:/root/example.txt (for example), how can I convert this path to URI? – user1972031 Aug 23 '19 at 06:32
  • Didn't I already answer exactly these questions? Your remote path doesn't specify a protocol so we can't know if it should have `http://` or `https://` or `ftp://` or something else in front of it. Maybe it's `scp`? (The colon after the host name looks vaguely like an `scp` path, in which case I guess find a question about how to fetch things over `scp` in Python. My first Google hit is https://stackoverflow.com/questions/250283/how-to-scp-in-python) – tripleee Aug 23 '19 at 07:21