{"componentChunkName":"component---src-templates-material-js","path":"/workshops/2021/07/09/pishem-svoego-robota-na-python-urok-11/","result":{"pageContext":{"pageParameters":{"isPreview":false,"isMirror":false,"delay_to_show_donate_popup_seconds":7,"about_page":false,"games_page":true,"games_page_link_in_header":false,"donate_popup_show_ru":true,"donate_popup_title_ru":"Без вас — нас не будет","donate_popup_content_ru":"Помогите нам добывать и рассказывать важные истории. Участие каждого — важно!","donate_popup_show_en":true,"donate_popup_title_en":"We can’t do it alone!","donate_popup_content_en":"Help us uncover and share the stories that matter. Every contribution makes a difference","display_regular_donors_banner":false,"showPatreonMethod":true,"useNewMaterialDonationBanner":true,"new_donations_page_design":true,"donations_popup_variants":{"ru":[{"id":"kc19b","title":"Сохраните журналистику для людей в России ","subtitle":"Читают нас прежде всего люди в России, но будущее «Важных историй» зависит от тех, кто живет за пределами страны. Поддержите нас ежемесячным донатом — сохраните независимую журналистику для ваших близких, которые остаются в России.","is_active":true,"background":"linear-gradient(10deg, #421f20 5%, #994146)"},{"id":"33tc9","title":"У пропаганды есть миллиарды из бюджета. У нас — только вы","subtitle":"Перемены возможны, если независимые от власти журналисты продолжают свою работу день за днем, год за годом. Мы рассказываем важные истории уже шесть лет. Помогите нам продолжать.","is_active":true,"background":"linear-gradient(10deg, #421f20 5%, #994146)"}],"en":[{"id":"nj1py","title":"Important stories don’t tell themselves","subtitle":"Independent journalism comes at a cost. We answer to no owners or advertisers — only to you, our readers.  Your ongoing support allows us to keep exposing the truth. Without you, we won’t be here.","is_active":true,"background":"linear-gradient(10deg, #421f20 5%, #994146)"},{"id":"xdfc1","title":"Help us outlast the Kremlin censorship","subtitle":"Censorship lose to journalism that keeps showing up, year after year. Monthly donors are what make that possible","is_active":true,"background":"linear-gradient(10deg, #421f20 5%, #994146)"},{"id":"ck121","title":"State propaganda has billions to spend. We have only you to rely on","subtitle":"Change happens when independent journalists keep showing up — day after day, year after year. Six years in, we're still showing up. Help us keep going with a monthly donation.","is_active":true,"background":"linear-gradient(10deg, #421f20 5%, #994146)"}]},"feedback_page":true,"donorsCount":0,"showAuthorsRequestAfterMaterial":true,"radioPage":true,"radioExists":{"ru":true,"en":false},"singlePlaylistPath":{"ru":"/radio/2025/12/22/razgovori-ovazhnikh/"},"with_bookmarks":true,"news_feed":true,"news_feed_skeleton":false,"news_page_redesign":true,"with_topics":true,"materials_topics":false,"topics_page":true,"with_pdf_share":false,"with_native_share":false,"preferred_source_page":true,"charon_new_form":true,"preferred_source_promos":true,"charon_en_page":true},"pk":288,"header":"Пишем своего робота на Python. Урок 11","lead":"Отправляем письмо с подборкой госконтрактов при помощи модуля smtplib","category":"workshops","entry_image":[{"format":"webp","url_1x":"https://static.istories.media/uploaded/images/73d062901c7e4722be110c68551ebe50-1920x960px_1x.webp","url_2x":"https://static.istories.media/uploaded/images/73d062901c7e4722be110c68551ebe50-1920x960px_2x.webp","for_width":1920,"for_height":960,"real_width_1x":1920,"real_width_2x":3840,"real_height_1x":960,"real_height_2x":1920},{"format":"png","url_1x":"https://static.istories.media/uploaded/images/73d062901c7e4722be110c68551ebe50-1920x960px_1x.png","url_2x":"https://static.istories.media/uploaded/images/73d062901c7e4722be110c68551ebe50-1920x960px_2x.png","for_width":1920,"for_height":960,"real_width_1x":1920,"real_width_2x":3840,"real_height_1x":960,"real_height_2x":1920},{"format":"webp","url_1x":"https://static.istories.media/uploaded/images/73d062901c7e4722be110c68551ebe50-1272x636px_1x.webp","url_2x":"https://static.istories.media/uploaded/images/73d062901c7e4722be110c68551ebe50-1272x636px_2x.webp","for_width":1272,"for_height":636,"real_width_1x":1272,"real_width_2x":2544,"real_height_1x":636,"real_height_2x":1272},{"format":"png","url_1x":"https://static.istories.media/uploaded/images/73d062901c7e4722be110c68551ebe50-1272x636px_1x.png","url_2x":"https://static.istories.media/uploaded/images/73d062901c7e4722be110c68551ebe50-1272x636px_2x.png","for_width":1272,"for_height":636,"real_width_1x":1272,"real_width_2x":2544,"real_height_1x":636,"real_height_2x":1272},{"format":"webp","url_1x":"https://static.istories.media/uploaded/images/73d062901c7e4722be110c68551ebe50-912x456px_1x.webp","url_2x":"https://static.istories.media/uploaded/images/73d062901c7e4722be110c68551ebe50-912x456px_2x.webp","for_width":912,"for_height":456,"real_width_1x":912,"real_width_2x":1824,"real_height_1x":456,"real_height_2x":912},{"format":"png","url_1x":"https://static.istories.media/uploaded/images/73d062901c7e4722be110c68551ebe50-912x456px_1x.png","url_2x":"https://static.istories.media/uploaded/images/73d062901c7e4722be110c68551ebe50-912x456px_2x.png","for_width":912,"for_height":456,"real_width_1x":912,"real_width_2x":1824,"real_height_1x":456,"real_height_2x":912},{"format":"webp","url_1x":"https://static.istories.media/uploaded/images/73d062901c7e4722be110c68551ebe50-600x400px_1x.webp","url_2x":"https://static.istories.media/uploaded/images/73d062901c7e4722be110c68551ebe50-600x400px_2x.webp","for_width":600,"for_height":400,"real_width_1x":600,"real_width_2x":1200,"real_height_1x":400,"real_height_2x":800},{"format":"png","url_1x":"https://static.istories.media/uploaded/images/73d062901c7e4722be110c68551ebe50-600x400px_1x.png","url_2x":"https://static.istories.media/uploaded/images/73d062901c7e4722be110c68551ebe50-600x400px_2x.png","for_width":600,"for_height":400,"real_width_1x":600,"real_width_2x":1200,"real_height_1x":400,"real_height_2x":800},{"format":"webp","url_1x":"https://static.istories.media/uploaded/images/73d062901c7e4722be110c68551ebe50-360x360px_1x.webp","url_2x":"https://static.istories.media/uploaded/images/73d062901c7e4722be110c68551ebe50-360x360px_2x.webp","for_width":360,"for_height":360,"real_width_1x":360,"real_width_2x":720,"real_height_1x":360,"real_height_2x":720},{"format":"png","url_1x":"https://static.istories.media/uploaded/images/73d062901c7e4722be110c68551ebe50-360x360px_1x.png","url_2x":"https://static.istories.media/uploaded/images/73d062901c7e4722be110c68551ebe50-360x360px_2x.png","for_width":360,"for_height":360,"real_width_1x":360,"real_width_2x":720,"real_height_1x":360,"real_height_2x":720}],"entry_image_credit":"","blocks":[{"data":"Сегодня мы научим нашего робота по анализу госконтрактов отправлять письма.","type":"p","className":""},{"data":{"id":2808,"data":{"url":"https://www.youtube.com/embed/2LCiYIrqSVw","text":"Съемка и монтаж: Глеб Лиманский","provider":"youtube","rawInput":"https://www.youtube.com/watch?v=2LCiYIrqSVw","ratio_mobile":"16:9","ratio_desktop":"16:9"},"category":"video-embed"},"type":"video-embed"},{"data":"Для отправки писем в Python есть встроенный модуль <strong>smtplib</strong>. Импортируем модуль и дополнительные надгрузки в наш проект.","type":"p","className":""},{"data":{"id":2588,"data":{"images":[{"imagesList":[{"format":"webp","url_1x":"https://static.istories.media/uploaded/images/620e7e41fef8473882fea34a212f64fe-1200xautopx_1x.webp","url_2x":"https://static.istories.media/uploaded/images/620e7e41fef8473882fea34a212f64fe-1200xautopx_2x.webp","for_width":1200,"for_height":"auto","real_width_1x":1200,"real_width_2x":2400,"real_height_1x":634,"real_height_2x":1267},{"format":"png","url_1x":"https://static.istories.media/uploaded/images/620e7e41fef8473882fea34a212f64fe-1200xautopx_1x.png","url_2x":"https://static.istories.media/uploaded/images/620e7e41fef8473882fea34a212f64fe-1200xautopx_2x.png","for_width":1200,"for_height":"auto","real_width_1x":1200,"real_width_2x":2400,"real_height_1x":634,"real_height_2x":1267}]}]},"category":"image-embed"},"type":"image-embed"},{"data":"Наш код уже умеет выгружать контракты с сайта «ГосЗатраты», формировать из них таблицу — теперь осталось только выслать эту таблицу пользователю. Запишем в переменную <strong>message</strong> то сообщение, которое мы будем высылать. Далее нам понадобится еще несколько переменных.","type":"p","className":""},{"data":{"id":2589,"data":{"images":[{"imagesList":[{"format":"webp","url_1x":"https://static.istories.media/uploaded/images/969140b23dc440fcbfaee1cc663f98e9-1200xautopx_1x.webp","url_2x":"https://static.istories.media/uploaded/images/969140b23dc440fcbfaee1cc663f98e9-1200xautopx_2x.webp","for_width":1200,"for_height":"auto","real_width_1x":1200,"real_width_2x":2400,"real_height_1x":575,"real_height_2x":1150},{"format":"png","url_1x":"https://static.istories.media/uploaded/images/969140b23dc440fcbfaee1cc663f98e9-1200xautopx_1x.png","url_2x":"https://static.istories.media/uploaded/images/969140b23dc440fcbfaee1cc663f98e9-1200xautopx_2x.png","for_width":1200,"for_height":"auto","real_width_1x":1200,"real_width_2x":2400,"real_height_1x":575,"real_height_2x":1150}]}]},"category":"image-embed"},"type":"image-embed"},{"data":"Первым понадобится список email-адресов, куда мы хотим отправить письмо (<strong>email_list</strong>) — запишем туда свой адрес. Далее понадобится логин и пароль почты (<strong>SENDER_EMAIL</strong> и <strong>EMAIL_PASSWORD</strong> соответственно), с которой письмо будет рассылаться. Возможно, для этого стоит зарегистрировать отдельную почту. ","type":"p","className":""},{"data":"Также будет лучше создать файл <strong>config</strong>, в котором будет храниться информация о логине и пароле, чтобы она не была доступна никому кроме вас.&nbsp;Теперь пишем функцию <strong>send_email_with_contracts (message, email_list)</strong> — на вход она получает сообщение, которое нужно отправить, и список получателей письма. ","type":"p","className":""},{"data":"Следом создадим переменную <strong>msg</strong>, которой зададим тип контента, который мы хотим высылать — в данном случае это <strong>alternative</strong>. Укажем также тему письма, адрес отправителя рассылки и адреса получателей. Список получателей мы формируем с помощью функции <strong>join</strong> через запятую. У нас получатель один, но их может быть сколько угодно.","type":"p","className":""},{"data":{"id":2590,"data":{"images":[{"imagesList":[{"format":"webp","url_1x":"https://static.istories.media/uploaded/images/e2a15bebef7f47629db67bdde61441ac-1200xautopx_1x.webp","url_2x":"https://static.istories.media/uploaded/images/e2a15bebef7f47629db67bdde61441ac-1200xautopx_2x.webp","for_width":1200,"for_height":"auto","real_width_1x":1200,"real_width_2x":2400,"real_height_1x":579,"real_height_2x":1158},{"format":"png","url_1x":"https://static.istories.media/uploaded/images/e2a15bebef7f47629db67bdde61441ac-1200xautopx_1x.png","url_2x":"https://static.istories.media/uploaded/images/e2a15bebef7f47629db67bdde61441ac-1200xautopx_2x.png","for_width":1200,"for_height":"auto","real_width_1x":1200,"real_width_2x":2400,"real_height_1x":579,"real_height_2x":1158}]}]},"category":"image-embed"},"type":"image-embed"},{"data":"Создаем непосредственно письмо. Передаем нашу переменную <strong>message</strong> и указываем <strong>html</strong> в качестве типа данных, потому что таблица, которую мы учились делать на <a href=\"https://istories.media/workshops/2021/06/18/pishem-svoego-robota-na-python-urok-9/\" data-type=\"usual\" target=\"_blank\">прошлых уроках</a>, как раз на HTML. Таблицу мы прикрепляем к письму.","type":"p","className":""},{"data":{"id":2591,"data":{"images":[{"imagesList":[{"format":"webp","url_1x":"https://static.istories.media/uploaded/images/486f87fd001d4424a76dd330eae001c8-1200xautopx_1x.webp","url_2x":"https://static.istories.media/uploaded/images/486f87fd001d4424a76dd330eae001c8-1200xautopx_2x.webp","for_width":1200,"for_height":"auto","real_width_1x":1200,"real_width_2x":2400,"real_height_1x":455,"real_height_2x":910},{"format":"png","url_1x":"https://static.istories.media/uploaded/images/486f87fd001d4424a76dd330eae001c8-1200xautopx_1x.png","url_2x":"https://static.istories.media/uploaded/images/486f87fd001d4424a76dd330eae001c8-1200xautopx_2x.png","for_width":1200,"for_height":"auto","real_width_1x":1200,"real_width_2x":2400,"real_height_1x":455,"real_height_2x":910}]}]},"category":"image-embed"},"type":"image-embed"},{"data":"С помощью сервера Google и безопасного протокола мы заходим в почту-отправителя, передаем логин и пароль и командой <strong>send_message </strong>отправляем сообщение.","type":"p","className":""},{"data":{"id":2592,"data":{"images":[{"imagesList":[{"format":"webp","url_1x":"https://static.istories.media/uploaded/images/e4ae3e77f57b415a90c9624e188fa9a7-1200xautopx_1x.webp","url_2x":"https://static.istories.media/uploaded/images/e4ae3e77f57b415a90c9624e188fa9a7-1200xautopx_2x.webp","for_width":1200,"for_height":"auto","real_width_1x":1200,"real_width_2x":2400,"real_height_1x":611,"real_height_2x":1222},{"format":"png","url_1x":"https://static.istories.media/uploaded/images/e4ae3e77f57b415a90c9624e188fa9a7-1200xautopx_1x.png","url_2x":"https://static.istories.media/uploaded/images/e4ae3e77f57b415a90c9624e188fa9a7-1200xautopx_2x.png","for_width":1200,"for_height":"auto","real_width_1x":1200,"real_width_2x":2400,"real_height_1x":611,"real_height_2x":1222}]}]},"category":"image-embed"},"type":"image-embed"},{"data":"Если вы следовали за нашими уроками, то после запуска всего кода на вашу почту должно прийти письмо с подборкой госконтрактов. В следующий раз научим наш код запускаться автоматически. Если у вас что-то не получилось —&nbsp;не стесняйтесь писать в наш <a href=\"https://t.me/istories_helloworld\" data-type=\"usual\" target=\"_blank\">чат</a> в Telegram, постараемся помочь.","type":"p","className":""}],"created_at_date":"2021-07-09","updated_at_date":null,"created_at_date_time":null,"updated_at_date_time":null,"is_exclusive":false,"is_urgent":false,"initially_published_at":"2021-07-09T06:41:00.638Z","has_additional_facts":false,"locale":"ru","has_visualization_without_fallback":false,"spec_project":null,"header_secondary":"","warning":"","is_translated_by_ai":false,"with_donation_banner":true,"donation_banner_header":"","donation_banner_text":"","media_outlet__codename":null,"with_authors_request_banner":false,"faq":[],"_path":"/workshops/2021/07/09/pishem-svoego-robota-na-python-urok-11/","is_published":true,"authors_list":[{"id":8,"lead":"Главная редакторка «Важных историй»","_path":"alesya-marokhovskaya","avatar":[{"format":"webp","url_1x":"https://static.istories.media/uploaded/images/12285015ded6416c9ce475e3a939d7f7-896x504px_1x.webp","url_2x":"https://static.istories.media/uploaded/images/12285015ded6416c9ce475e3a939d7f7-896x504px_2x.webp","for_width":896,"for_height":504,"original_url":"https://static.istories.media/uploaded/images/12285015ded6416c9ce475e3a939d7f7-original.jpg","real_width_1x":896,"real_width_2x":1792,"real_height_1x":504,"real_height_2x":1008},{"format":"jpg","url_1x":"https://static.istories.media/uploaded/images/12285015ded6416c9ce475e3a939d7f7-896x504px_1x.jpg","url_2x":"https://static.istories.media/uploaded/images/12285015ded6416c9ce475e3a939d7f7-896x504px_2x.jpg","for_width":896,"for_height":504,"original_url":"https://static.istories.media/uploaded/images/12285015ded6416c9ce475e3a939d7f7-original.jpg","real_width_1x":896,"real_width_2x":1792,"real_height_1x":504,"real_height_2x":1008},{"format":"webp","url_1x":"https://static.istories.media/uploaded/images/12285015ded6416c9ce475e3a939d7f7-688x387px_1x.webp","url_2x":"https://static.istories.media/uploaded/images/12285015ded6416c9ce475e3a939d7f7-688x387px_2x.webp","for_width":688,"for_height":387,"original_url":"https://static.istories.media/uploaded/images/12285015ded6416c9ce475e3a939d7f7-original.jpg","real_width_1x":688,"real_width_2x":1376,"real_height_1x":387,"real_height_2x":774},{"format":"jpg","url_1x":"https://static.istories.media/uploaded/images/12285015ded6416c9ce475e3a939d7f7-688x387px_1x.jpg","url_2x":"https://static.istories.media/uploaded/images/12285015ded6416c9ce475e3a939d7f7-688x387px_2x.jpg","for_width":688,"for_height":387,"original_url":"https://static.istories.media/uploaded/images/12285015ded6416c9ce475e3a939d7f7-original.jpg","real_width_1x":688,"real_width_2x":1376,"real_height_1x":387,"real_height_2x":774},{"format":"webp","url_1x":"https://static.istories.media/uploaded/images/12285015ded6416c9ce475e3a939d7f7-416x234px_1x.webp","url_2x":"https://static.istories.media/uploaded/images/12285015ded6416c9ce475e3a939d7f7-416x234px_2x.webp","for_width":416,"for_height":234,"original_url":"https://static.istories.media/uploaded/images/12285015ded6416c9ce475e3a939d7f7-original.jpg","real_width_1x":416,"real_width_2x":832,"real_height_1x":234,"real_height_2x":468},{"format":"jpg","url_1x":"https://static.istories.media/uploaded/images/12285015ded6416c9ce475e3a939d7f7-416x234px_1x.jpg","url_2x":"https://static.istories.media/uploaded/images/12285015ded6416c9ce475e3a939d7f7-416x234px_2x.jpg","for_width":416,"for_height":234,"original_url":"https://static.istories.media/uploaded/images/12285015ded6416c9ce475e3a939d7f7-original.jpg","real_width_1x":416,"real_width_2x":832,"real_height_1x":234,"real_height_2x":468},{"format":"webp","url_1x":"https://static.istories.media/uploaded/images/12285015ded6416c9ce475e3a939d7f7-336x189px_1x.webp","url_2x":"https://static.istories.media/uploaded/images/12285015ded6416c9ce475e3a939d7f7-336x189px_2x.webp","for_width":336,"for_height":189,"original_url":"https://static.istories.media/uploaded/images/12285015ded6416c9ce475e3a939d7f7-original.jpg","real_width_1x":336,"real_width_2x":672,"real_height_1x":188,"real_height_2x":378},{"format":"jpg","url_1x":"https://static.istories.media/uploaded/images/12285015ded6416c9ce475e3a939d7f7-336x189px_1x.jpg","url_2x":"https://static.istories.media/uploaded/images/12285015ded6416c9ce475e3a939d7f7-336x189px_2x.jpg","for_width":336,"for_height":189,"original_url":"https://static.istories.media/uploaded/images/12285015ded6416c9ce475e3a939d7f7-original.jpg","real_width_1x":336,"real_width_2x":672,"real_height_1x":188,"real_height_2x":378}],"lead_en":"Editor-in-chief","ordering":null,"position":"Редакторка","is_public":true,"last_name":"Мароховская","first_name":"Алеся","position_en":"Editor","last_name_en":"Marokhovskaya","first_name_en":"Alesya"}],"external_authors_list":[],"editors_list":[],"external_editors_list":[],"participants_list":[],"external_participants_list":[],"partners_list":[],"case":null,"translated_material":null,"audio_material":null,"text_material":null,"id":288,"topics":[],"meta":{"og_title":"","og_description":"","twitter_title":"","twitter_description":"","og_image":"https://static.istories.media/uploaded/images/share/a6fc5c1679ab46cc99c3d8ff36f8ca83.png","twitter_image":"https://static.istories.media/uploaded/images/share/a6fc5c1679ab46cc99c3d8ff36f8ca83.png","vk_image":"https://static.istories.media/uploaded/images/share/vk/803d82053b3d416ea1e9231719dba884.png"},"tags":["html","python","ddj","smtplib","email"],"playlist":null,"socialMediaBanners":[{"id":10,"text":"У пропаганды есть миллиарды из бюджета. У нас — только вы","link":"","codename":"donation"},{"id":14,"text":"Список погибших и пропавших без вести российских военных","link":"","codename":"haron"},{"id":18,"text":"","link":"","codename":"mobile_app"},{"id":6,"text":"Оставайтесь на связи: подпишитесь на нашу рассылку","link":"","codename":"newsletter"}]}},"staticQueryHashes":["764694655"],"slicesMap":{}}