在Ruby中利用Net::SMTP類寄送電子郵件的教程

來源:互聯網
上載者:User

在Ruby中利用Net::SMTP類寄送電子郵件的教程

   這篇文章主要介紹了在Ruby中利用Net::SMTP類寄送電子郵件的教程,包括類中所帶方法的用法介紹,需要的朋友可以參考下

  簡易郵件傳輸通訊協定(SMTP)寄送電子郵件及路由的e-mail郵件伺服器之間的協議處理。

  Ruby 提供 Net::SMTP 類的簡易郵件傳輸通訊協定(SMTP)用戶端的串連,並提供了兩個新的方法:new 和 start.

  new 帶兩個參數:

  server name 預設為 localhost

  port number 預設為熟知的 25

  start 方法帶有以下這些參數:

  server - IP SMTP伺服器名稱,預設為localhost

  port - 連接埠號碼,預設為25

  domain - 郵件寄件者的網域名稱,預設為 ENV["HOSTNAME"]

  account - 使用者名稱,預設為 nil

  password - 使用者密碼,預設為 nil

  authtype - 授與類型,預設為 cram_md5

  SMTP對象有一個執行個體方法調用sendmail,後者通常會被用來做郵寄訊息的工作。它有三個參數:

  source - 字串或數組或任何同每個迭代一次返回一個字串。

  sender - 一個字串,它將會出現在電子郵件欄位中。

  recipients - 代表收件者的地址的字串的字串或數組

  例子:

  下面是一個簡單使用Ruby指令碼的方法來發送一個電子郵件。試一次看看吧:

  ?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

require 'net/smtp'

 

message = <<MESSAGE_END

From: Private Person <me@fromdomain.com>

To: A Test User <test@todomain.com>

Subject: SMTP e-mail test

 

This is a test e-mail message.

MESSAGE_END

 

Net::SMTP.start('localhost') do |smtp|

smtp.send_message message, 'me@fromdomain.com',

'test@todomain.com'

end

  在這裡已經放置了一個基本的電子郵件訊息,使用檔案,在這裡同時注意正確格式化標題。一封郵件需要寄件者,收件者,主題頭,從電子郵件的主體有一個空白行分隔。

  隨著訊息要發送郵件使用Net::SMTP串連到本地機器上的SMTP伺服器,然後使用 send_message 方法,從地址,目的地址作為參數(即使地址電子郵件本身範圍內,這些並非總是用來將郵件路由)。

  如果還沒有在機器上運行一個SMTP伺服器,可以使用的Net::SMTP遠程SMTP伺服器進行通訊。除非使用一個webmail服務(如Hotmail或雅虎郵件),電子郵件供應商將提供外發郵件伺服器的細節,可以提Net::SMTP,具體如下:

  ?

1

Net::SMTP.start('mail.your-domain.com')

  這行代碼串連到SMTP伺服器mail.your-domain.com 連接埠25。,而無需使用任何使用者名稱或密碼。不過,如果需要的話可以指定連接埠號碼或其他參數等。例如:

  ?

1

2

3

4

Net::SMTP.start('mail.your-domain.com',

25,

'localhost',

'username', 'password' :plain)

  這個例子串連mail.your-domain.com到SMTP伺服器使用的使用者名稱和密碼以純文字格式。它標識為localhost用戶端的主機名稱。

  使用Ruby發送HTML電子郵件:

  當想要發送簡訊,Ruby所有內容將被視為簡單的文本。即使會在簡訊中包含HTML標記,它會顯示簡單的文本和HTML標記將不會格式化HTML文法。但是Ruby的 Net::SMTP 提供了實際的HTML郵件發送HTML訊息的選項。

  在寄送電子郵件訊息時,可以指定一個MIME版本,內容類型和字元集發送HTML電子郵件。

  例如:

  下面的例子作為電子郵件發送HTML內容。試一次看看吧:

  ?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

require 'net/smtp'

 

message = <<MESSAGE_END

From: Private Person <me@fromdomain.com>

To: A Test User <test@todomain.com>

MIME-Version: 1.0

Content-type: text/html

Subject: SMTP e-mail test

 

This is an e-mail message to be sent in HTML format

 

<b>This is HTML message.</b>

<h1>This is headline.</h1>

MESSAGE_END

 

Net::SMTP.start('localhost') do |smtp|

smtp.send_message message, 'me@fromdomain.com',

'test@todomain.com'

end

  作為電子郵件的附件發送:

  混合內容發送一封電子郵件,要求設定Content-type頭為 multipart/mixed。然後,文本和附件部分可以指定 boundaries 範圍內。

  兩個連字號後跟一個唯一的編號,不能出現在電子郵件的訊息部分的邊界開始。最後一個邊界表示電子郵件的最後一節的結尾也必須有兩個連字號。

  附加檔案使用 pack("m") 函數來base64編碼傳輸之前進行編碼。

  例子:

  下面的例子將檔案 /tmp/test.txt 作為附件發送。試一次看看吧:

  ?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

require 'net/smtp'

 

filename = "/tmp/test.txt"

# Read a file and encode it into base64 format

filecontent = File.read(filename)

encodedcontent = [filecontent].pack("m") # base64

 

marker = "AUNIQUEMARKER"

 

body =<<EOF

This is a test email to send an attachement.

EOF

 

# Define the main headers.

part1 =<<EOF

From: Private Person <me@fromdomain.net>

To: A Test User <test@todmain.com>

Subject: Sending Attachement

MIME-Version: 1.0

Content-Type: multipart/mixed; boundary=#{marker}

--#{marker}

EOF

 

# Define the message action

part2 =<<EOF

Content-Type: text/plain

Content-Transfer-Encoding:8bit

 

#{body}

--#{marker}

EOF

 

# Define the attachment section

part3 =<<EOF

Content-Type: multipart/mixed; name="#{filename}"

Content-Transfer-Encoding:base64

Content-Disposition: attachment; filename="#{filename}"

 

#{encodedcontent}

--#{marker}--

EOF

 

mailtext = part1 + part2 + part3

 

# Let's put our code in safe area

begin

Net::SMTP.start('localhost') do |smtp|

smtp.sendmail(mailtext, 'me@fromdomain.net',

['test@todmain.com'])

end

rescue Exception => e

print "Exception occured: " + e

end

  注意:可以指定多個目的地內部數組,但他們需要用逗號分隔。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.