TypeError:只能在发送电子邮件时将列表(而不是“ str”)串联在一起

时间:2019-01-09 21:05:13

标签: python

尝试在接收器中发送带有user_id的电子邮件时出现此错误。请有人建议如何解决它。我的代码示例:

if #something == True:
    user_id = q["user"]
    print user_id ####produces [['e11111'], ['e11234']]

    Sender = "Sender@sender.com"
    Receiver = (user_id[0:6] + '@co.com', 'john.smith@co.com')
    msg = MIMEMultipart('alternative')
    msg['Subject'] = "Subject"
    msg['From'] = Sender
    msg['To'] = ', '.join(Receiver)
    html = "text here"

    part = MIMEText(html, 'html')
    msg.attach(part)

这对

正常工作
Receiver = ('Joshua@co.com', 'john.smith@co.com')

或者,如果打印user_id仅产生一个结果-e11111。 我需要将该电子邮件发送到e11111 @ co.com,e11234 @ co.com和john.smith@co.com

2 个答案:

答案 0 :(得分:1)

如果user_id[['e11111'], ['e11234']]形式的列表列表,则可以执行以下操作:

 Receiver = tuple([i[0][0:6] + '@co.com' for i in user_id] + ['john.smith@co.com'])

这将从user_id内的1长度列表中提取字符串,并将'@co.com'字符串附加到其中。约翰·史密斯(John smith)被连接到结果列表,最终列表被转换成一个元组(我想Receiver应该是一个元组,如果可以的话,可以省略tuple()

评论后编辑

对于i[0][0:6],仅使用字符串的前6个字符。

答案 1 :(得分:0)

如上所述,void singleton_utest::test() { try { singleton<int, int>::instance(); UTEST_CHECK(false); } catch (std::logic_error& e) { UTEST_CHECK(true); } try { UTEST_CHECK((singleton<int, int>::create(1) == 1)); UTEST_CHECK((singleton<int, int>::instance() == 1)); } catch (...) { UTEST_CHECK(false); } using stester0 = singleton<tester0>; try { stester0::instance(); UTEST_CHECK(false); } catch (std::logic_error& e) { UTEST_CHECK(true); } try { UTEST_CHECK((stester0::create().result() == 0)); UTEST_CHECK((stester0::instance().result() == 0)); } catch (...) { UTEST_CHECK(false); } using stester1 = singleton<tester1, int>; try { stester1::instance(); UTEST_CHECK(false); } catch (std::logic_error& e) { UTEST_CHECK(true); } try { UTEST_CHECK((stester1::create(1).result() == 1)); UTEST_CHECK((stester1::instance().result() == 1)); } catch (...) { UTEST_CHECK(false); } using stester2 = singleton<tester2, int, int>; try { stester2::instance(); UTEST_CHECK(false); } catch (std::logic_error& e) { UTEST_CHECK(true); } try { UTEST_CHECK((stester2::create(1, 2).result() == 3)); UTEST_CHECK((stester2::instance().result() == 3)); } catch (...) { UTEST_CHECK(false); } } 是列表列表,而不是字符串列表。所以你可以尝试

user_id

注意:Receiver = (user_id[0][0] + '@sender', 'john.smith@co.com') 仍然是列表。