Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[jsk_robot_startup] Support embed image in email_topic.py #1485

Merged
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions jsk_robot_common/jsk_robot_startup/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ add_message_files(
FILES
RoomLight.msg
Email.msg
EmailBody.msg
)

generate_messages(
Expand Down
2 changes: 1 addition & 1 deletion jsk_robot_common/jsk_robot_startup/msg/Email.msg
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
std_msgs/Header header
string subject # Email subject
string body # Email body
jsk_robot_startup/EmailBody[] body # Email body
string sender_address # Sender's email address
string receiver_address # receiver's email address
string smtp_server # smtp server address
Expand Down
4 changes: 4 additions & 0 deletions jsk_robot_common/jsk_robot_startup/msg/EmailBody.msg
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
string type # text, html, img
string message # For text and html type
string file_path # For img type
uint8 img_size # For img type [percent]
35 changes: 34 additions & 1 deletion jsk_robot_common/jsk_robot_startup/scripts/email_topic.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/usr/bin/env python

from email.mime.application import MIMEApplication
from email.mime.image import MIMEImage
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
import errno
Expand Down Expand Up @@ -75,7 +76,39 @@ def _send_mail(
msg["Subject"] = subject
msg["From"] = sender_address
msg["To"] = receiver_address
msg.attach(MIMEText(body, 'plain', 'utf-8'))
# Support embed image
for content in body:
if content.type == 'text':
msg.attach(MIMEText(content.message, 'plain', 'utf-8'))
elif content.type == 'html':
msg.attach(MIMEText(content.message, 'html'))
elif content.type == 'img':
if content.file_path == '':
rospy.logwarn('File name is empty. Skipped.')
continue
if not os.path.exists(content.file_path):
rospy.logerr(
'File {} is not found.'.format(content.file_path))
return
with open(content.file_path, 'rb') as img:
embed_img = MIMEImage(img.read())
embed_img.add_header(
'Content-ID', '<{}>'.format(content.file_path))
embed_img.add_header(
'Content-Disposition', 'inline; filename="{}"'.format(
os.path.basename(content.file_path)))
msg.attach(embed_img) # This line is necessary to embed
if content.img_size:
image_size = content.img_size
else:
image_size = 100
text = '<img src="cid:{}" width={}%>'.format(
content.file_path, image_size)
bodytext = MIMEText(text, 'html')
msg.attach(bodytext)
else:
rospy.logwarn('Unknown content type {}'.format(content.type))
continue
# Attach file
for attached_file in attached_files:
if attached_file == '':
Expand Down