استاد عزیز لطفا به من در این قطعه کد کمک کنید
میخواهم لاگین شم به جمیل اما انجام نمیشه
smtp رو روی پورت 465 و 587 چک کردم نشد و حتی از smtp.SMTP() خالی هم استفاده کردم نشد
import smtplib
gmail_user = 'ag0627120@gmail.com'
gmail_password = '13755731'
try:
server = smtplib.SMTP_SSL('smtp.gmail.com', 465)
server.ehlo()
server.login(gmail_user, gmail_password)
print('sent successfully')
except:
print('Something went wrong...')با سلام.
نمونه کد زیر برای خواندن ایمیل ها از gmail:
# Python 3.8.0
import smtplib
import time
import imaplib
import email
import traceback
# -------------------------------------------------
#
# Utility to read email from Gmail Using Python
#
# ------------------------------------------------
ORG_EMAIL = "@gmail.com"
FROM_EMAIL = "your_email" + ORG_EMAIL
FROM_PWD = "your-password"
SMTP_SERVER = "imap.gmail.com"
SMTP_PORT = 993
def read_email_from_gmail():
try:
mail = imaplib.IMAP4_SSL(SMTP_SERVER)
mail.login(FROM_EMAIL,FROM_PWD)
mail.select('inbox')
data = mail.search(None, 'ALL')
mail_ids = data[1]
id_list = mail_ids[0].split()
first_email_id = int(id_list[0])
latest_email_id = int(id_list[-1])
for i in range(latest_email_id,first_email_id, -1):
data = mail.fetch(str(i), '(RFC822)' )
for response_part in data:
arr = response_part[0]
if isinstance(arr, tuple):
msg = email.message_from_string(str(arr[1],'utf-8'))
email_subject = msg['subject']
email_from = msg['from']
print('From : ' + email_from + '\n')
print('Subject : ' + email_subject + '\n')
except Exception as e:
traceback.print_exc()
print(str(e))
read_email_from_gmail()
و برای ارسال ایمیل توسط gmail خود:
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
mail_content = '''Hello,
This is a simple mail. There is only text, no attachments are there The mail is sent using Python SMTP library.
Thank You
' ' '
#The mail addresses and password
sender_address = 'sender@gmail.com'
sender_pass = 'xxxxxxxx'
receiver_address = 'receiver@gmail.com'
#Setup the MIME
message = MIMEMultipart()
message['From'] = sender_address
message['To'] = receiver_address
message['Subject'] = 'Your email subject'
session = smtplib.SMTP('smtp.gmail.com', 587) #use gmail with port
session.starttls() #enable security
session.login(sender_address, sender_pass) #login with mail_id and password
text = message.as_string()
session.sendmail(sender_address, receiver_address, text)
session.quit()
print('Mail Sent')
توجه کنید که برای ارسال ایمیل باید Less Secure App Access در gmail شما فعال باشد (در صورتی که two step verification فعال باشد این گزینه را نمی بینید)
اطلاعات بیشتر را اینجا و اینجا ببینید.
در ضمن id , pass خود را از کدهای ارسالی حذف کنید.
موفق باشید.
سلام و ممنون از شما استاد گرامی
من میخوام بدون این دسترسی Less Secure App Access کارمو انجام بدم چون میخوام چندین ایمیل رو لاگین شم
حتی با کتابخانه سلنیوم هم امتحان کردم اما گوگل میگه از مرورگر تستی ای که سلنیوم میدهد نمیشه استفاده کرد برای لاگین و جلوی صفحه پسورد رو میگیره
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
import time
print('Enter the gmailid and password')
gmailId, passWord = map(str, input().split())
driver = webdriver.Chrome("ChromeDriver")
driver.get(r'https://accounts.google.com/signin/v2/identifier?continue='+\
'https%3A%2F%2Fmail.google.com%2Fmail%2F&service=mail&sacu=1&rip=1'+\
'&flowName=GlifWebSignIn&flowEntry = ServiceLogin')
driver.implicitly_wait(15)
loginBox = driver.find_element_by_xpath('//*[@id ="identifierId"]')
loginBox.send_keys(gmailId)
nextButton = driver.find_elements_by_xpath('//*[@id ="identifierNext"]')
nextButton[0].click()
time.sleep(3)
passWordBox = driver.find_element_by_xpath(
'//*[@id ="password"]/div[1]/div / div[1]/input')
passWordBox.send_keys(passWord)
nextButton = driver.find_elements_by_xpath('//*[@id ="passwordNext"]')
nextButton[0].click()مشکلم حل شد با اضاف کردن دو خط کد زیر به کانفیگ های سلنیوم :
options.add_argument('--disable-web-security')
options.add_argument('--allow-running-insecure-content')خوشحالم که مشکل حل شد.
در ضمن، من استاد نیستم (: