Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

251

252

253

254

255

256

257

258

259

260

261

262

263

264

265

266

267

268

269

270

271

272

273

274

275

276

277

278

279

280

281

282

283

284

285

286

287

288

289

290

291

292

293

294

295

296

297

298

299

300

301

302

303

304

305

306

307

308

309

310

311

312

313

314

315

316

317

318

319

320

321

322

323

324

325

326

327

328

329

330

331

332

333

334

335

336

337

# -*- coding: utf-8 -*- 

""" 

@file 

@brief Functions to dump emails 

""" 

import os 

import re 

import pprint 

from jinja2.exceptions import UndefinedError 

from pyquickhelper.loghelper import noLOG 

from ..grabber.email_message import EmailMessage 

from ..helpers.buffer_files_writing import BufferFilesWriting 

from .email_message_style import template_email_html, template_email_css 

from .renderer import Renderer 

 

 

class EmailMessageRenderer(Renderer): 

""" 

defines way to render an email 

""" 

 

def __init__(self, tmpl=None, css=None, 

style_table="dataframe100l", 

style_highlight="dataframe100l_hl", 

buffer_write=None, 

fLOG=noLOG): 

""" 

Defines a template based 

on `Jinja2 <http://jinja.pocoo.org/docs/dev/>`_. 

 

@param tmpl template (string or file) 

@param css style 

@param style_table style for the table 

@param style_highlight style for highlighted cells 

@param buffer_write instance of class @see cl BufferFilesWriting 

@param fLOG logging function 

 

@example(Template to render an email) 

 

See file :mod:`email_message_style <pymmails.grabber.email_message_style>` 

 

HTML template:: 

 

<?xml version="1.0" encoding="utf-8"?> 

<body> 

<html> 

<head> 

<title>{{ message.get_field("subject") }}</title> 

<link rel="stylesheet" type="text/css" href="{{ css }}"> 

</head> 

<body> 

{{ '<a href="{0}">&lt;--</a>'.format(prev_mail) if prev_mail else '' }} 

{{ '<a href="{0}">--&gt;</a>'.format(next_mail) if next_mail else '' }} 

<h1>{{ message.get_date().strftime('%Y/%m/%d') }} - {{ message.get_field("subject") }}</h1> 

<h2>attributes</h2> 

{{ render.produce_table_html(message, toshow=EmailMessage.subset, location=location, avoid=EmailMessage.avoid) }} 

<h2>message</h2> 

{{ render.process_body_html(location, message.body_html, attachments) }} 

<h2>full list of attributes</h2> 

{{ render.produce_table_html(message, toshow=message.Fields, location=location, tohighlight=EmailMessage.subset) }} 

</body> 

</html> 

 

CSS style:: 

 

.dataframe100l { 

padding: 0; 

width=75% 

font-family: Calibri; 

font-size: 100%; 

cursor: pointer; 

} 

 

.dataframe100l table { 

border-collapse: collapse; 

text-align: left; 

font-size: 11px; 

} 

 

.dataframe100l table td, .dataframe100l table th { 

padding: 3px 6px; 

} 

 

.dataframe100l table thead th { 

background-color:#AAAAAA; 

color:#ffffff; 

font-size: 11px; 

font-weight: bold; 

border-left: 1px solid #0070A8; 

} 

 

.dataframe100l table tbody td { 

color: #00496B; 

border-left: 1px solid #E1EEF4; 

font-size: 11px; 

font-weight: normal; 

} 

 

.dataframe100l table tbody .alt td { 

background: #E1EEF4; 

color: #00496B; 

} 

 

.dataframe100l_hl td { 

background: #FFFF00; 

} 

 

.dataframe100l_hl th { 

background: #FFFF00; 

} 

 

.dataframe100l_hl tr { 

background: #FFFF00; 

} 

 

.dataframe100l table tbody td:first-child { 

border-left: none; 

} 

 

.dataframe100l table tbody tr:last-child td { 

border-bottom: none; 

} 

 

.dataframe100l table tfoot td div { 

border-top: 1px solid #006699; 

background: #E1EEF4; 

} 

 

.dataframe100l table tfoot td { 

padding: 0; 

font-size: 11px 

} 

 

.dataframe100l table tfoot td div{ padding: 2px; } 

.dataframe100l table tfoot td ul { 

margin: 0; 

padding:0; 

list-style: none; 

text-align: right; 

} 

 

.dataframe100l table tfoot li { display: inline; } 

.dataframe100l table tfoot li a { 

text-decoration: none; 

display: inline-block; 

padding: 2px 8px; 

margin: 1px; 

color: #FFFFFF; 

border: 1px solid #006699; 

border-radius: 3px; 

background-color:#006699; 

} 

 

.dataframe100l table tfoot ul.active, .dataframe100l table tfoot ul a:hover { 

text-decoration: none; 

border-color: #006699; 

color: #FFFFFF; 

background: none; 

background-color:#00557F; 

} 

 

@endexample 

 

""" 

if tmpl is None: 

_template = template_email_html 

elif len(tmpl) < 5000 and os.path.exists(tmpl): 

with open(tmpl, "r", encoding="utf8") as f: 

_template = f.read() 

else: 

_template = tmpl 

 

if css is None: 

_css = template_email_css 

elif len(css) < 5000 and os.path.exists(css): 

with open(css, "r", encoding="utf8") as f: 

_css = f.read() 

else: 

_css = css 

 

if buffer_write is None: 

buffer_write = BufferFilesWriting(fLOG=fLOG) 

Renderer.__init__(self, tmpl=_template, css=_css, style_table=style_table, 

style_highlight=style_highlight, buffer_write=buffer_write, fLOG=fLOG) 

 

def render(self, location, mail, attachments, file_css="mail_style.css", 

prev_mail=None, next_mail=None, **addition): 

""" 

Renders a mail. 

 

@paramp location location where this mail should be saved 

@param mail instance of @see cl EmailMessage 

@param file_css css file (where it is supposed to be stored) 

@param attachments attachments 

@param prev_mail previous mail (or None if there is none) 

@param next_mail next mail (or None if there is none) 

@param addition sent to *Jinja* 

@return html, css (content), attachments 

 

The mail is stored in object ``message``, ``css`` means the style sheet, 

``render`` means this object, ``location`` means *location*, 

``EmailMessage`` is the class *EmailMessage*, ``attachments`` is *attachments*:: 

 

{{ message.get_subject() }} 

 

The value stored in *file_css* will be relative to *location*. 

 

""" 

file_css = os.path.relpath(file_css, location) 

css = self._css.render(message=mail) 

try: 

html = self._template.render(message=mail, css=file_css, render=self, 

location=location, EmailMessage=EmailMessage, 

attachments=attachments, prev_mail=prev_mail, 

next_mail=next_mail, **addition) 

except UndefinedError as e: 

empty = [] 

if 'groups' in addition: 

for gr in addition['groups']: 

if 'emails' in gr and len(gr['emails']) == 0: 

empty.append(gr) 

tmpl = self._raw_template 

disp1 = pprint.pformat(empty) 

mes = "This usually happens when the project was sent with a mail not retained in a the final list." 

raise RuntimeError("Unable to apply pattern\n----\n{0}\n----\n{1}\n----\n{2}".format( 

mes, tmpl, disp1)) from e 

return html, css, attachments 

 

def write(self, location, mail, filename, attachments=None, 

overwrite=False, file_css="mail_style.css", encoding="utf8", 

prev_mail=None, next_mail=None, **addition): 

""" 

Writes a mail, the function assumes the attachments were already dumped. 

 

@param location location 

@param mail instance of @see cl EmailMessage 

@param attachments list of attachments (see @see me dump_attachments) 

@param overwrite the function does not overwrite 

@param file_css css file (where it is supposed to be stored) 

@param encoding encoding 

@param addition additional parameter sent to Jinja2 

@param prev_mail previous mail (or None if there is none) 

@param next_mail next mail (or None if there is none) 

@return list of written local files, attachements 

""" 

full_css = os.path.join(location, file_css) 

full_mail = os.path.join(location, filename) 

if not overwrite and self.BufferWrite.exists(full_css) and \ 

self.BufferWrite.exists(full_mail): 

return [full_mail, full_css], attachments 

html, css, attachments = self.render( 

location, mail, attachments, file_css=full_css, 

prev_mail=prev_mail, next_mail=next_mail, **addition) 

if not self.BufferWrite.exists(full_css, local=not overwrite): 

f = self.BufferWrite.open(full_css, text=True, encoding=encoding) 

f.write(css) 

if not self.BufferWrite.exists(full_mail, local=not overwrite): 

f = self.BufferWrite.open(full_mail, text=True, encoding=encoding) 

f.write(html) 

return [full_mail, full_css], attachments 

 

def produce_table_html(self, email, location, toshow, tohighlight=None, atts=None, avoid=None): 

""" 

produces a table with the values of some fields of the message 

 

@param toshow list of fields to show, if None, it considers all fields 

@param tohighlight list of fields to highlights 

@param atts list of files to append at the end of the table, 

list of tuple *((filename,message_id,content_id))* 

@param location folder where this page will be saved (for attachment) 

@param avoid fields to avoid 

@return html string 

""" 

if atts is None: 

atts = [] 

if avoid is None: 

avoid = [] 

rows = [] 

rows.append('<div class="{0}">'.format(self._style_table)) 

rows.append('<table border="1">') 

rows.append("<thead><tr><th>key</th><th>value</th></tr></thead>") 

for tu in sorted(email.items()): 

if toshow is not None and tu[0] not in toshow: 

continue 

if tu[0] in avoid: 

continue 

 

tu = (tu[0], email.decode_header(tu[0], tu[1])) 

 

if tohighlight and tu[0] in tohighlight: 

format = '<tr class="%s"><th>{0}</th><td>{1}</td></tr>' % self._style_highlight 

else: 

format = "<tr><th>{0}</th><td>{1}</td></tr>" 

rows.append(format.format( 

tu[0].replace("<", "&lt;").replace(">", "&gt;"), 

tu[1].replace("<", "&lt;").replace(">", "&gt;"))) 

 

for i, a in enumerate(atts): 

filename, _, cid = a 

rows.append('<tr><td>{0}</td><td><a href="{1}">{2}</a>{3}</td></tr>'.format( 

"attachment %d" % (i + 1), 

os.path.relpath( 

filename, location) if location else filename, 

os.path.split(filename)[-1], 

"id={0}".format(cid) if cid else "")) 

 

rows.append("</table>") 

rows.append("<br /></div>") 

return "\n".join(rows) 

 

def process_body_html(self, location, body, atts): 

""" 

replaces link to images included in the mail body:: 

 

<img name="14a318e16161c62a_14a31789f7a34aae_null" 

title="pastedImage.png" 

src="cid:1146aa0a-244a-440e-8ea5-7b272c94f89a" 

height="153.02644466209597" 

width="560"> 

 

@param location location where the HTML body will be saved 

@param body html body 

@param atts attachements (filename, message id, content id) 

@return modified body html 

""" 

if atts: 

for filename, _, cid in atts: 

if cid is None: 

continue 

pattern = 'src="cid:{0}"'.format(cid.strip("<>")) 

exp = re.compile('({0})'.format(pattern)) 

fall = exp.findall(body) 

if len(fall) > 0: 

relf = os.path.relpath(filename, location) 

link = 'src="{0}"'.format(relf) 

body = body.replace(pattern, link) 

return body