Email: helpdesk@telkomuniversity.ac.id

This Portal for internal use only!

  • My Download
  • Checkout
Application Package Repository Telkom University
All Categories

All Categories

  • Visual Paradigm
  • IBM
  • Adobe
  • Google
  • Matlab
  • Microsoft
    • Microsoft Apps
    • Analytics
    • AI + Machine Learning
    • Compute
    • Database
    • Developer Tools
    • Internet Of Things
    • Learning Services
    • Middleware System
    • Networking
    • Operating System
    • Productivity Tools
    • Security
    • VLS
      • Windows
      • Office
  • Opensource
  • Wordpress
    • Plugin WP
    • Themes WP
  • Others

Search

0 Wishlist

Cart

Categories
  • Microsoft
    • Microsoft Apps
    • Office
    • Operating System
    • VLS
    • Developer Tools
    • Productivity Tools
    • Database
    • AI + Machine Learning
    • Middleware System
    • Learning Services
    • Analytics
    • Networking
    • Compute
    • Security
    • Internet Of Things
  • Adobe
  • Matlab
  • Google
  • Visual Paradigm
  • WordPress
    • Plugin WP
    • Themes WP
  • Opensource
  • Others
More Categories Less Categories
  • Get Pack
    • Product Category
    • Simple Product
    • Grouped Product
    • Variable Product
    • External Product
  • My Account
    • Download
    • Cart
    • Checkout
    • Login
  • About Us
    • Contact
    • Forum
    • Frequently Questions
    • Privacy Policy
  • Forum
    • News
      • Category
      • News Tag

iconTicket Service Desk

  • My Download
  • Checkout
Application Package Repository Telkom University
All Categories

All Categories

  • Visual Paradigm
  • IBM
  • Adobe
  • Google
  • Matlab
  • Microsoft
    • Microsoft Apps
    • Analytics
    • AI + Machine Learning
    • Compute
    • Database
    • Developer Tools
    • Internet Of Things
    • Learning Services
    • Middleware System
    • Networking
    • Operating System
    • Productivity Tools
    • Security
    • VLS
      • Windows
      • Office
  • Opensource
  • Wordpress
    • Plugin WP
    • Themes WP
  • Others

Search

0 Wishlist

Cart

Menu
  • Home
    • Download Application Package Repository Telkom University
    • Application Package Repository Telkom University
    • Download Official License Telkom University
    • Download Installer Application Pack
    • Product Category
    • Simple Product
    • Grouped Product
    • Variable Product
    • External Product
  • All Pack
    • Microsoft
      • Operating System
      • Productivity Tools
      • Developer Tools
      • Database
      • AI + Machine Learning
      • Middleware System
      • Networking
      • Compute
      • Security
      • Analytics
      • Internet Of Things
      • Learning Services
    • Microsoft Apps
      • VLS
    • Adobe
    • Matlab
    • WordPress
      • Themes WP
      • Plugin WP
    • Google
    • Opensource
    • Others
  • My account
    • Download
    • Get Pack
    • Cart
    • Checkout
  • News
    • Category
    • News Tag
  • Forum
  • About Us
    • Privacy Policy
    • Frequently Questions
    • Contact
Home/Matlab/calling a python fuction within matlab

calling a python fuction within matlab

PuTI / 2025-01-21
calling a python fuction within matlab
Matlab News

Hi,

i encountered the problem that when calling a python function within the matlab environment, the return value from python is not recognized in matlab – i always get a py.NoneType as an output.
Here my code in python, it is a code to send E-mails. I can see the print commands as an output in matlab but cannot capture the return values properly (always a py.NoneType in Matlab). When calling the function directly from python everything works fine.
What could the problem be ?

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

def send_email(gmail_user, gmail_password, to_email, subject, body, attachment_path):
"""
Send an email with an attachment using Gmail’s SMTP server.

Parameters:
gmail_user (str): Your Gmail email address
gmail_password (str): Your Gmail app password
to_email (str): Recipient’s email address
subject (str): Subject of the email
body (str): Body text of the email
attachment_path (str): Full path to the attachment file
"""

try:
# Create the email
email = MIMEMultipart()
email[‘From’] = gmail_user
email[‘To’] = to_email
email[‘Subject’] = subject

# Add the email body
email.attach(MIMEText(body, ‘plain’))

# Normalize the file path
attachment_path = attachment_path.encode(‘utf-8’).decode(‘utf-8’)
attachment_path = os.path.normpath(attachment_path)
# attachment_path = attachment_path.replace("\", "\\")
print(f"Processed attachment path: {attachment_path}")

# Check if the attachment file exists
if not os.path.exists(attachment_path):
print(f"Error: The file {attachment_path} does not exist.")
return ‘Name in Email not correct!’;

# Open the file in binary mode and encode it
with open(attachment_path, ‘rb’) as attachment_file:
attachment = MIMEBase(‘application’, ‘octet-stream’) # Generic MIME type, could be more specific
attachment.set_payload(attachment_file.read())
encoders.encode_base64(attachment)

# Extract filename from the path
filename = os.path.basename(attachment_path)
print(f"Attachment found: {filename}")

# Add header for the attachment
attachment.add_header(‘Content-Disposition’, ‘attachment’, filename=filename)

# Attach the file to the email
email.attach(attachment)
print(f"Attachment ‘{filename}’ added successfully.")
return ‘yes’

# Send the email using Gmail’s SMTP server
with smtplib.SMTP(‘smtp.gmail.com’, 587) as server:
server.starttls() # Secure the connection
server.login(gmail_user, gmail_password)
server.sendmail(gmail_user, to_email, email.as_string())
print("Email sent successfully!")

return ‘Email sent!’

except Exception as e:
print(f"Failed to send email: {e}")
return ‘Email failed fatal!’

return "Unknown error occurred."

Thanks in advance
I tried a "mini example" with a simple python function (just adding two numbers) and it worked, so I have no idea why the more advanced code does not work.Hi,

i encountered the problem that when calling a python function within the matlab environment, the return value from python is not recognized in matlab – i always get a py.NoneType as an output.
Here my code in python, it is a code to send E-mails. I can see the print commands as an output in matlab but cannot capture the return values properly (always a py.NoneType in Matlab). When calling the function directly from python everything works fine.
What could the problem be ?

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

def send_email(gmail_user, gmail_password, to_email, subject, body, attachment_path):
"""
Send an email with an attachment using Gmail’s SMTP server.

Parameters:
gmail_user (str): Your Gmail email address
gmail_password (str): Your Gmail app password
to_email (str): Recipient’s email address
subject (str): Subject of the email
body (str): Body text of the email
attachment_path (str): Full path to the attachment file
"""

try:
# Create the email
email = MIMEMultipart()
email[‘From’] = gmail_user
email[‘To’] = to_email
email[‘Subject’] = subject

# Add the email body
email.attach(MIMEText(body, ‘plain’))

# Normalize the file path
attachment_path = attachment_path.encode(‘utf-8’).decode(‘utf-8’)
attachment_path = os.path.normpath(attachment_path)
# attachment_path = attachment_path.replace("\", "\\")
print(f"Processed attachment path: {attachment_path}")

# Check if the attachment file exists
if not os.path.exists(attachment_path):
print(f"Error: The file {attachment_path} does not exist.")
return ‘Name in Email not correct!’;

# Open the file in binary mode and encode it
with open(attachment_path, ‘rb’) as attachment_file:
attachment = MIMEBase(‘application’, ‘octet-stream’) # Generic MIME type, could be more specific
attachment.set_payload(attachment_file.read())
encoders.encode_base64(attachment)

# Extract filename from the path
filename = os.path.basename(attachment_path)
print(f"Attachment found: {filename}")

# Add header for the attachment
attachment.add_header(‘Content-Disposition’, ‘attachment’, filename=filename)

# Attach the file to the email
email.attach(attachment)
print(f"Attachment ‘{filename}’ added successfully.")
return ‘yes’

# Send the email using Gmail’s SMTP server
with smtplib.SMTP(‘smtp.gmail.com’, 587) as server:
server.starttls() # Secure the connection
server.login(gmail_user, gmail_password)
server.sendmail(gmail_user, to_email, email.as_string())
print("Email sent successfully!")

return ‘Email sent!’

except Exception as e:
print(f"Failed to send email: {e}")
return ‘Email failed fatal!’

return "Unknown error occurred."

Thanks in advance
I tried a "mini example" with a simple python function (just adding two numbers) and it worked, so I have no idea why the more advanced code does not work. Hi,

i encountered the problem that when calling a python function within the matlab environment, the return value from python is not recognized in matlab – i always get a py.NoneType as an output.
Here my code in python, it is a code to send E-mails. I can see the print commands as an output in matlab but cannot capture the return values properly (always a py.NoneType in Matlab). When calling the function directly from python everything works fine.
What could the problem be ?

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

def send_email(gmail_user, gmail_password, to_email, subject, body, attachment_path):
"""
Send an email with an attachment using Gmail’s SMTP server.

Parameters:
gmail_user (str): Your Gmail email address
gmail_password (str): Your Gmail app password
to_email (str): Recipient’s email address
subject (str): Subject of the email
body (str): Body text of the email
attachment_path (str): Full path to the attachment file
"""

try:
# Create the email
email = MIMEMultipart()
email[‘From’] = gmail_user
email[‘To’] = to_email
email[‘Subject’] = subject

# Add the email body
email.attach(MIMEText(body, ‘plain’))

# Normalize the file path
attachment_path = attachment_path.encode(‘utf-8’).decode(‘utf-8’)
attachment_path = os.path.normpath(attachment_path)
# attachment_path = attachment_path.replace("\", "\\")
print(f"Processed attachment path: {attachment_path}")

# Check if the attachment file exists
if not os.path.exists(attachment_path):
print(f"Error: The file {attachment_path} does not exist.")
return ‘Name in Email not correct!’;

# Open the file in binary mode and encode it
with open(attachment_path, ‘rb’) as attachment_file:
attachment = MIMEBase(‘application’, ‘octet-stream’) # Generic MIME type, could be more specific
attachment.set_payload(attachment_file.read())
encoders.encode_base64(attachment)

# Extract filename from the path
filename = os.path.basename(attachment_path)
print(f"Attachment found: {filename}")

# Add header for the attachment
attachment.add_header(‘Content-Disposition’, ‘attachment’, filename=filename)

# Attach the file to the email
email.attach(attachment)
print(f"Attachment ‘{filename}’ added successfully.")
return ‘yes’

# Send the email using Gmail’s SMTP server
with smtplib.SMTP(‘smtp.gmail.com’, 587) as server:
server.starttls() # Secure the connection
server.login(gmail_user, gmail_password)
server.sendmail(gmail_user, to_email, email.as_string())
print("Email sent successfully!")

return ‘Email sent!’

except Exception as e:
print(f"Failed to send email: {e}")
return ‘Email failed fatal!’

return "Unknown error occurred."

Thanks in advance
I tried a "mini example" with a simple python function (just adding two numbers) and it worked, so I have no idea why the more advanced code does not work. python, function, py.nonetype MATLAB Answers — New Questions

​

Tags: matlab

Share this!

Related posts

Warning: Error updating FunctionLine in using fplot
2025-05-12

Warning: Error updating FunctionLine in using fplot

Solid creation Simulink, simscape multibody: stuck in loading
2025-05-12

Solid creation Simulink, simscape multibody: stuck in loading

Issues with EMG Signal Acquisition via Simulink Desktop Real-Time (SDRT) and DAQ Board Usage
2025-05-12

Issues with EMG Signal Acquisition via Simulink Desktop Real-Time (SDRT) and DAQ Board Usage

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Search

Categories

  • Matlab
  • Microsoft
  • News
  • Other
Application Package Repository Telkom University

Tags

matlab microsoft opensources
Application Package Download License

Application Package Download License

Adobe
Google for Education
IBM
Matlab
Microsoft
Wordpress
Visual Paradigm
Opensource

Sign Up For Newsletters

Be the First to Know. Sign up for newsletter today

Application Package Repository Telkom University

Portal Application Package Repository Telkom University, for internal use only, empower civitas academica in study and research.

Information

  • Telkom University
  • About Us
  • Contact
  • Forum Discussion
  • FAQ
  • Helpdesk Ticket

Contact Us

  • Ask: Any question please read FAQ
  • Mail: helpdesk@telkomuniversity.ac.id
  • Call: +62 823-1994-9941
  • WA: +62 823-1994-9943
  • Site: Gedung Panambulai. Jl. Telekomunikasi

Copyright © Telkom University. All Rights Reserved. ch

  • FAQ
  • Privacy Policy
  • Term

This Application Package for internal Telkom University only (students and employee). Chiers... Dismiss