import random
import smtplib
from email.mime.text import MIMEText
# 1. Fungsi buat kode acak
def generate_verification_code(length=6):
return ''.join(str(random.randint(0, 9)) for _ in range(length))
# 2. Buat kode
code = generate_verification_code()
# 3. Email tujuan dan isi
recipient_email = "[email protected]"
subject = "Kode Verifikasi Anda"
body = f"Kode verifikasi Anda adalah: {code}"
# 4. Buat email
msg = MIMEText(body)
msg['Subject'] = subject
msg['From'] = "[email protected]" # Ganti dengan email kamu
msg['To'] = recipient_email
# 5. Kirim via SMTP Gmail
try:
smtp_server = smtplib.SMTP_SSL("smtp.gmail.com", 465)
smtp_server.login("[email protected]", "password_aplikasi_anda") # Gunakan password aplikasi Gmail
smtp_server.send_message(msg)
smtp_server.quit()
print(f"Kode berhasil dikirim ke {recipient_email}")
except Exception as e:
print("Gagal mengirim email:", e)