I am trying to send email from my app, but is not working

What I’m trying to do:
I am trying to send email from my app, but is not working., I dont get any errors, but never get the emails send. Even tought I tried the Test setting and commes back green

What I’ve tried and what’s not working:
Tried without images, minimun text, different emails.

Code Sample:

@anvil.server.callable
def send_images_to_process(doctor_email, images):
  try:
    # Load doctor information
    doctor_info = load_doctor_by_email(doctor_email)
    if not doctor_info:
        return False

    # Create a zip file with the images
    zip_buffer = BytesIO()
    with zipfile.ZipFile(zip_buffer, "w") as zip_file:
        for image in images:
            # Write each image to the zip file
            with anvil.media.TempFile(image) as temp_file:
                zip_file.write(temp_file, arcname=image.name)

    # Reset buffer position
    zip_buffer.seek(0)
    
    # Create a media object for the zip file
    zip_media = anvil.BlobMedia("application/zip", zip_buffer.read(), name="images.zip")

    # Send email with doctor information and the zip file
    email_body = f"""
    Dear Dr. {doctor_info['first_name']} {doctor_info['last_name']},

    Please find the images attached for your review.

    Specialty: {doctor_info['specialty']}
    Phone Number: {doctor_info['phone_number']}

    """
    anvil.email.send(
        from_name = "My App Support", 
        to='****@sangre-viva.com',  # Use doctor_info["email"] for dynamic email
        subject="Images for Review",
        text=email_body,
        attachments=[zip_media]
    )
    print("Email sent successfully.")
    return True
  except Exception as e:
    # Log the exception for debugging purposes
    print(f"Error sending email: {e}")
    return False
      

–Custom SMTP Settings–
Host: mail.sangre-viva.com
Port: 465
Encryption: SSL
Username: ****@sangre-viva.com
Password: *****

Test settings: Connection succeeded


Thanks!

Are you using DKIM on your custom email server? That needs set up specifically via Anvil support, otherwise your emails sent from Anvil will not be verified through DKIM and will be silently discarded at the receiving end.

1 Like

Hi @jshaffstall, thanks for the replay, can you tell me more about DKIM and how to integrate it with anvil?

Thanks again.

If you have your email server set up to use DKIM, you’ll need to work through the process of getting DKIM enabled on your Anvil email. When I did it, that happened through Anvil support. They’ll ask you for some information, then send you keys to put into your DNS records.

2 Likes

Thanks! I have send them a message but no response… we will wait and see.