MATLAB Answers

Sendmail and attaching images

45 views (last 30 days)
I'm encountering a problem when using sendmail to include an image attachment. I have sendmail set up so that it works. When I have a plot, I can send it by typing the following in the command window
print(gcf,'-dpng','-r0','bargraph.png')
sendmail('fishbacp@mail.gvsu.edu','Bargraph from script attached','bargraph.png')
However, when I try to include the above lines within a script, the actual image is not attached. Instead, I receive the email but it merely has "bargraph.png" appearing as the message body.

  0 Comments

Sign in to comment.

Accepted Answer

Paul Fishback
Paul Fishback on 23 Apr 2018
Thank you so much. Am I correct that your code prompts the use to specify the attachment directory, as described below the line
%------------ Attach file ----------
I'm curious to know if there's a simpler way to do this so that I can let a script run for several hours (or couple of days) and then return to find that the image attachment has automatically been sent to my inbox.

  3 Comments

Image Analyst
Image Analyst on 24 Apr 2018
You can use whatever method you want to generate the filename. You can ask the user, hard code in a permanent/known filename, generate one from variables in your program, or whatever. Just replace the user-asking part with however you want to make up a file name.
Image Analyst
Image Analyst on 25 Apr 2018
Normally you don't accept your own "Answer".

Sign in to comment.

More Answers (2)

Image Analyst
Image Analyst on 23 Apr 2018
See attached demo. Make the trivial, obvious changes and it should work.

  3 Comments

Arsalan Akbar
Arsalan Akbar on 25 Apr 2018
hi Image Analyst I need some help. I'm new in image processing and i want to cut an image to get longitudinal view .but dont know how to implement this . can you help me . i have attached the image . in this image, i have upper circular shape and i want to get lower longitudinal part. please help me if you can
Image Analyst
Image Analyst on 25 Apr 2018
This has nothing to do with Paul's question on email. Please start your own question.

Sign in to comment.


Deena Dayalan
Deena Dayalan on 7 Feb 2019
Edited: Image Analyst on 7 Feb 2019
Try this code
% Set up the preferences
setpref('Internet','SMTP_Server','smtp.gmail.com');
setpref('Internet','E_mail','urmailid@gmail.com'); %sender mail id
setpref('Internet','SMTP_Username','urmailid@gmail.com'); %sender mail id
setpref('Internet','SMTP_Password','urpassword'); %sender password
% The following is necessary only if you are using GMail
props = java.lang.System.getProperties;
props.setProperty('mail.smtp.auth','true');
props.setProperty('mail.smtp.starttls.enable','true');
%props.setProperty('mail.smtp.socketFactory.class', 'javax.net.ssl.SSLSocketFactory');
%props.setProperty('mail.smtp.socketFactory.port','465');
%------------ Attach file ---------------------------------------------------
% Have user browse and specify the full file name of the file to attach.
% startingFolder = pwd;
startingFolder = 'C:\Users\Public\Documents'; % Wherever you want.
defaultFileName = fullfile(startingFolder, '*.*');
% Let user browse to attach a file.
[baseFileName, folder] = uigetfile(defaultFileName, 'Select a file to attach to the e-mail.');
if baseFileName == 0
% User clicked the Cancel button.
return;
end
attachedFullFileName = fullfile(folder, baseFileName); % Combine folder and base name into full file name.
sendmail('receivermailid@gmail.com','matlab mail','hello matlab',attachedFullFileName);

  0 Comments

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!