289 return result |
289 return result |
290 |
290 |
291 MailMessageToMIMEMessage = mail_message_to_mime_message |
291 MailMessageToMIMEMessage = mail_message_to_mime_message |
292 |
292 |
293 |
293 |
|
294 def _to_str(value): |
|
295 """Helper function to make sure unicode values converted to utf-8 |
|
296 |
|
297 Args: |
|
298 value: str or unicode to convert to utf-8. |
|
299 |
|
300 Returns: |
|
301 UTF-8 encoded str of value, otherwise value unchanged. |
|
302 """ |
|
303 if isinstance(value, unicode): |
|
304 return value.encode('utf-8') |
|
305 return value |
|
306 |
294 class _EmailMessageBase(object): |
307 class _EmailMessageBase(object): |
295 """Base class for email API service objects. |
308 """Base class for email API service objects. |
296 |
309 |
297 Subclasses must define a class variable called _API_CALL with the name |
310 Subclasses must define a class variable called _API_CALL with the name |
298 of its underlying mail sending API call. |
311 of its underlying mail sending API call. |
390 |
403 |
391 def IsInitialized(self): |
404 def IsInitialized(self): |
392 return self.is_initialized() |
405 return self.is_initialized() |
393 |
406 |
394 def ToProto(self): |
407 def ToProto(self): |
|
408 """Convert mail message to protocol message. |
|
409 |
|
410 Unicode strings are converted to UTF-8 for all fields. |
|
411 |
|
412 This method is overriden by EmailMessage to support the sender fields. |
|
413 |
|
414 Returns: |
|
415 MailMessage protocol version of mail message. |
|
416 """ |
395 self.check_initialized() |
417 self.check_initialized() |
396 message = mail_service_pb.MailMessage() |
418 message = mail_service_pb.MailMessage() |
397 message.set_sender(self.sender) |
419 message.set_sender(_to_str(self.sender)) |
398 |
420 |
399 if hasattr(self, 'reply_to'): |
421 if hasattr(self, 'reply_to'): |
400 message.set_replyto(self.reply_to) |
422 message.set_replyto(_to_str(self.reply_to)) |
401 message.set_subject(self.subject) |
423 message.set_subject(_to_str(self.subject)) |
402 if hasattr(self, 'body'): |
424 if hasattr(self, 'body'): |
403 message.set_textbody(self.body) |
425 message.set_textbody(_to_str(self.body)) |
404 if hasattr(self, 'html'): |
426 if hasattr(self, 'html'): |
405 message.set_htmlbody(self.html) |
427 message.set_htmlbody(_to_str(self.html)) |
406 |
428 |
407 if hasattr(self, 'attachments'): |
429 if hasattr(self, 'attachments'): |
408 for file_name, data in _attachment_sequence(self.attachments): |
430 for file_name, data in _attachment_sequence(self.attachments): |
409 attachment = message.add_attachment() |
431 attachment = message.add_attachment() |
410 attachment.set_filename(file_name) |
432 attachment.set_filename(_to_str(file_name)) |
411 attachment.set_data(data) |
433 attachment.set_data(_to_str(data)) |
412 return message |
434 return message |
413 |
435 |
414 def to_mime_message(self): |
436 def to_mime_message(self): |
415 """Generate a MIMEMultitype message from EmailMessage. |
437 """Generate a MIMEMultitype message from EmailMessage. |
416 |
438 |
553 def CheckInitialized(self): |
575 def CheckInitialized(self): |
554 self.check_initialized() |
576 self.check_initialized() |
555 |
577 |
556 def ToProto(self): |
578 def ToProto(self): |
557 """Does addition conversion of recipient fields to protocol buffer. |
579 """Does addition conversion of recipient fields to protocol buffer. |
|
580 |
|
581 Returns: |
|
582 MailMessage protocol version of mail message including sender fields. |
558 """ |
583 """ |
559 message = super(EmailMessage, self).ToProto() |
584 message = super(EmailMessage, self).ToProto() |
560 |
585 |
561 for attribute, adder in (('to', message.add_to), |
586 for attribute, adder in (('to', message.add_to), |
562 ('cc', message.add_cc), |
587 ('cc', message.add_cc), |
563 ('bcc', message.add_bcc)): |
588 ('bcc', message.add_bcc)): |
564 if hasattr(self, attribute): |
589 if hasattr(self, attribute): |
565 for address in _email_sequence(getattr(self, attribute)): |
590 for address in _email_sequence(getattr(self, attribute)): |
566 adder(address) |
591 adder(_to_str(address)) |
567 return message |
592 return message |
568 |
593 |
569 def __setattr__(self, attr, value): |
594 def __setattr__(self, attr, value): |
570 """Provides additional checks on recipient fields.""" |
595 """Provides additional checks on recipient fields.""" |
571 if attr in ['to', 'cc', 'bcc']: |
596 if attr in ['to', 'cc', 'bcc']: |