{"slug": "python-script-to-send-mail-via-apple-s-icloud-be-sure-to-setup-an-app-specific", "title": "Python script to send mail via Apple's iCloud.  Be sure to setup an app specific password for and do not use or expose your iCloud password.  https://support.apple.com/en-us/HT204397", "summary": "This article provides a Python script that uses the `smtplib` library to send emails through Apple's iCloud SMTP server (`smtp.mail.me.com` on port 587). The script establishes a TLS-encrypted connection, logs in using an iCloud ID and an app-specific password (not the user's main iCloud password), and sends a MIME-formatted email with a subject and body. It emphasizes the importance of creating an app-specific password for security and avoiding exposure of the primary iCloud password.", "body_md": "smtp_icloud.py\n\n      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.\n      \nLearn more about bidirectional Unicode characters\n\n \n    Show hidden characters\n\nimport smtplib\n\n#email.mime.multipart is specific to python3\n\nfrom email.mime.multipart import MIMEMultipart\n\nfrom email.mime.text import MIMEText\n\nmsg = MIMEMultipart()\n\nmsg['From'] = 'sendfrom@mail.com'\n\nmsg['To'] = 'sendto@mail.com'\n\nmsg['Subject'] = 'Subject'\n\nmessage = 'Message body'\n\nmsg.attach(MIMEText(message))\n\nmailserver = smtplib.SMTP('smtp.mail.me.com', 587)\n\n# identify ourselves\n\nmailserver.ehlo()\n\n# secure our email with tls encryption\n\nmailserver.starttls()\n\n# re-identify ourselves as an encrypted connection\n\nmailserver.ehlo()\n\nmailserver.login('iCloud ID', 'app-specific password')\n\nmailserver.sendmail('sendfrom@mail.com',\n\n                    'sendto@mail.com', msg.as_string())\n\nmailserver.quit()", "url": "https://wpnews.pro/news/python-script-to-send-mail-via-apple-s-icloud-be-sure-to-setup-an-app-specific", "canonical_source": "https://gist.github.com/chrisswanda/57fbc7c587f76d8437880657c0893009", "published_at": "2020-10-29 11:06:35+00:00", "updated_at": "2026-05-22 23:06:33.060517+00:00", "lang": "en", "topics": ["developer-tools"], "entities": ["Apple", "iCloud"], "alternates": {"html": "https://wpnews.pro/news/python-script-to-send-mail-via-apple-s-icloud-be-sure-to-setup-an-app-specific", "markdown": "https://wpnews.pro/news/python-script-to-send-mail-via-apple-s-icloud-be-sure-to-setup-an-app-specific.md", "text": "https://wpnews.pro/news/python-script-to-send-mail-via-apple-s-icloud-be-sure-to-setup-an-app-specific.txt", "jsonld": "https://wpnews.pro/news/python-script-to-send-mail-via-apple-s-icloud-be-sure-to-setup-an-app-specific.jsonld"}}