Coverage for /var/srv/projects/api.amasfac.comuna18.com/tmp/venv/lib/python3.9/site-packages/sendgrid/helpers/mail/email.py: 79%

79 statements  

« prev     ^ index     » next       coverage.py v6.4.4, created at 2023-07-17 14:22 -0600

1try: 

2 import rfc822 

3except ImportError: 

4 import email.utils as rfc822 

5 

6try: 

7 basestring = basestring 

8except NameError: 

9 # Define basestring when Python >= 3.0 

10 basestring = str 

11 

12 

13class Email(object): 

14 """An email address with an optional name.""" 

15 

16 def __init__(self, 

17 email=None, 

18 name=None, 

19 substitutions=None, 

20 subject=None, 

21 p=0, 

22 dynamic_template_data=None): 

23 """Create an Email with the given address and name. 

24 

25 Either fill the separate name and email fields, or pass all information 

26 in the email parameter (e.g. email="dude Fella <example@example.com>"). 

27 :param email: Email address, or name and address in standard format. 

28 :type email: string, optional 

29 :param name: Name for this sender or recipient. 

30 :type name: string, optional 

31 :param substitutions: String substitutions to be applied to the email. 

32 :type substitutions: list(Substitution), optional 

33 :param subject: Subject for this sender or recipient. 

34 :type subject: string, optional 

35 :param p: p is the Personalization object or Personalization object 

36 index 

37 :type p: Personalization, integer, optional 

38 :param dynamic_template_data: Data for a dynamic transactional template. 

39 :type dynamic_template_data: DynamicTemplateData, optional 

40 """ 

41 self._name = None 

42 self._email = None 

43 self._personalization = p 

44 

45 if email and not name: 45 ↛ 50line 45 didn't jump to line 50, because the condition on line 45 was never false

46 # allows passing emails as "Example Name <example@example.com>" 

47 self.parse_email(email) 

48 else: 

49 # allows backwards compatibility for Email(email, name) 

50 if email is not None: 

51 self.email = email 

52 

53 if name is not None: 

54 self.name = name 

55 

56 # Note that these only apply to To Emails (see Personalization.add_to) 

57 # and should be moved but have not been for compatibility. 

58 self._substitutions = substitutions 

59 self._dynamic_template_data = dynamic_template_data 

60 self._subject = subject 

61 

62 @property 

63 def name(self): 

64 """Name associated with this email. 

65 

66 :rtype: string 

67 """ 

68 return self._name 

69 

70 @name.setter 

71 def name(self, value): 

72 """Name associated with this email. 

73 

74 :param value: Name associated with this email. 

75 :type value: string 

76 """ 

77 if not (value is None or isinstance(value, basestring)): 77 ↛ 78line 77 didn't jump to line 78, because the condition on line 77 was never true

78 raise TypeError('name must be of type string.') 

79 

80 self._name = value 

81 

82 @property 

83 def email(self): 

84 """Email address. 

85 

86 See http://tools.ietf.org/html/rfc3696#section-3 and its errata 

87 http://www.rfc-editor.org/errata_search.php?rfc=3696 for information 

88 on valid email addresses. 

89 

90 :rtype: string 

91 """ 

92 return self._email 

93 

94 @email.setter 

95 def email(self, value): 

96 """Email address. 

97 

98 See http://tools.ietf.org/html/rfc3696#section-3 and its errata 

99 http://www.rfc-editor.org/errata_search.php?rfc=3696 for information 

100 on valid email addresses. 

101 

102 :param value: Email address. 

103 See http://tools.ietf.org/html/rfc3696#section-3 and its errata 

104 http://www.rfc-editor.org/errata_search.php?rfc=3696 for information 

105 on valid email addresses. 

106 :type value: string 

107 """ 

108 self._email = value 

109 

110 @property 

111 def substitutions(self): 

112 """A list of Substitution objects. These substitutions will apply to 

113 the text and html content of the body of your email, in addition 

114 to the subject and reply-to parameters. The total collective size 

115 of your substitutions may not exceed 10,000 bytes per 

116 personalization object. 

117 

118 :rtype: list(Substitution) 

119 """ 

120 return self._substitutions 

121 

122 @substitutions.setter 

123 def substitutions(self, value): 

124 """A list of Substitution objects. These substitutions will apply to 

125 the text and html content of the body of your email, in addition to 

126 the subject and reply-to parameters. The total collective size of 

127 your substitutions may not exceed 10,000 bytes per personalization 

128 object. 

129 

130 :param value: A list of Substitution objects. These substitutions will 

131 apply to the text and html content of the body of your email, in 

132 addition to the subject and reply-to parameters. The total collective 

133 size of your substitutions may not exceed 10,000 bytes per 

134 personalization object. 

135 :type value: list(Substitution) 

136 """ 

137 self._substitutions = value 

138 

139 @property 

140 def dynamic_template_data(self): 

141 """Data for a dynamic transactional template. 

142 

143 :rtype: DynamicTemplateData 

144 """ 

145 return self._dynamic_template_data 

146 

147 @dynamic_template_data.setter 

148 def dynamic_template_data(self, value): 

149 """Data for a dynamic transactional template. 

150 

151 :param value: DynamicTemplateData 

152 :type value: DynamicTemplateData 

153 """ 

154 self._dynamic_template_data = value 

155 

156 @property 

157 def subject(self): 

158 """Subject for this sender or recipient. 

159 

160 :rtype: string 

161 """ 

162 return self._subject 

163 

164 @subject.setter 

165 def subject(self, value): 

166 """Subject for this sender or recipient. 

167 

168 :param value: Subject for this sender or recipient. 

169 :type value: string, optional 

170 """ 

171 self._subject = value 

172 

173 @property 

174 def personalization(self): 

175 """The Personalization object or Personalization object index 

176 

177 :rtype: Personalization, integer 

178 """ 

179 return self._personalization 

180 

181 @personalization.setter 

182 def personalization(self, value): 

183 """The Personalization object or Personalization object index 

184 

185 :param value: The Personalization object or Personalization object 

186 index 

187 :type value: Personalization, integer 

188 """ 

189 self._personalization = value 

190 

191 def parse_email(self, email_info): 

192 """Allows passing emails as "Example Name <example@example.com>" 

193 

194 :param email_info: Allows passing emails as 

195 "Example Name <example@example.com>" 

196 :type email_info: string 

197 """ 

198 name, email = rfc822.parseaddr(email_info) 

199 

200 # more than likely a string was passed here instead of an email address 

201 if "@" not in email: 201 ↛ 202line 201 didn't jump to line 202, because the condition on line 201 was never true

202 name = email 

203 email = None 

204 

205 if not name: 

206 name = None 

207 

208 if not email: 208 ↛ 209line 208 didn't jump to line 209, because the condition on line 208 was never true

209 email = None 

210 

211 self.name = name 

212 self.email = email 

213 return name, email 

214 

215 def get(self): 

216 """ 

217 Get a JSON-ready representation of this Email. 

218 

219 :returns: This Email, ready for use in a request body. 

220 :rtype: dict 

221 """ 

222 email = {} 

223 if self.name is not None: 

224 email["name"] = self.name 

225 

226 if self.email is not None: 226 ↛ 228line 226 didn't jump to line 228, because the condition on line 226 was never false

227 email["email"] = self.email 

228 return email