Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8242129
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T21:04:00+00:00 2026-06-07T21:04:00+00:00

I developed an email system for my client by using email.mime library. I works

  • 0

I developed an email system for my client by using email.mime library. I works very well for some mail clients. For example on gmail I can see html body and attachment file but on yahoo I get an empty email. Here is the code:

import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.base import MIMEBase
import email.charset

from email import Encoders
def buildEmail(from_email, to_email, subject, text_msg, html_msg, attach_pdf_file):
    """
    This function build a body for text/html message and if attachment file provide
    then it will attach it too with email.
    """

    # Create message container - the correct MIME type is multipart/alternative.

    msgRoot = MIMEMultipart('alternative')
    msgRoot['Subject'] = subject

    msgRoot.preamble = 'This is a multi-part message in MIME format.'
    msgRoot.epilogue = ''


    # Record the MIME types of both parts - text/plain and text/html.
    text_part = MIMEText(text_msg, 'plain')
    msgRoot.attach(text_part)

    if html_msg is not None and html_msg != '':
        html_part = MIMEText(html_msg, 'html')
        msgRoot.attach(html_part)


    # Attach a file if provided
    if attach_file is not None and attach_file != '':
        fp = open(attach_pdf_file, 'rb')
        part = MIMEBase('application', 'pdf')
        part.set_payload( fp.read() )
        Encoders.encode_base64(part)
        part.add_header('Content-Disposition', 'attachment', filename="%s"%          os.path.basename(attach_pdf_file))
        msgRoot.attach(part)
        fp.close()


    s = smtplib.SMTP('localhost')
    s.sendmail(from_email, to_email, msgRoot.as_string())
    s.quit()

Any idea where am I making a mistake?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T21:04:01+00:00Added an answer on June 7, 2026 at 9:04 pm

    I see several problems with this code that should prevent it from working at all on Python 2.x.

    1) You don’t define attach_file anywhere, so this line raises an exception:

    if attach_file is not None and attach_file != '':
    

    Did you mean attach_pdf_file? Changing attach_file to attach_pdf_file corrects that exception.

    2) Maybe you just left it out of your code sample, but you call a function from os.path, but you never import os anywhere in your script. Adding that corrects that exception.

    Once those problems are corrected, running the script on my system with the from address as a gmail address raises this exception:

    Traceback (most recent call last):
      File "/home/ra/Desktop/stack-overflow.py", line 50, in <module>
        "output.pdf")
      File "/home/ra/Desktop/stack-overflow.py", line 44, in buildEmail
        s = smtplib.SMTP('localhost')
      File "/usr/lib/python2.7/smtplib.py", line 249, in __init__
        (code, msg) = self.connect(host, port)
      File "/usr/lib/python2.7/smtplib.py", line 309, in connect
        self.sock = self._get_socket(host, port, self.timeout)
      File "/usr/lib/python2.7/smtplib.py", line 284, in _get_socket
        return socket.create_connection((port, host), timeout)
      File "/usr/lib/python2.7/socket.py", line 571, in create_connection
        raise err
    error: [Errno 111] Connection refused
    

    I don’t have an SMTP server configured locally on this machine, which explains why I get the error I do. In your case, if the email is sending properly, but only for some email providers, it could be because an email provider realises that even though the address is Yahoo, the SMTP server isn’t and thus refuses the email. It’s just a hunch, but it might explain it (if this weren’t the case, it would be even easier for anyone to send an email “from” any address).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have developed push notification system in android using c2dm, It's working fine but
I've developed an automated crash reporting system that sends in realtime (via email) any
For my Rails app, we developed an half home-brewed email system. We created a
I had developed an application for sending email. On click of email button it
I developed my site and it works like a charm on my local machine.
I developed a site using twitter bootstrap and it seems that the responsive layout
I developed a module to display submenus as stand alone module, every thing works
I have built a webservice into my companies self developed CRM system that we
I have developed a very simple Android app where user has to select items
VC++ 2008, CLR Console App, being developed under win7 x64. I'm using .net with

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.