Forum Discussion

ankita_sharma's avatar
ankita_sharma
Occasional Contributor
12 years ago

Send Email fucntion do not send emails





Hi All,



I am not able to send emails using the script below, Where as my log shows as mail has been sent.



Please look into this piece of code and let me know why it doesnot work.







Sub Main



'call Test

Call SendEmail("ankita_shama@xyz.com", "ritu_Pur@xyz.com", "Test", "TestMessage using script", "")



End Sub



 



Function SendEmail(mFrom, mTo, mSubject, mBody, mAttachment)



  Dim smtpServer, smtpPort, userLogin, userPassword



  Dim authenticationType, useSSL, connectionTimeout



  Dim schema, mConfig, mMessage



 



  smtpServer = "mailwestcoast.corp.nai.cum.org"



  smtpPort = 25



  userLogin = "ankita_shama@xyz.com" ' e.g. "abc" if the address is abc@gmail.com



  userPassword = "pwrd"



  authenticationType = 1 ' cdoBasic



  connectionTimeout = 30



  ' Required by Gmail



  useSSL = True



 



  Err.Clear()



  On Error Resume Next



 



  schema = "http://schemas.microsoft.com/cdo/configuration/"



  Set mConfig = Sys.OleObject("CDO.Configuration")



  mConfig.Fields.Item(schema & "sendusing") = 2 ' cdoSendUsingPort



  mConfig.Fields.Item(schema & "smtpserver") = smtpServer



  mConfig.Fields.Item(schema & "smtpserverport") = smtpPort



  mConfig.Fields.Item(schema & "sendusername") = userLogin



  mConfig.Fields.Item(schema & "sendpassword") = userPassword



  mConfig.Fields.Item(schema & "smtpauthenticate") = authenticationType



  mConfig.Fields.Item(schema & "smtpusessl") = useSSL



  mConfig.Fields.Item(schema & "smtpconnectiontimeout") = connectionTimeout



  mConfig.Fields.Update()



 



  Set mMessage = Sys.OleObject("CDO.Message")



  Set mMessage.Configuration = mConfig



   mMessage.From = mFrom



   mMessage.To = mTo



   mMessage.Subject = mSubject



   mMessage.HTMLBody = mBody



 



  If 0 < Len(mAttachment) Then



    mMessage.AddAttachment(mAttachment)



  End If



 



  mMessage.Send()



 



  If Err.Number > 0 Then



    Call Log.Error("E-mail cannot be sent", Err.Description)



    SendEMail = False



    Err.Clear()



  Else



    Log.Message("Message to <" & mTo & "> was successfully sent")



    SendEMail = True



  End If



End Function



  • hantrung_truong's avatar
    hantrung_truong
    Occasional Contributor
    Hi Ankita, please try the following code:




    from email.Encoders import encode_base64


    from email.MIMEAudio import MIMEAudio


    from email.MIMEBase import MIMEBase


    from email.MIMEImage import MIMEImage


    from email.MIMEMultipart import MIMEMultipart


    from email.MIMEText import MIMEText


    import email.message




    # Implementation of sendGMail function


    def sendGMail(gmail, password, recipient, subject, text, *attachmentFilePaths):


        gmailUser = gmail


        gmailPassword = password


        msg = MIMEMultipart()


        msg['From'] = gmailUser


        msg['To'] = recipient


        msg['Subject'] = subject


        msg.attach(MIMEText(text))


        for attachmentFilePath in attachmentFilePaths:


            msg.attach(getAttachment(attachmentFilePath))


        mailServer = smtplib.SMTP('smtp.gmail.com', 587)


        mailServer.ehlo()


        mailServer.starttls()


        mailServer.ehlo()


        mailServer.login(gmailUser, gmailPassword)


        mailServer.sendmail(gmailUser, msg['To'], msg.as_string())


        mailServer.close()


        print('Sent email to %s' % recipient)


     


     


    # Implementation of getAttachment function


    def getAttachment(attachmentFilePath):


        contentType, encoding = mimetypes.guess_type(attachmentFilePath)


        if contentType is None or encoding is not None:


            contentType = 'application/octet-stream'


        mainType, subType = contentType.split('/', 1)


        file = open(attachmentFilePath, 'rb')


        if mainType == 'text':


            attachment = MIMEText(file.read())


        elif mainType == 'message':


            attachment = email.message_from_file(file)


        elif mainType == 'image':


            attachment = MIMEImage(file.read(),_subType=subType)


        elif mainType == 'audio':


            attachment = MIMEAudio(file.read(),_subType=subType)


        else:


            attachment = MIMEBase(mainType, subType)


        attachment.set_payload(file.read())


        encode_base64(attachment)


        file.close()


        attachment.add_header('Content-Disposition', 'attachment',   filename=os.path.basename(attachmentFilePath))


        


        return attachment




  • ankita_sharma's avatar
    ankita_sharma
    Occasional Contributor


    Thanks for the Reply Trung.



    But i was looking for sending an email from Outlook and not from gmail. Can you please confirm if the following code will send email from Outlook

  • hantrung_truong's avatar
    hantrung_truong
    Occasional Contributor
    Hi Ankita, I haven't tried on Outlook. But if your are using SMTP email, you will be able to use it with appropriate parameters.
  • ankita_sharma's avatar
    ankita_sharma
    Occasional Contributor
    Thanks Trung for Reply.



    I was trying to execute your code and i See that int he first command it gives a compilation error. I tried placing import email.message command in the first line, but still the issue continues.



    Any idea on it