Use per to automatically send emails

Source: Internet
Author: User
Recently, the automatic deployment of the automated test environment was successful, but my colleagues said that we didn't know whether you installed the environment successfully or what the latest version is, it is not very convenient, so I want to try to use Perl to write a script to automatically send emails. But when the latest build is successfully installed, I will send an email to the tester, in this way, the whole process is automated.

Haha, here is my implementation. It is mainly written through the net: SMTP package provided by Perl.

  1. #! /Usr/bin/perl-W

  2. Use Net: SMTP;
  3. Use Authen: SASL; // you need to install it yourself (ppm install Authen: SASL). This is required for the encrypted email server.

  4. Sub send_mail {
  5. My ($ to, $ from, $ subject, @ body) = @_;
  6. My $ host = "smtp.163.net"; # mail server address
  7. My $ SMTP = net: SMTP-> New (
  8. Host => $ host,
  9. Hello => $ host,
  10. Timeout => 30,
  11. DEBUG => 1
  12. );
  13. # $ SMTP-> auth ('username', 'Password'); this line is required for the encrypted SMTP server; otherwise, this line is not required.
  14. Die "cocould not open connection: $! "If (! Defined $ SMTP );
  15. $ SMTP-> mail ($ from); # sender
  16. $ SMTP-> to ($ to); # Receiver
  17. $ SMTP-> data (); # data sending start mark
  18. $ SMTP-> datasend ("Content-Type: text/plain; charset = gb2312/N"); # indicates the header part of the sent data
  19. $ SMTP-> datasend ("from: $ from/N ");
  20. $ SMTP-> datasend ("Subject: $ subject/N ");
  21. $ SMTP-> datasend ("/N ");
  22. Foreach (@ body ){
  23. $ SMTP-> datasend ("$ _/N"); # send data
  24. }
  25. $ SMTP-> dataend (); # sending end mark
  26. $ SMTP-> quit;
  27. }
  28. Send_mail ("zhtsuc", "zhtsuc", "test", QW (Hello World ));

Note that the sender and receiver parameters of an email only need the user name, not the full name of your email. This is very important, otherwise it will fail to be sent.

However, when I test using QQ mail, I find that the receiver's information must be given to the full name. I don't know what to ask them to violate this rule. If a message is prompted that the receiver is incorrect, then try to modify it like this.

This automation script is very useful in the automation environment and can greatly improve the efficiency.
Therefore, we recommend that you do this. :-D.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.