This post is about two things. First, I talk on how to use FOR loops. Then it is about LOVE and how to spam your girlfriend email address on valentine’s day. Whether you are interested in one of these or both is up to you.
You are in love, lucky you. Thanks to the usage of the IF statement in Matlab, you have found the right person. Today is February 13th and you have found a great idea to surprise your girlfriend. Tomorrow, she is going to open her email account and there will be HUNDREDS of emails from you.
You don’t know how to do that.
If you were a buddhist monk, you could slowly send all these emails one by one. But how could you be a buddhist monk and be dominated by such a passionate emotion?
Lucky you (again!), you have found Matlabtips.com AND you have Matlab.
First, you need to set sendmail up in Matlab. That’s easy, I took the trick from here. For me it works fine with Gmail like so :
setpref('Internet', 'E_mail', [email protected]'); setpref('Internet', 'SMTP_Username', [email protected]'); setpref('Internet', 'SMTP_Password', 'YOURPASSWORD'); setpref('Internet', 'SMTP_Server', 'smtp.gmail.com'); props = java.lang.System.getProperties; props.setProperty('mail.smtp.auth','true'); props.setProperty('mail.smtp.socketFactory.class', 'javax.net.ssl.SSLSocketFactory'); props.setProperty('mail.smtp.socketFactory.port', '465');
Just replace the field in CAPITALS with your own Gmail account parameters.
Now that this is done. How to send 200 hundreds email?
Just use FOR loops, like so:
for i=1:200 sendmail([email protected]', 'Happy valentine','I love you!'); end
What this code does is to start a for loop. First it creates a variable (here i) and it iterates this variable from 1 to 200 with a step of 1. At each iteration it executes all the code between the for statement and the end statement. So here, it will send slowly but surely 200 emails to your girlfriend. The nice think about you is that you are not on her SPAM list so it will get there, don’t worry.
There are several variants of the for loops. You also can do :
Iterations=1:200; for i=Iterations sendmail([email protected]', 'Happy valentine','I love you!'); end
This code is equivalent. It will use the values in the matrix Iterations to iterates i. The advantage of this code is that you could, if you wanted, have any matrix Iterations. So you can even have random numbers like so :
Iterations=randi(100,200,1); for i=Iterations sendmail([email protected]', 'Happy valentine','['I love you ' num2str(i) ' times']); end
Here randi will create a 200 by 1 matrix of random integers between 1 and 100. So the for loop will use this for i. I also added a little bonus for you. Here your girlfriend will receive a message that changes every time. ‘[‘ and ‘]’ is use to assemble strings together. num2str is used to convert a number into a string.
One last thing. This is actually not really used very often in real programs but for loops can go forever ON PURPOSE. Here, we have the one example where it would make sense if, for instance, you are especially mean and you want to completely flood your girlfriend account for the entire day.
So do this :
for i=1:Inf sendmail([email protected]', 'Happy valentine','['I love you ' num2str(i) ' times']); end
The for loop will go to infinity. The only way to get out of here (for the sake of your computer) is to CTRL+C.