25 from email
import encoders
26 from email.mime.text
import MIMEText
27 from email.mime.multipart
import MIMEMultipart
28 from email.mime.audio
import MIMEAudio
29 from email.mime.image
import MIMEImage
30 from email.mime.base
import MIMEBase
34 from rapp_utilities
import RappUtilities
36 from rapp_platform_ros_communications.srv
import (
44 sendSrvTopic = rospy.get_param(
"rapp_email_send_topic")
45 sendSrv = rospy.Service(sendSrvTopic, SendEmailSrv, \
55 resp = SendEmailSrvResponse()
57 RappUtilities.rapp_print(req,
'DEBUG')
59 if len(req.recipients) == 0:
60 RappUtilities.rapp_print(
"Must provide at least one recipient",
'ERROR')
65 req.recipients, req.userEmail, req.body, req.subject )
67 for attachment
in req.files:
70 except EnvironmentError, err:
71 RappUtilities.rapp_print(
"Failed to handle attachment: " + attachment, \
73 RappUtilities.rapp_print(err,
'ERROR')
81 req.server, req.port, msg )
82 except (socket.error, smtplib.SMTPException):
101 RappUtilities.rapp_print(
"Connecting to the requested SMTP server: " + \
102 server +
' port: ' + port)
104 socket.setdefaulttimeout(5)
105 if port
is not None and port !=
'':
106 smtpServer = smtplib.SMTP(server, port)
108 smtpServer = smtplib.SMTP(server)
109 except socket.error, err:
110 RappUtilities.rapp_print( \
111 "Could not establish a connection to the requested SMTP server: " + \
112 server +
' port: ' + port,
'ERROR')
113 RappUtilities.rapp_print( err,
'ERROR')
118 smtpServer.starttls()
120 smtpServer.login( userEmail, userPassword )
121 smtpServer.sendmail( userEmail, recipients, msg.as_string() )
122 except smtplib.SMTPException, err:
123 RappUtilities.rapp_print(
"SMTP failed!",
'ERROR')
124 RappUtilities.rapp_print( err,
'ERROR')
139 if subject ==
'' or subject
is None:
140 RappUtilities.rapp_print(
'No email subject provided')
141 msg = MIMEMultipart()
142 msg[
'Subject'] = subject
143 msg[
'From'] = userEmail
144 msg[
'To'] =
', '.join( recipients )
145 if body ==
'' or body
is None:
146 RappUtilities.rapp_print(
'No email body provided')
147 msg.attach( MIMEText(body) )
161 if not os.path.isfile( filename ):
162 raise IOError(
'Filename does not exist! ' + filename)
164 fp = open( filename ,
'rb')
165 filetype, encoding = mimetypes.guess_type( fp.name )
166 if filetype
is None or encoding
is not None:
167 filetype =
'application/octet-stream'
168 maintype, subtype = filetype.split(
'/', 1)
170 RappUtilities.rapp_print(
'Attachment : ' + filename )
171 RappUtilities.rapp_print(
'Attachment maintype: ' + maintype +
' subtype: ' +\
173 if maintype ==
'text':
174 attachment = MIMEText( fp.read(), _subtype=subtype )
175 elif maintype ==
'image':
176 attachment = MIMEImage( fp.read(), _subtype=subtype )
177 elif maintype ==
'audio':
178 attachment = MIMEAudio( fp.read(), _subtype=subtype )
180 attachment = MIMEBase( maintype, subtype )
181 attachment.set_payload( fp.read() )
182 encoders.encode_base64( attachment )
186 attachment.add_header(
'Content-Disposition',\
187 'attachment', filename = os.path.basename(filename) )
190 if __name__ ==
'__main__':
191 print(
'Implements server for EmailSendSrv. Not supposed to' + \
192 ' be called directly')
def _handleAttachment
Add attachment to the email.
def sendEmailSrvCallback
The callback to send specified mails from users email account.
def _createEmailBody
Creates the emails main body.
def _connectAndSend
Connect to the requested SMTP server and send email.