我正在尝试将图像显示为使用Python发送的邮件的一部分。有一个关于Python文档的例子没有用。
from datetime import datetime
import sys
import smtplib
from email.message import EmailMessage
from email.headerregistry import Address
from email.utils import make_msgid
from email.mime.image import MIMEImage
attachment = '/user/file/test.png'
import email.policy
msg = EmailMessage()
msg['To'] = Address("Srujan", 'myemail@example.com')
msg['From'] = Address("Srujan", 'myemail@example.com')
msg['Subject'] = "Nice message goes with it "+str(datetime.now())
id = make_msgid()
msg.set_type('text/html')
msg.set_content(" This is the Data Message that we want to send")
html_msg = "<br> <b><u> This is the Text .... </u></b><br> <img src='cid:{image_id}' />".format(image_id=id[1:-1])
msg.add_alternative(html_msg, subtype="html")
image_data = open(attachment, "rb")
image_mime = MIMEImage(image_data.read())
image_data.close()
msg.add_attachment(image_mime, cid=id, filename = "myown.png" ,)
try:
with smtplib.SMTP('example.com') as s:
s.ehlo()
s.starttls()
s.ehlo()
s.send_message(msg)
s.close()
print("Email sent!")
except:
print("Unable to send the email. Error: ", sys.exc_info()[0])
raise
我发现了一些事情:
收件人:Srujan&lt;“myemail@example.com”&gt;来自:Srujan &LT; “myemail@example.com” &GT;主题:好消息与它一起2016-01-21 17:39:23.642762 MIME-Version:1.0 Content-Type:multipart / mixed; 边界= “=============== 3463325241650937591 ==”
- =============== 3463325241650937591 == Content-Type:multipart / alternative;边界= “=============== 0739224896516732414 ==”
- =============== 0739224896516732414 == Content-Type:text / plain; charset =“utf-8”Content-Transfer-Encoding:7bit
这是我们要发送的数据消息
- =============== 0739224896516732414 == Content-Type:text / html; charset =“utf-8”Content-Transfer-Encoding:7bit MIME-Version:1.0
这是文字....
- =============== 0739224896516732414 == -
- =============== 3463325241650937591 == 内容类型:message / rfc822 Content-Transfer-Encoding:8bit Content-Disposition:attachment; filename =“myown.png”内容ID: &LT; 20160122013923.64205.76661&GT; MIME版本:1.0
内容类型:image / png MIME版本:1.0 Content-Transfer-Encoding: BASE64
iVBORw0KGgoAAAANSUhEUgAAAyAAAAJYCAIAAAAVFBUnAAAABmJLR0QA / WD / AP + gvaeTAAAgAElE QVR4nOzdd2BUVfYH8HNfmZJAChAEQ1FAqoKgCAsWLEgTG4gKP11FQNG1AauIgGBXVhRUsCK4i6gI
现在附加的消息有两个内容类型值。电子邮件附带文字和无图像
我已成功使用MultiPart类,但希望通过EmailMessage实现这一目标。
答案 0 :(得分:4)
关于这个主题的Python文档是不完整/错误真的很遗憾。我想做同样的事情并得到同样的问题(附件有两个内容类型)
在public function Upload($id){
$upload = $this->input->post('fotoDsn');
//Foto Set
$photoName = gmdate("d-m-y-H-i-s", time()+3600*7).".jpg";
$config['upload_path'] = './assets/';
$config['allowed_types'] = 'gif||jpg||png';
$config['max_size'] = '2048000';
$config['file_name'] = $photoName;
$this->load->library('upload',$config);
if($this->upload->do_upload('userfile')){
$upload = 1;
}
else{
$upload = 2;
}
if($upload==1){
$data = array(
'foto_dosen'=>$photoName);
$update = $this->MDosen->update(array('id'=>$id), $data);
if($update){
echo 1;
}else{
echo 2;
}
}//else kalo gagal
}
个对象上使用function update($cond, $data){
$this->db->where($cond);
$query = $this->db->update('dosen', $data);
return $query;
}
方法代替attach()
解决了问题。唯一需要注意的是,在执行此操作之前,您必须将add_attachment()
转换为EmailMessage
类型。一个例子:
EmailMessage