passing string to shell using runscript

Asked by Ben Dix on 2021-05-21

Hey 😄
I am trying to send a mail using this code:

def start_user():
    print('sending mail to user')
    mail_sending = """
    powershell
    $EmailFrom = "<email address hidden>"
    $EmailTo = "<email address hidden>"
    $Subject ="Try done"
    $Body ="Content of body"
    $SMTPServer ="smtp.server"
    $SMTPClient = New-Object Net.Mail.SmtpClient($SMTPServer, 587)
    $SMTPClient.EnableSsl = $true
    $SMTPClient.Credentials = New-Object System.Net.NetworkCredential("<email address hidden>","password");
    $SMTPClient.Send($EmailFrom, $EmailTo, $Subject, $Body)
    """
    returnCode = runScript(mail_sending)

it is working fine, using the right content.
But even after hours of research I can't find a way to insert the fields using variables of my script:

MyMail = <email address hidden>
email = <email address hidden>
and so on for

def start_user():
    print('sending mail to user')
    mail_sending = """
    powershell
    $EmailFrom = str(MyMail)
    $EmailTo = str(email)
    ...

anyone can help me?

thx

Question information

Language:
English Edit question
Status:
Answered
For:
SikuliX Edit question
Assignee:
No assignee Edit question
Last query:
Last reply:
Revision history for this message
Manfred Hampl (m-hampl) said :
#1

What you need has nothing to do with Sikulix, but is plain Python syntax.
You need string concatenation to construct the value for "mail_sending".

Something like this:

def start_user():
    print('sending mail to user')
    mail_sending = 'powershell \n'
    mail_sending = mail_sending + '$EmailFrom = "' + MyMail + '\n'
    mail_sending = mail_sending + '$EmailTo = "' + email + '\n'
etc.

Another possibility is substitution with %s

see e.g. https://www.pythonforbeginners.com/concatenation/string-concatenation-and-formatting-in-python

Revision history for this message
RaiMan (raimund-hocke) said :
#2

using the string format option mentioned by Manfred

def start_user(from, to):
    print('sending mail to user')
    mail_sending = """
    powershell
    $EmailFrom = %s
    $EmailTo = %s
    $Subject ="Try done"
    $Body ="Content of body"
    $SMTPServer ="smtp.server"
    $SMTPClient = New-Object Net.Mail.SmtpClient($SMTPServer, 587)
    $SMTPClient.EnableSsl = $true
    $SMTPClient.Credentials = New-Object System.Net.NetworkCredential("<email address hidden>","password");
    $SMTPClient.Send($EmailFrom, $EmailTo, $Subject, $Body)
    """
    returnCode = runScript(mail_sending % (from, to))

start_user("<email address hidden>", "<email address hidden>")

Revision history for this message
Ben Dix (bendix80) said :
#3

lovley 😄 thx Manfred and RainMan!
I am really thankful for your support!

I'll try as soon as possible!

greetings!

Can you help with this problem?

Provide an answer of your own, or ask Ben Dix for more information if necessary.

To post a message you must log in.