[{"data":1,"prerenderedAt":1796},["ShallowReactive",2],{"doc-\u002Fexamples\u002Fpython-email-sender-script-example":3},{"id":4,"title":5,"body":6,"description":1789,"extension":1790,"meta":1791,"navigation":93,"path":1792,"seo":1793,"stem":1794,"__hash__":1795},"content\u002Fexamples\u002Fpython-email-sender-script-example.md","Python Email Sender Script Example",{"type":7,"value":8,"toc":1764},"minimark",[9,13,17,20,36,39,44,335,338,342,345,359,362,376,379,383,386,400,403,422,425,429,432,646,649,707,716,720,726,729,756,761,764,791,794,799,802,829,834,837,869,872,886,891,894,913,916,920,923,936,939,947,950,954,957,960,1235,1238,1245,1249,1252,1275,1278,1298,1310,1314,1317,1338,1341,1384,1391,1395,1398,1421,1424,1685,1688,1692,1696,1705,1709,1712,1716,1722,1726,1729,1733,1760],[10,11,5],"h1",{"id":12},"python-email-sender-script-example",[14,15,16],"p",{},"This example shows how to send an email in Python using the standard library.",[14,18,19],{},"You will learn:",[21,22,23,27,30,33],"ul",{},[24,25,26],"li",{},"How to create a simple email message",[24,28,29],{},"How to connect to an SMTP server",[24,31,32],{},"How to log in and send the email",[24,34,35],{},"Which values you must replace before the script works",[14,37,38],{},"This page focuses on a small working script. It also covers common setup problems that beginners often hit.",[40,41,43],"h2",{"id":42},"quick-example","Quick example",[45,46,51],"pre",{"className":47,"code":48,"language":49,"meta":50,"style":50},"language-python shiki shiki-themes material-theme-lighter github-light github-dark","import smtplib\nfrom email.message import EmailMessage\n\nmsg = EmailMessage()\nmsg['Subject'] = 'Test email from Python'\nmsg['From'] = 'your_email@example.com'\nmsg['To'] = 'friend@example.com'\nmsg.set_content('Hello from Python!')\n\nwith smtplib.SMTP_SSL('smtp.example.com', 465) as smtp:\n    smtp.login('your_email@example.com', 'your_password')\n    smtp.send_message(msg)\n\nprint('Email sent')\n","python","",[52,53,54,67,88,95,112,146,171,196,219,224,266,296,312,317],"code",{"__ignoreMap":50},[55,56,59,63],"span",{"class":57,"line":58},"line",1,[55,60,62],{"class":61},"sVHd0","import",[55,64,66],{"class":65},"su5hD"," smtplib\n",[55,68,70,73,76,80,83,85],{"class":57,"line":69},2,[55,71,72],{"class":61},"from",[55,74,75],{"class":65}," email",[55,77,79],{"class":78},"sP7_E",".",[55,81,82],{"class":65},"message ",[55,84,62],{"class":61},[55,86,87],{"class":65}," EmailMessage\n",[55,89,91],{"class":57,"line":90},3,[55,92,94],{"emptyLinePlaceholder":93},true,"\n",[55,96,98,101,105,109],{"class":57,"line":97},4,[55,99,100],{"class":65},"msg ",[55,102,104],{"class":103},"smGrS","=",[55,106,108],{"class":107},"slqww"," EmailMessage",[55,110,111],{"class":78},"()\n",[55,113,115,118,121,125,129,131,134,137,140,143],{"class":57,"line":114},5,[55,116,117],{"class":65},"msg",[55,119,120],{"class":78},"[",[55,122,124],{"class":123},"sjJ54","'",[55,126,128],{"class":127},"s_sjI","Subject",[55,130,124],{"class":123},[55,132,133],{"class":78},"]",[55,135,136],{"class":103}," =",[55,138,139],{"class":123}," '",[55,141,142],{"class":127},"Test email from Python",[55,144,145],{"class":123},"'\n",[55,147,149,151,153,155,158,160,162,164,166,169],{"class":57,"line":148},6,[55,150,117],{"class":65},[55,152,120],{"class":78},[55,154,124],{"class":123},[55,156,157],{"class":127},"From",[55,159,124],{"class":123},[55,161,133],{"class":78},[55,163,136],{"class":103},[55,165,139],{"class":123},[55,167,168],{"class":127},"your_email@example.com",[55,170,145],{"class":123},[55,172,174,176,178,180,183,185,187,189,191,194],{"class":57,"line":173},7,[55,175,117],{"class":65},[55,177,120],{"class":78},[55,179,124],{"class":123},[55,181,182],{"class":127},"To",[55,184,124],{"class":123},[55,186,133],{"class":78},[55,188,136],{"class":103},[55,190,139],{"class":123},[55,192,193],{"class":127},"friend@example.com",[55,195,145],{"class":123},[55,197,199,201,203,206,209,211,214,216],{"class":57,"line":198},8,[55,200,117],{"class":65},[55,202,79],{"class":78},[55,204,205],{"class":107},"set_content",[55,207,208],{"class":78},"(",[55,210,124],{"class":123},[55,212,213],{"class":127},"Hello from Python!",[55,215,124],{"class":123},[55,217,218],{"class":78},")\n",[55,220,222],{"class":57,"line":221},9,[55,223,94],{"emptyLinePlaceholder":93},[55,225,227,230,233,235,238,240,242,245,247,250,254,257,260,263],{"class":57,"line":226},10,[55,228,229],{"class":61},"with",[55,231,232],{"class":65}," smtplib",[55,234,79],{"class":78},[55,236,237],{"class":107},"SMTP_SSL",[55,239,208],{"class":78},[55,241,124],{"class":123},[55,243,244],{"class":127},"smtp.example.com",[55,246,124],{"class":123},[55,248,249],{"class":78},",",[55,251,253],{"class":252},"srdBf"," 465",[55,255,256],{"class":78},")",[55,258,259],{"class":61}," as",[55,261,262],{"class":65}," smtp",[55,264,265],{"class":78},":\n",[55,267,269,272,274,277,279,281,283,285,287,289,292,294],{"class":57,"line":268},11,[55,270,271],{"class":65},"    smtp",[55,273,79],{"class":78},[55,275,276],{"class":107},"login",[55,278,208],{"class":78},[55,280,124],{"class":123},[55,282,168],{"class":127},[55,284,124],{"class":123},[55,286,249],{"class":78},[55,288,139],{"class":123},[55,290,291],{"class":127},"your_password",[55,293,124],{"class":123},[55,295,218],{"class":78},[55,297,299,301,303,306,308,310],{"class":57,"line":298},12,[55,300,271],{"class":65},[55,302,79],{"class":78},[55,304,305],{"class":107},"send_message",[55,307,208],{"class":78},[55,309,117],{"class":107},[55,311,218],{"class":78},[55,313,315],{"class":57,"line":314},13,[55,316,94],{"emptyLinePlaceholder":93},[55,318,320,324,326,328,331,333],{"class":57,"line":319},14,[55,321,323],{"class":322},"sptTA","print",[55,325,208],{"class":78},[55,327,124],{"class":123},[55,329,330],{"class":127},"Email sent",[55,332,124],{"class":123},[55,334,218],{"class":78},[14,336,337],{},"Replace the SMTP server, port, email address, and password with real values from your email provider. Some providers require an app password instead of your normal password.",[40,339,341],{"id":340},"what-this-example-shows","What this example shows",[14,343,344],{},"This script demonstrates the basic steps for sending an email:",[21,346,347,350,353,356],{},[24,348,349],{},"Build an email message with a subject, sender, recipient, and body",[24,351,352],{},"Connect to an SMTP server",[24,354,355],{},"Log in with your email account",[24,357,358],{},"Send the message",[14,360,361],{},"It uses:",[21,363,364,370],{},[24,365,366,369],{},[52,367,368],{},"smtplib"," to talk to the email server",[24,371,372,375],{},[52,373,374],{},"EmailMessage"," to build the email",[14,377,378],{},"You do not need to install extra packages for this basic example. Both modules are included with Python.",[40,380,382],{"id":381},"before-you-run-the-script","Before you run the script",[14,384,385],{},"Before this can work, you need a few real values from your email provider:",[21,387,388,391,394,397],{},[24,389,390],{},"An email account that allows SMTP sending",[24,392,393],{},"The correct SMTP server name",[24,395,396],{},"The correct SMTP port",[24,398,399],{},"A working password or app password",[14,401,402],{},"A few important notes:",[21,404,405,413,419],{},[24,406,407,408,412],{},"Some providers do ",[409,410,411],"strong",{},"not"," let scripts use your normal password",[24,414,415,416],{},"Some providers require an ",[409,417,418],{},"app password",[24,420,421],{},"Some providers block sign-in attempts until you enable a security setting",[14,423,424],{},"An app password is a special password made for scripts or apps. It is often safer than using your normal account password directly.",[40,426,428],{"id":427},"how-the-script-works","How the script works",[14,430,431],{},"Here is the same script again:",[45,433,434],{"className":47,"code":48,"language":49,"meta":50,"style":50},[52,435,436,442,456,460,470,492,514,536,554,558,588,614,628,632],{"__ignoreMap":50},[55,437,438,440],{"class":57,"line":58},[55,439,62],{"class":61},[55,441,66],{"class":65},[55,443,444,446,448,450,452,454],{"class":57,"line":69},[55,445,72],{"class":61},[55,447,75],{"class":65},[55,449,79],{"class":78},[55,451,82],{"class":65},[55,453,62],{"class":61},[55,455,87],{"class":65},[55,457,458],{"class":57,"line":90},[55,459,94],{"emptyLinePlaceholder":93},[55,461,462,464,466,468],{"class":57,"line":97},[55,463,100],{"class":65},[55,465,104],{"class":103},[55,467,108],{"class":107},[55,469,111],{"class":78},[55,471,472,474,476,478,480,482,484,486,488,490],{"class":57,"line":114},[55,473,117],{"class":65},[55,475,120],{"class":78},[55,477,124],{"class":123},[55,479,128],{"class":127},[55,481,124],{"class":123},[55,483,133],{"class":78},[55,485,136],{"class":103},[55,487,139],{"class":123},[55,489,142],{"class":127},[55,491,145],{"class":123},[55,493,494,496,498,500,502,504,506,508,510,512],{"class":57,"line":148},[55,495,117],{"class":65},[55,497,120],{"class":78},[55,499,124],{"class":123},[55,501,157],{"class":127},[55,503,124],{"class":123},[55,505,133],{"class":78},[55,507,136],{"class":103},[55,509,139],{"class":123},[55,511,168],{"class":127},[55,513,145],{"class":123},[55,515,516,518,520,522,524,526,528,530,532,534],{"class":57,"line":173},[55,517,117],{"class":65},[55,519,120],{"class":78},[55,521,124],{"class":123},[55,523,182],{"class":127},[55,525,124],{"class":123},[55,527,133],{"class":78},[55,529,136],{"class":103},[55,531,139],{"class":123},[55,533,193],{"class":127},[55,535,145],{"class":123},[55,537,538,540,542,544,546,548,550,552],{"class":57,"line":198},[55,539,117],{"class":65},[55,541,79],{"class":78},[55,543,205],{"class":107},[55,545,208],{"class":78},[55,547,124],{"class":123},[55,549,213],{"class":127},[55,551,124],{"class":123},[55,553,218],{"class":78},[55,555,556],{"class":57,"line":221},[55,557,94],{"emptyLinePlaceholder":93},[55,559,560,562,564,566,568,570,572,574,576,578,580,582,584,586],{"class":57,"line":226},[55,561,229],{"class":61},[55,563,232],{"class":65},[55,565,79],{"class":78},[55,567,237],{"class":107},[55,569,208],{"class":78},[55,571,124],{"class":123},[55,573,244],{"class":127},[55,575,124],{"class":123},[55,577,249],{"class":78},[55,579,253],{"class":252},[55,581,256],{"class":78},[55,583,259],{"class":61},[55,585,262],{"class":65},[55,587,265],{"class":78},[55,589,590,592,594,596,598,600,602,604,606,608,610,612],{"class":57,"line":268},[55,591,271],{"class":65},[55,593,79],{"class":78},[55,595,276],{"class":107},[55,597,208],{"class":78},[55,599,124],{"class":123},[55,601,168],{"class":127},[55,603,124],{"class":123},[55,605,249],{"class":78},[55,607,139],{"class":123},[55,609,291],{"class":127},[55,611,124],{"class":123},[55,613,218],{"class":78},[55,615,616,618,620,622,624,626],{"class":57,"line":298},[55,617,271],{"class":65},[55,619,79],{"class":78},[55,621,305],{"class":107},[55,623,208],{"class":78},[55,625,117],{"class":107},[55,627,218],{"class":78},[55,629,630],{"class":57,"line":314},[55,631,94],{"emptyLinePlaceholder":93},[55,633,634,636,638,640,642,644],{"class":57,"line":319},[55,635,323],{"class":322},[55,637,208],{"class":78},[55,639,124],{"class":123},[55,641,330],{"class":127},[55,643,124],{"class":123},[55,645,218],{"class":78},[14,647,648],{},"Step by step:",[21,650,651,657,663,669,683,689,695,701],{},[24,652,653,656],{},[52,654,655],{},"import smtplib"," imports the module used to connect to the mail server",[24,658,659,662],{},[52,660,661],{},"from email.message import EmailMessage"," imports the class used to create the email",[24,664,665,668],{},[52,666,667],{},"msg = EmailMessage()"," creates a new email message object",[24,670,671,674,675,678,679,682],{},[52,672,673],{},"msg['Subject']",", ",[52,676,677],{},"msg['From']",", and ",[52,680,681],{},"msg['To']"," set the email headers",[24,684,685,688],{},[52,686,687],{},"msg.set_content(...)"," sets the plain text body",[24,690,691,694],{},[52,692,693],{},"smtplib.SMTP_SSL(...)"," opens a secure connection to the SMTP server",[24,696,697,700],{},[52,698,699],{},"smtp.login(...)"," signs in to your account",[24,702,703,706],{},[52,704,705],{},"smtp.send_message(msg)"," sends the email",[14,708,709,710,715],{},"If you are new to Python input, see ",[711,712,714],"a",{"href":713},"\u002Freference\u002Fpython-input-function-explained\u002F","Python input() function explained"," for a simple way to ask the user for values.",[40,717,719],{"id":718},"important-lines-to-explain","Important lines to explain",[721,722,724],"h3",{"id":723},"msgsubject",[52,725,673],{},[14,727,728],{},"This sets the subject line that the recipient sees.",[45,730,732],{"className":47,"code":731,"language":49,"meta":50,"style":50},"msg['Subject'] = 'Test email from Python'\n",[52,733,734],{"__ignoreMap":50},[55,735,736,738,740,742,744,746,748,750,752,754],{"class":57,"line":58},[55,737,117],{"class":65},[55,739,120],{"class":78},[55,741,124],{"class":123},[55,743,128],{"class":127},[55,745,124],{"class":123},[55,747,133],{"class":78},[55,749,136],{"class":103},[55,751,139],{"class":123},[55,753,142],{"class":127},[55,755,145],{"class":123},[721,757,759],{"id":758},"msgfrom",[52,760,677],{},[14,762,763],{},"This sets the sender address.",[45,765,767],{"className":47,"code":766,"language":49,"meta":50,"style":50},"msg['From'] = 'your_email@example.com'\n",[52,768,769],{"__ignoreMap":50},[55,770,771,773,775,777,779,781,783,785,787,789],{"class":57,"line":58},[55,772,117],{"class":65},[55,774,120],{"class":78},[55,776,124],{"class":123},[55,778,157],{"class":127},[55,780,124],{"class":123},[55,782,133],{"class":78},[55,784,136],{"class":103},[55,786,139],{"class":123},[55,788,168],{"class":127},[55,790,145],{"class":123},[14,792,793],{},"In many cases, this should match the account you log in with. If they do not match, some providers may reject the message.",[721,795,797],{"id":796},"msgto",[52,798,681],{},[14,800,801],{},"This is the recipient email address.",[45,803,805],{"className":47,"code":804,"language":49,"meta":50,"style":50},"msg['To'] = 'friend@example.com'\n",[52,806,807],{"__ignoreMap":50},[55,808,809,811,813,815,817,819,821,823,825,827],{"class":57,"line":58},[55,810,117],{"class":65},[55,812,120],{"class":78},[55,814,124],{"class":123},[55,816,182],{"class":127},[55,818,124],{"class":123},[55,820,133],{"class":78},[55,822,136],{"class":103},[55,824,139],{"class":123},[55,826,193],{"class":127},[55,828,145],{"class":123},[721,830,832],{"id":831},"smtplogin",[52,833,699],{},[14,835,836],{},"This authenticates with the email server.",[45,838,840],{"className":47,"code":839,"language":49,"meta":50,"style":50},"smtp.login('your_email@example.com', 'your_password')\n",[52,841,842],{"__ignoreMap":50},[55,843,844,847,849,851,853,855,857,859,861,863,865,867],{"class":57,"line":58},[55,845,846],{"class":65},"smtp",[55,848,79],{"class":78},[55,850,276],{"class":107},[55,852,208],{"class":78},[55,854,124],{"class":123},[55,856,168],{"class":127},[55,858,124],{"class":123},[55,860,249],{"class":78},[55,862,139],{"class":123},[55,864,291],{"class":127},[55,866,124],{"class":123},[55,868,218],{"class":78},[14,870,871],{},"If this line fails, the problem is often:",[21,873,874,877,880,883],{},[24,875,876],{},"Wrong email address",[24,878,879],{},"Wrong password",[24,881,882],{},"App password required",[24,884,885],{},"Provider security settings blocking the login",[721,887,889],{"id":888},"smtpsend_messagemsg",[52,890,705],{},[14,892,893],{},"This actually sends the email.",[45,895,897],{"className":47,"code":896,"language":49,"meta":50,"style":50},"smtp.send_message(msg)\n",[52,898,899],{"__ignoreMap":50},[55,900,901,903,905,907,909,911],{"class":57,"line":58},[55,902,846],{"class":65},[55,904,79],{"class":78},[55,906,305],{"class":107},[55,908,208],{"class":78},[55,910,117],{"class":107},[55,912,218],{"class":78},[14,914,915],{},"If the connection and login are correct, this sends the message to the server.",[40,917,919],{"id":918},"expected-result","Expected result",[14,921,922],{},"If everything works:",[21,924,925,930,933],{},[24,926,927,928],{},"The script prints ",[52,929,330],{},[24,931,932],{},"The recipient should receive a plain text email",[24,934,935],{},"The message may still be delayed, filtered, or sent to spam",[14,937,938],{},"Example output:",[45,940,945],{"className":941,"code":943,"language":944,"meta":50},[942],"language-text","Email sent\n","text",[52,946,943],{"__ignoreMap":50},[14,948,949],{},"Even when the script finishes without an error, delivery is not always guaranteed. The provider might still reject or filter the message later.",[40,951,953],{"id":952},"safer-way-to-handle-passwords","Safer way to handle passwords",[14,955,956],{},"Do not hard-code real passwords in a file you plan to share.",[14,958,959],{},"A simple beginner-friendly option is to ask for the password while the script runs:",[45,961,963],{"className":47,"code":962,"language":49,"meta":50,"style":50},"import smtplib\nfrom email.message import EmailMessage\n\nsender = input(\"Enter your email address: \")\npassword = input(\"Enter your email password or app password: \")\nrecipient = input(\"Enter recipient email address: \")\n\nmsg = EmailMessage()\nmsg[\"Subject\"] = \"Test email from Python\"\nmsg[\"From\"] = sender\nmsg[\"To\"] = recipient\nmsg.set_content(\"Hello from Python!\")\n\nwith smtplib.SMTP_SSL(\"smtp.example.com\", 465) as smtp:\n    smtp.login(sender, password)\n    smtp.send_message(msg)\n\nprint(\"Email sent\")\n",[52,964,965,971,985,989,1011,1031,1051,1055,1065,1089,1108,1127,1145,1149,1179,1200,1215,1220],{"__ignoreMap":50},[55,966,967,969],{"class":57,"line":58},[55,968,62],{"class":61},[55,970,66],{"class":65},[55,972,973,975,977,979,981,983],{"class":57,"line":69},[55,974,72],{"class":61},[55,976,75],{"class":65},[55,978,79],{"class":78},[55,980,82],{"class":65},[55,982,62],{"class":61},[55,984,87],{"class":65},[55,986,987],{"class":57,"line":90},[55,988,94],{"emptyLinePlaceholder":93},[55,990,991,994,996,999,1001,1004,1007,1009],{"class":57,"line":97},[55,992,993],{"class":65},"sender ",[55,995,104],{"class":103},[55,997,998],{"class":322}," input",[55,1000,208],{"class":78},[55,1002,1003],{"class":123},"\"",[55,1005,1006],{"class":127},"Enter your email address: ",[55,1008,1003],{"class":123},[55,1010,218],{"class":78},[55,1012,1013,1016,1018,1020,1022,1024,1027,1029],{"class":57,"line":114},[55,1014,1015],{"class":65},"password ",[55,1017,104],{"class":103},[55,1019,998],{"class":322},[55,1021,208],{"class":78},[55,1023,1003],{"class":123},[55,1025,1026],{"class":127},"Enter your email password or app password: ",[55,1028,1003],{"class":123},[55,1030,218],{"class":78},[55,1032,1033,1036,1038,1040,1042,1044,1047,1049],{"class":57,"line":148},[55,1034,1035],{"class":65},"recipient ",[55,1037,104],{"class":103},[55,1039,998],{"class":322},[55,1041,208],{"class":78},[55,1043,1003],{"class":123},[55,1045,1046],{"class":127},"Enter recipient email address: ",[55,1048,1003],{"class":123},[55,1050,218],{"class":78},[55,1052,1053],{"class":57,"line":173},[55,1054,94],{"emptyLinePlaceholder":93},[55,1056,1057,1059,1061,1063],{"class":57,"line":198},[55,1058,100],{"class":65},[55,1060,104],{"class":103},[55,1062,108],{"class":107},[55,1064,111],{"class":78},[55,1066,1067,1069,1071,1073,1075,1077,1079,1081,1084,1086],{"class":57,"line":221},[55,1068,117],{"class":65},[55,1070,120],{"class":78},[55,1072,1003],{"class":123},[55,1074,128],{"class":127},[55,1076,1003],{"class":123},[55,1078,133],{"class":78},[55,1080,136],{"class":103},[55,1082,1083],{"class":123}," \"",[55,1085,142],{"class":127},[55,1087,1088],{"class":123},"\"\n",[55,1090,1091,1093,1095,1097,1099,1101,1103,1105],{"class":57,"line":226},[55,1092,117],{"class":65},[55,1094,120],{"class":78},[55,1096,1003],{"class":123},[55,1098,157],{"class":127},[55,1100,1003],{"class":123},[55,1102,133],{"class":78},[55,1104,136],{"class":103},[55,1106,1107],{"class":65}," sender\n",[55,1109,1110,1112,1114,1116,1118,1120,1122,1124],{"class":57,"line":268},[55,1111,117],{"class":65},[55,1113,120],{"class":78},[55,1115,1003],{"class":123},[55,1117,182],{"class":127},[55,1119,1003],{"class":123},[55,1121,133],{"class":78},[55,1123,136],{"class":103},[55,1125,1126],{"class":65}," recipient\n",[55,1128,1129,1131,1133,1135,1137,1139,1141,1143],{"class":57,"line":298},[55,1130,117],{"class":65},[55,1132,79],{"class":78},[55,1134,205],{"class":107},[55,1136,208],{"class":78},[55,1138,1003],{"class":123},[55,1140,213],{"class":127},[55,1142,1003],{"class":123},[55,1144,218],{"class":78},[55,1146,1147],{"class":57,"line":314},[55,1148,94],{"emptyLinePlaceholder":93},[55,1150,1151,1153,1155,1157,1159,1161,1163,1165,1167,1169,1171,1173,1175,1177],{"class":57,"line":319},[55,1152,229],{"class":61},[55,1154,232],{"class":65},[55,1156,79],{"class":78},[55,1158,237],{"class":107},[55,1160,208],{"class":78},[55,1162,1003],{"class":123},[55,1164,244],{"class":127},[55,1166,1003],{"class":123},[55,1168,249],{"class":78},[55,1170,253],{"class":252},[55,1172,256],{"class":78},[55,1174,259],{"class":61},[55,1176,262],{"class":65},[55,1178,265],{"class":78},[55,1180,1182,1184,1186,1188,1190,1193,1195,1198],{"class":57,"line":1181},15,[55,1183,271],{"class":65},[55,1185,79],{"class":78},[55,1187,276],{"class":107},[55,1189,208],{"class":78},[55,1191,1192],{"class":107},"sender",[55,1194,249],{"class":78},[55,1196,1197],{"class":107}," password",[55,1199,218],{"class":78},[55,1201,1203,1205,1207,1209,1211,1213],{"class":57,"line":1202},16,[55,1204,271],{"class":65},[55,1206,79],{"class":78},[55,1208,305],{"class":107},[55,1210,208],{"class":78},[55,1212,117],{"class":107},[55,1214,218],{"class":78},[55,1216,1218],{"class":57,"line":1217},17,[55,1219,94],{"emptyLinePlaceholder":93},[55,1221,1223,1225,1227,1229,1231,1233],{"class":57,"line":1222},18,[55,1224,323],{"class":322},[55,1226,208],{"class":78},[55,1228,1003],{"class":123},[55,1230,330],{"class":127},[55,1232,1003],{"class":123},[55,1234,218],{"class":78},[14,1236,1237],{},"This is better than saving the password in the script, but it still shows the password on screen while typing.",[14,1239,1240,1241,1244],{},"A more advanced next step is to use environment variables. If you are still learning, start with ",[52,1242,1243],{},"input()"," and move to safer storage later.",[40,1246,1248],{"id":1247},"common-problems-with-email-scripts","Common problems with email scripts",[14,1250,1251],{},"These are the most common reasons the script does not work:",[21,1253,1254,1257,1260,1263,1266,1269,1272],{},[24,1255,1256],{},"Wrong SMTP host",[24,1258,1259],{},"Wrong SMTP port",[24,1261,1262],{},"Incorrect username or password",[24,1264,1265],{},"Using a normal password when an app password is required",[24,1267,1268],{},"Provider blocks sign-in from scripts",[24,1270,1271],{},"No internet connection",[24,1273,1274],{},"Firewall or network blocks the SMTP port",[14,1276,1277],{},"Other common causes include:",[21,1279,1280,1283,1286,1289,1292],{},[24,1281,1282],{},"Using the wrong SMTP server address",[24,1284,1285],{},"Using the wrong SMTP port",[24,1287,1288],{},"Entering the wrong email password",[24,1290,1291],{},"Trying to send from an account that blocks SMTP access",[24,1293,1294,1295,1297],{},"Forgetting to match the ",[52,1296,157],{}," address with the login account",[14,1299,1300,1301,1305,1306,79],{},"If you get an error, read the full message carefully. A good next step is learning ",[711,1302,1304],{"href":1303},"\u002Flearn\u002Fpython-errors-and-exceptions-explained\u002F","Python errors and exceptions explained"," and ",[711,1307,1309],{"href":1308},"\u002Fhow-to\u002Fhow-to-handle-exceptions-in-python\u002F","how to handle exceptions in Python",[40,1311,1313],{"id":1312},"simple-debugging-checks","Simple debugging checks",[14,1315,1316],{},"Try these checks before changing a lot of code:",[21,1318,1319,1326,1329,1332,1335],{},[24,1320,1321,1322,1325],{},"Make sure the email address in ",[52,1323,1324],{},"login()"," is correct",[24,1327,1328],{},"Check the SMTP server and port in your provider's documentation",[24,1330,1331],{},"Print the host and port before connecting",[24,1333,1334],{},"Try sending to your own email address first",[24,1336,1337],{},"Read the full error message instead of guessing",[14,1339,1340],{},"Useful test commands:",[45,1342,1346],{"className":1343,"code":1344,"language":1345,"meta":50,"style":50},"language-bash shiki shiki-themes material-theme-lighter github-light github-dark","python email_sender.py\npython -c \"import smtplib; print('smtplib imported successfully')\"\npython -c \"from email.message import EmailMessage; print('EmailMessage imported successfully')\"\n","bash",[52,1347,1348,1356,1371],{"__ignoreMap":50},[55,1349,1350,1353],{"class":57,"line":58},[55,1351,49],{"class":1352},"sbgvK",[55,1354,1355],{"class":127}," email_sender.py\n",[55,1357,1358,1360,1364,1366,1369],{"class":57,"line":69},[55,1359,49],{"class":1352},[55,1361,1363],{"class":1362},"stzsN"," -c",[55,1365,1083],{"class":123},[55,1367,1368],{"class":127},"import smtplib; print('smtplib imported successfully')",[55,1370,1088],{"class":123},[55,1372,1373,1375,1377,1379,1382],{"class":57,"line":90},[55,1374,49],{"class":1352},[55,1376,1363],{"class":1362},[55,1378,1083],{"class":123},[55,1380,1381],{"class":127},"from email.message import EmailMessage; print('EmailMessage imported successfully')",[55,1383,1088],{"class":123},[14,1385,1386,1387,79],{},"If imports fail, the problem may be with your Python installation or environment. If you see missing module errors in other scripts, read ",[711,1388,1390],{"href":1389},"\u002Ferrors\u002Fmodulenotfounderror-no-module-named-x-fix\u002F","ModuleNotFoundError: no module named X fix",[40,1392,1394],{"id":1393},"good-next-steps-for-readers","Good next steps for readers",[14,1396,1397],{},"Once this basic version works, you can improve it by:",[21,1399,1400,1403,1406,1409,1412],{},[24,1401,1402],{},"Adding HTML email content",[24,1404,1405],{},"Sending to multiple recipients",[24,1407,1408],{},"Attaching a file",[24,1410,1411],{},"Moving login details out of the script",[24,1413,1414,1415,1305,1418],{},"Wrapping the send code in ",[52,1416,1417],{},"try",[52,1419,1420],{},"except",[14,1422,1423],{},"Here is a simple version with basic error handling:",[45,1425,1427],{"className":47,"code":1426,"language":49,"meta":50,"style":50},"import smtplib\nfrom email.message import EmailMessage\n\nmsg = EmailMessage()\nmsg[\"Subject\"] = \"Test email from Python\"\nmsg[\"From\"] = \"your_email@example.com\"\nmsg[\"To\"] = \"friend@example.com\"\nmsg.set_content(\"Hello from Python!\")\n\ntry:\n    with smtplib.SMTP_SSL(\"smtp.example.com\", 465) as smtp:\n        smtp.login(\"your_email@example.com\", \"your_password\")\n        smtp.send_message(msg)\n    print(\"Email sent\")\nexcept Exception as error:\n    print(\"Could not send email:\")\n    print(error)\n",[52,1428,1429,1435,1449,1453,1463,1485,1507,1529,1547,1551,1557,1588,1615,1629,1644,1659,1674],{"__ignoreMap":50},[55,1430,1431,1433],{"class":57,"line":58},[55,1432,62],{"class":61},[55,1434,66],{"class":65},[55,1436,1437,1439,1441,1443,1445,1447],{"class":57,"line":69},[55,1438,72],{"class":61},[55,1440,75],{"class":65},[55,1442,79],{"class":78},[55,1444,82],{"class":65},[55,1446,62],{"class":61},[55,1448,87],{"class":65},[55,1450,1451],{"class":57,"line":90},[55,1452,94],{"emptyLinePlaceholder":93},[55,1454,1455,1457,1459,1461],{"class":57,"line":97},[55,1456,100],{"class":65},[55,1458,104],{"class":103},[55,1460,108],{"class":107},[55,1462,111],{"class":78},[55,1464,1465,1467,1469,1471,1473,1475,1477,1479,1481,1483],{"class":57,"line":114},[55,1466,117],{"class":65},[55,1468,120],{"class":78},[55,1470,1003],{"class":123},[55,1472,128],{"class":127},[55,1474,1003],{"class":123},[55,1476,133],{"class":78},[55,1478,136],{"class":103},[55,1480,1083],{"class":123},[55,1482,142],{"class":127},[55,1484,1088],{"class":123},[55,1486,1487,1489,1491,1493,1495,1497,1499,1501,1503,1505],{"class":57,"line":148},[55,1488,117],{"class":65},[55,1490,120],{"class":78},[55,1492,1003],{"class":123},[55,1494,157],{"class":127},[55,1496,1003],{"class":123},[55,1498,133],{"class":78},[55,1500,136],{"class":103},[55,1502,1083],{"class":123},[55,1504,168],{"class":127},[55,1506,1088],{"class":123},[55,1508,1509,1511,1513,1515,1517,1519,1521,1523,1525,1527],{"class":57,"line":173},[55,1510,117],{"class":65},[55,1512,120],{"class":78},[55,1514,1003],{"class":123},[55,1516,182],{"class":127},[55,1518,1003],{"class":123},[55,1520,133],{"class":78},[55,1522,136],{"class":103},[55,1524,1083],{"class":123},[55,1526,193],{"class":127},[55,1528,1088],{"class":123},[55,1530,1531,1533,1535,1537,1539,1541,1543,1545],{"class":57,"line":198},[55,1532,117],{"class":65},[55,1534,79],{"class":78},[55,1536,205],{"class":107},[55,1538,208],{"class":78},[55,1540,1003],{"class":123},[55,1542,213],{"class":127},[55,1544,1003],{"class":123},[55,1546,218],{"class":78},[55,1548,1549],{"class":57,"line":221},[55,1550,94],{"emptyLinePlaceholder":93},[55,1552,1553,1555],{"class":57,"line":226},[55,1554,1417],{"class":61},[55,1556,265],{"class":78},[55,1558,1559,1562,1564,1566,1568,1570,1572,1574,1576,1578,1580,1582,1584,1586],{"class":57,"line":268},[55,1560,1561],{"class":61},"    with",[55,1563,232],{"class":65},[55,1565,79],{"class":78},[55,1567,237],{"class":107},[55,1569,208],{"class":78},[55,1571,1003],{"class":123},[55,1573,244],{"class":127},[55,1575,1003],{"class":123},[55,1577,249],{"class":78},[55,1579,253],{"class":252},[55,1581,256],{"class":78},[55,1583,259],{"class":61},[55,1585,262],{"class":65},[55,1587,265],{"class":78},[55,1589,1590,1593,1595,1597,1599,1601,1603,1605,1607,1609,1611,1613],{"class":57,"line":298},[55,1591,1592],{"class":65},"        smtp",[55,1594,79],{"class":78},[55,1596,276],{"class":107},[55,1598,208],{"class":78},[55,1600,1003],{"class":123},[55,1602,168],{"class":127},[55,1604,1003],{"class":123},[55,1606,249],{"class":78},[55,1608,1083],{"class":123},[55,1610,291],{"class":127},[55,1612,1003],{"class":123},[55,1614,218],{"class":78},[55,1616,1617,1619,1621,1623,1625,1627],{"class":57,"line":314},[55,1618,1592],{"class":65},[55,1620,79],{"class":78},[55,1622,305],{"class":107},[55,1624,208],{"class":78},[55,1626,117],{"class":107},[55,1628,218],{"class":78},[55,1630,1631,1634,1636,1638,1640,1642],{"class":57,"line":319},[55,1632,1633],{"class":322},"    print",[55,1635,208],{"class":78},[55,1637,1003],{"class":123},[55,1639,330],{"class":127},[55,1641,1003],{"class":123},[55,1643,218],{"class":78},[55,1645,1646,1648,1652,1654,1657],{"class":57,"line":1181},[55,1647,1420],{"class":61},[55,1649,1651],{"class":1650},"sZMiF"," Exception",[55,1653,259],{"class":61},[55,1655,1656],{"class":65}," error",[55,1658,265],{"class":78},[55,1660,1661,1663,1665,1667,1670,1672],{"class":57,"line":1202},[55,1662,1633],{"class":322},[55,1664,208],{"class":78},[55,1666,1003],{"class":123},[55,1668,1669],{"class":127},"Could not send email:",[55,1671,1003],{"class":123},[55,1673,218],{"class":78},[55,1675,1676,1678,1680,1683],{"class":57,"line":1217},[55,1677,1633],{"class":322},[55,1679,208],{"class":78},[55,1681,1682],{"class":107},"error",[55,1684,218],{"class":78},[14,1686,1687],{},"This does not fix every problem automatically, but it helps you see the real error instead of the program stopping with no explanation.",[40,1689,1691],{"id":1690},"faq","FAQ",[721,1693,1695],{"id":1694},"do-i-need-to-install-a-package-to-send-email-in-python","Do I need to install a package to send email in Python?",[14,1697,1698,1699,1305,1701,1704],{},"No. For a basic SMTP example, ",[52,1700,368],{},[52,1702,1703],{},"email.message"," are part of Python's standard library.",[721,1706,1708],{"id":1707},"why-does-my-email-script-say-login-failed","Why does my email script say login failed?",[14,1710,1711],{},"Usually the username, password, app password, or provider security settings are wrong.",[721,1713,1715],{"id":1714},"can-i-send-html-emails-with-python","Can I send HTML emails with Python?",[14,1717,1718,1719,1721],{},"Yes. You can use ",[52,1720,374],{}," and add HTML content. For a first script, plain text is easier to test.",[721,1723,1725],{"id":1724},"why-is-my-script-correct-but-the-email-still-does-not-arrive","Why is my script correct but the email still does not arrive?",[14,1727,1728],{},"The provider may reject, delay, or filter the message. Check the spam folder and your provider settings.",[40,1730,1732],{"id":1731},"see-also","See also",[21,1734,1735,1740,1744,1748,1754],{},[24,1736,1737],{},[711,1738,1739],{"href":1308},"How to handle exceptions in Python",[24,1741,1742],{},[711,1743,1304],{"href":1303},[24,1745,1746],{},[711,1747,714],{"href":713},[24,1749,1750],{},[711,1751,1753],{"href":1752},"\u002Fstandard-library\u002Fpython-sys-module-overview\u002F","Python sys module overview",[24,1755,1756],{},[711,1757,1759],{"href":1758},"\u002Fhow-to\u002Fhow-to-make-an-api-request-in-python\u002F","How to make an API request in Python",[1761,1762,1763],"style",{},"html pre.shiki code .sVHd0, html code.shiki .sVHd0{--shiki-light:#39ADB5;--shiki-light-font-style:italic;--shiki-default:#D73A49;--shiki-default-font-style:inherit;--shiki-dark:#F97583;--shiki-dark-font-style:inherit}html pre.shiki code .su5hD, html code.shiki .su5hD{--shiki-light:#90A4AE;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .sP7_E, html code.shiki .sP7_E{--shiki-light:#39ADB5;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .smGrS, html code.shiki .smGrS{--shiki-light:#39ADB5;--shiki-default:#D73A49;--shiki-dark:#F97583}html pre.shiki code .slqww, html code.shiki .slqww{--shiki-light:#6182B8;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .sjJ54, html code.shiki .sjJ54{--shiki-light:#39ADB5;--shiki-default:#032F62;--shiki-dark:#9ECBFF}html pre.shiki code .s_sjI, html code.shiki .s_sjI{--shiki-light:#91B859;--shiki-default:#032F62;--shiki-dark:#9ECBFF}html pre.shiki code .srdBf, html code.shiki .srdBf{--shiki-light:#F76D47;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .sptTA, html code.shiki .sptTA{--shiki-light:#6182B8;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html .light .shiki span {color: var(--shiki-light);background: var(--shiki-light-bg);font-style: var(--shiki-light-font-style);font-weight: var(--shiki-light-font-weight);text-decoration: var(--shiki-light-text-decoration);}html.light .shiki span {color: var(--shiki-light);background: var(--shiki-light-bg);font-style: var(--shiki-light-font-style);font-weight: var(--shiki-light-font-weight);text-decoration: var(--shiki-light-text-decoration);}html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html.dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html pre.shiki code .sbgvK, html code.shiki .sbgvK{--shiki-light:#E2931D;--shiki-default:#6F42C1;--shiki-dark:#B392F0}html pre.shiki code .stzsN, html code.shiki .stzsN{--shiki-light:#91B859;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .sZMiF, html code.shiki .sZMiF{--shiki-light:#E2931D;--shiki-default:#005CC5;--shiki-dark:#79B8FF}",{"title":50,"searchDepth":69,"depth":69,"links":1765},[1766,1767,1768,1769,1770,1777,1778,1779,1780,1781,1782,1788],{"id":42,"depth":69,"text":43},{"id":340,"depth":69,"text":341},{"id":381,"depth":69,"text":382},{"id":427,"depth":69,"text":428},{"id":718,"depth":69,"text":719,"children":1771},[1772,1773,1774,1775,1776],{"id":723,"depth":90,"text":673},{"id":758,"depth":90,"text":677},{"id":796,"depth":90,"text":681},{"id":831,"depth":90,"text":699},{"id":888,"depth":90,"text":705},{"id":918,"depth":69,"text":919},{"id":952,"depth":69,"text":953},{"id":1247,"depth":69,"text":1248},{"id":1312,"depth":69,"text":1313},{"id":1393,"depth":69,"text":1394},{"id":1690,"depth":69,"text":1691,"children":1783},[1784,1785,1786,1787],{"id":1694,"depth":90,"text":1695},{"id":1707,"depth":90,"text":1708},{"id":1714,"depth":90,"text":1715},{"id":1724,"depth":90,"text":1725},{"id":1731,"depth":69,"text":1732},"Master python email sender script example in our comprehensive Python beginner guide.","md",{},"\u002Fexamples\u002Fpython-email-sender-script-example",{"title":5,"description":1789},"examples\u002Fpython-email-sender-script-example","yPu0kW52TJkUKhbdrYqZL0O6TTN1MbrQs15ZuJBcokM",1777585475059]