Hello guest, if you read this it means you are not registered. Click here to register in a few simple steps, you will enjoy all features of our Forum.
Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
error: unrecognized arguments
#1
hi folks i am new to python and getting below error when i call my python function thru shell scripting. for some reason it doesnt likes the s_body="test kajdsfkj"


Error:
------------------------ error unrecognized arguments: kajdsfkj usage: pemail.py [-h] [-f FRM] [-t TO] [-s SUBJECT] [-b BODY] [-a ATTACHMENT] pemail.py: error: [b]unrecognized arguments: kajdsfkj[/b]
------------------------- saved below code as pmail.sh

#!/bin/sh

s_email='xxx@xxx.com'
s_body="test kajdsfkj"
s_attachment='/tmp/test3.txt'
python pemail.py -t $s_email -s $s_subject -b $s_body -a $s_attachment > /home/xxx/mytest/logs/email.$(date +"%Y%m%d%H%M%S").log
------------------------- saved below python code to pmail.py


#!/usr/bin/python

import boto3
import sys
import csv
import os
#import optparse
import argparse
import re
import email.utils

from email.mime.text import MIMEText
from email.mime.application import MIMEApplication
from email.mime.multipart import MIMEMultipart
from email.Utils import parseaddr, formataddr
from email.utils import getaddresses


def pemail(args):
   print args.to
   print args.subject
   print args.body
   print args.attachment

   xfilename = os.path.basename(args.attachment)

   #recipients = [ args.to ]
   recipients = args.to.split(',') 
   #strbody = args.body.split(',') 


   msg=MIMEMultipart()
   msg['Subject'] = args.subject 
   #msg['Subject'] = my_string
   msg['From'] = args.frm
   msg['To'] = ",".join(recipients)
   
   msg.preable = 'Multipart message.\n'
   
   emailbody = args.body

   endline ="\n NOTE: this is an automated e-mail, please do not reply to this address as it is not monitored."
   
   xdata = unicode( emailbody + "\n" + endline )
   part = MIMEText(xdata)
   msg.attach(part)
      
   part = MIMEApplication(open(args.attachment, 'r').read(), Name=xfilename)
   part.add_header('Content-Disposition', 'attachment; filename='+xfilename)
   msg.attach(part)
   
   ses = boto3.client('ses')
   
   response = ses.send_raw_email(
           Source = msg['From'],
           Destinations= recipients ,
           RawMessage={
               'Data': msg.as_string()
           }
       )
   print response
   if response['ResponseMetadata']['HTTPStatusCode'] == 200:
      print 'Success'
   else:
      print 'Email failed.'

if __name__ == '__main__':
    ## Setup and parse command line arguments
    parser = argparse.ArgumentParser(prog='pemail.py', description='Sends an email with an attachment.')
    parser.add_argument('-f', '--frm', default='no_reply@precima.com', help='The sender address.')
    parser.add_argument('-t', '--to', help='The recipient address.')
    parser.add_argument('-s', '--subject', help='The subject of the email.')
    parser.add_argument('-b', '--body', help='The body of the email.')
    ##parser.add_argument('-b', '--body', type=str, required=True)
    parser.add_argument('-a', '--attachment', help='The path to the attachment.')

    args=parser.parse_args()

    pemail(args)
Quote
#2
What do you pass as arguments to the command? And according to the error, we don't need all the script. Just the argparse part.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Quote
#3
if __name__ == '__main__':
   ## Setup and parse command line arguments
   parser = argparse.ArgumentParser(prog='pemail.py', description='Sends an email with an attachment.')
   parser.add_argument('-f', '--frm', default='no_reply@xxxxxxx.com', help='The sender address.')
   parser.add_argument('-t', '--to', help='The recipient address.')
   parser.add_argument('-s', '--subject', help='The subject of the email.')
   parser.add_argument('-b', '--body', help='The body of the email.')
   ##parser.add_argument('-b', '--body', type=str, required=True)
   parser.add_argument('-a', '--attachment', help='The path to the attachment.')

   args=parser.parse_args()

   pemail(args)

hi Wavic
I am passing following command to the function

there is some issue passing parameter s_body="test kajdsfkj"
------------------

#!/bin/sh

s_email='xxx@xxx.com'
s_body="test kajdsfkj"
s_attachment='/tmp/test3.txt'
python pemail.py -t $s_email -s $s_subject -b $s_body -a $s_attachment > /home/xxx/mytest/logs/email.$(date +"%Y%m%d%H%M%S").log
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Trouble passing arguments underoathed 2 166 Nov-13-2017, 04:20 PM
Last Post: underoathed
  python with arguments haye 1 225 Oct-10-2017, 11:42 AM
Last Post: gruntfutuk
  Supplying 2 arguments to 3-argument function valtih 3 386 Sep-10-2017, 11:10 PM
Last Post: nilamo
  # of Positional arguments to pass for creating an object? burvil 2 209 Sep-09-2017, 06:43 PM
Last Post: burvil
  How to use positional arguments RedSkeleton007 1 257 Aug-29-2017, 01:49 AM
Last Post: ichabod801
  empty arguments noob 16 1,568 May-11-2017, 10:37 PM
Last Post: micseydel
  Functions (Arguments Passing,Changing a mutable ,Assignment to Arguments Names) Adelton 2 580 Mar-02-2017, 10:23 PM
Last Post: zivoni
  documentation of arguments Skaperen 6 1,187 Dec-02-2016, 07:56 AM
Last Post: Skaperen
  bksp() requires no arguments (one given) gdog1102 3 872 Oct-28-2016, 04:50 PM
Last Post: gdog1102
  multiprocess passing multiple arguments double asterisk pic8690 1 832 Oct-23-2016, 08:51 AM
Last Post: Skaperen

Forum Jump:


Users browsing this thread: 1 Guest(s)