如何使用EventMachine发送包含content_type ='text / html'的邮件

时间:2011-03-08 08:13:56

标签: html ruby smtp eventmachine

我有一个sned邮件系统。我使用EventMachine发送邮件。但是当我收到我发送的邮件时,我只是得到了html代码,但是html视图。

如何在EventMachine中设置conent_type ='text / html'?

THX

2 个答案:

答案 0 :(得分:1)

    %w(rubygems eventmachine mailfactory).each{|lib|require lib}
    #gem install mailfactory -V

    EM.run{
        mail = MailFactory.new
        mail.to = 'xxx@gmail.com'
        mail.from = 'dongyuwei@weibo.com'
        mail.subject = 'hi!'
        #mail.text = 'hello world'
        mail.html = '<h1>hello world</h1><a href="http://weibo.com">weibo.com</a>'

        email = EM::P::SmtpClient.send(
            :from=>mail.from,
            :to=>mail.to,
            :content=>"#{mail.to_s}\r\n.\r\n",
            :header=> {"Subject" => mail.subject},
            :domain=>"session.im",
            :host=>'smtp.gmail.com',
            :port=>587,   
            :starttls=>true,  
            :auth => {
                :type=>:plain, 
                :username=>"xxxx@gmail.com", 
                :password=>"yyyyyyy"
            },
            :verbose => true
        )
        email.callback{
            puts 'Email sent!'
        }
        email.errback{ |e|
            puts 'Email failed!'
        }
    }

答案 1 :(得分:0)

构建电子邮件时,请使用文本和HTML部件将其构建为多部分。您需要设置所有标题等。这是一个示例邮件消息:

Date: Wed, 13 Apr 2011 17:44:05 -0400
From: Me <me@example.com>
To: User Name <user@example.com>
Message-ID: <7672767637637@example.com>
Subject: test email
MIME-Version: 1.0
Content-Type: multipart/alternative; boundary="4da61925_46111ba5_b0f1"
Content-Transfer-Encoding: 8bit

--4da61925_46111ba5_b0f1
Content-Type: text/plain; charset="utf-8"
Content-Transfer-Encoding: 8bit
Content-Disposition: inline

Message here!


--4da61925_46111ba5_b0f1
Content-Type: text/html; charset="utf-8"
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline

<div>
    html message here!
</div>
--4da61925_46111ba5_b0f1--