Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import smtplib
- from email.mime.multipart import MIMEMultipart
- from email.mime.text import MIMEText
- from email.utils import formataddr
- import pandas as pd
- import time
- # Email configuration
- gmail_user = 'put_your_email_here' # Replace with your Gmail address
- gmail_password = 'put_you_password_here' # Replace with your Gmail password
- full_name = "put_your_name_here" # Your full name
- # CSV file with pharmacy data
- file_path = 'pharmacy_contacts.csv' # Update this path if necessary. For testing, create a new one with a single record. You can even include your email address here.
- # Load pharmacy data from CSV
- pharmacy_data = pd.read_csv(file_path,header=None,names=['Name', 'Email'])
- print (pharmacy_data)
- # Message template
- subject = "Demande de médicaments"
- body_html_template = """
- <div dir="ltr">
- Bonjour {pharmacy_name},<br><br>
- Mon docteur m'a prescrit une série de médicaments. Malheureusement, tous ne sont pas disponibles dans ma pharmacie, donc je cherche les articles suivants :<br><br>
- - Freestyle Libre 2 - 12 pièces (pour trois mois pour moi et ma fille)<br>
- - Fiasp (au moins 2 boîtes avec 5 stylos)<br>
- - Ozempic - 1 mg (1 boîtes).<br><br>
- Merci de me dire si vous les avez en stock.<br><br>
- Cordialement,<br><br><br>
- --<br>
- <b>Your Name</b><br>
- </div>
- """
- def send_email(pharmacy_name, recipient_email):
- """Send an email to the specified pharmacy."""
- try:
- # Create message
- msg = MIMEMultipart('alternative')
- msg['From'] = formataddr((full_name, gmail_user))
- msg['To'] = recipient_email
- msg['Subject'] = pharmacy_name+': '+subject
- # Customize the HTML body with the pharmacy name
- body_html = body_html_template.format(pharmacy_name=pharmacy_name)
- # Attach both plain text and HTML parts
- msg.attach(MIMEText("Bonjour " + pharmacy_name + ",\n\n" +
- "Mon docteur m'a prescrit une série de médicaments. Malheureusement, tous ne sont pas disponibles dans ma pharmacie, donc je cherche les articles suivants:\n\n" +
- "- Freestyle Libre 2 - 12 pièces (pour trois mois pour moi et ma fille)\n" +
- "- Fiasp (au moins 3 boîtes avec 5 stylos)\n" +
- "- Ozempic - 1 mg (1 boîtes).\n\n" +
- "Merci de me dire si vous les avez en stock.\n\n" +
- "Cordialement,\n\n\nYour_Name\n", 'plain'))
- msg.attach(MIMEText(body_html, 'html'))
- # Connect to Gmail's SMTP server
- server = smtplib.SMTP('smtp.gmail.com', 587)
- server.starttls() # Start TLS for security
- server.login(gmail_user, gmail_password)
- # Send email
- server.sendmail(gmail_user, recipient_email, msg.as_string())
- server.quit()
- print(f"Email sent to {pharmacy_name} at {recipient_email}")
- except Exception as e:
- print(f"Failed to send email to {pharmacy_name} at {recipient_email}: {e}")
- def main():
- # Loop through each pharmacy and send an email
- for index, row in pharmacy_data.iterrows():
- pharmacy_name = row['Name']
- recipient_email = row['Email']
- if pd.notna(recipient_email): # Check if email is not NaN
- send_email(pharmacy_name, recipient_email)
- print("Waiting for 30 seconds before sending the next email...")
- time.sleep(30) # Wait 30 seconds between emails
- if __name__ == "__main__":
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement