suntsh 2009-06-13 16:50 采纳率: 0%
浏览 395
已采纳

如何用ruby发送中文邮件?

require 'net/smtp'

#Senders and Recipients
from_name = 'myname'
from_mail = 'myname@126.com'
to_name = 'yourname'
to_mail = 'yourname@163.com'

#Servers and Authentication
smtp_host = 'smtp.126.com'
smtp_port = 25
smtp_domain = '126.com'
smtp_user = 'myname@126.com'
smtp_pwd = 'mypwd'

#The subject and the message
t = Time.now
subj = 'Sending Email with Ruby'
msg_body = "我的测试Check out the instructions on how to send mail using Ruby.\n"

#The date/time should look something like: Thu, 03 Jan 2006 12:33:22 -0700
msg_date = t.strftime("%a, %d %b %Y %H:%M:%S +0800")

#Compose the message for the email
msg = < Date: #{msg_date}
From: #{from_name}
To: #{to_name} <#{to_mail}>
Subject: #{subj}

#{msg_body}

END_OF_MESSAGE

Net::SMTP.start(smtp_host, smtp_port, smtp_domain, smtp_user, smtp_pwd, :plain) do |smtp|
smtp.send_message msg, smtp_user, to_mail
end

发送英文邮件正常,不能发送带中文的邮件
msg_body = "我的测试Check out the instructions on how to send mail using Ruby.\n" 这个信件内容不能有中文
提示错误
sendemail:19: invalid multibyte char (US-ASCII)
sendemail:19: invalid multibyte char (US-ASCII)

那位高手能帮忙解决一下

[b]问题补充:[/b]
我把文件转换成utf-8,还是提示有错误
/usr/local/lib/ruby/1.9.1/net/protocol.rb:291: warning: regexp match /.../n against to UTF-8 string
[b]问题补充:[/b]
转成utf-8的编码,邮件可以收到,但是国内的大部分邮箱都是gbk编码的,
到收件箱里面看到的是乱码,

  • 写回答

4条回答 默认 最新

  • Hooopo 2009-06-13 19:40
    关注

    [quote] warning: regexp match /.../n against to UTF-8 string[/quote]
    只是warning没问题的,你看看邮件收到没有

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 高德地图点聚合中Marker的位置无法实时更新
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办