Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/75.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP mail()-HTML在添加文件附件后显示为附件_Php_Html_Email Client - Fatal编程技术网

PHP mail()-HTML在添加文件附件后显示为附件

PHP mail()-HTML在添加文件附件后显示为附件,php,html,email-client,Php,Html,Email Client,终于解决了所有的bug,现在他们说“哦,我们需要添加附件…”所以,这发送了一封纯文本版本的html邮件,效果非常好。现在我已经收到了附件,邮件客户端将以内联方式显示纯文本版本,html版本作为另一个附件,然后是一个93字节的空文件,文件名为ATT00248.txt 有谁能从后面猛击我的头,或者告诉我哪里出了问题?我希望在邮件用户界面中可以使用HTML内联,在HTML不可用的地方使用纯文本版本,并将单个附件作为附件 有什么帮助吗 <?php $template = $_SERVER['DOC

终于解决了所有的bug,现在他们说“哦,我们需要添加附件…”所以,这发送了一封纯文本版本的html邮件,效果非常好。现在我已经收到了附件,邮件客户端将以内联方式显示纯文本版本,html版本作为另一个附件,然后是一个93字节的空文件,文件名为ATT00248.txt

有谁能从后面猛击我的头,或者告诉我哪里出了问题?我希望在邮件用户界面中可以使用HTML内联,在HTML不可用的地方使用纯文本版本,并将单个附件作为附件

有什么帮助吗

<?php
$template = $_SERVER['DOCUMENT_ROOT'] . '/leads/templates/'.$_SESSION['templateFile'];
ob_start();
include($template);
$html = ob_get_contents();
ob_end_clean();

if (strlen($html) == 0) {
    echo "The template at $template did not load.";
    exit;
}

$email   = $_SESSION['user']->email;
$name    = $_SESSION['user']->first_name . ' ' . $_SESSION['user']->last_name;
$from = "$name <$email>";
$subject = unslash($_SESSION['subject']);

$TextMessage =  strip_tags(unslash($_SESSION['message']));

$notice_text = "This is a multi-part message in MIME format.";
$plain_text =  str_replace('&nbsp;',' ', $TextMessage);

if ($_SESSION['attachment']) {
    $fileatt = 'files/' . $_SESSION['attachment'];
    $file = fopen($fileatt,'rb');
    $data = fread($file,filesize($fileatt));
    fclose($file);
    $data = chunk_split(base64_encode($data));
    $mailtype = 'mixed';

    $fileatt_type = "application/octet-stream"; 
    $fileatt_name = $_SESSION['attachment'];
} else {
    $mailtype = 'alternative';
}

$semi_rand = md5(time());
$mime_boundary = "==MULTIPART_BOUNDARY_$semi_rand";
$mime_boundary_header = chr(34) . $mime_boundary . chr(34);

$body = "$notice_text

--$mime_boundary
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit

$plain_text

--$mime_boundary
Content-Type: text/html; charset=us-ascii
Content-Transfer-Encoding: 7bit

$html

--$mime_boundary
";

$body .= "Content-Type: {$fileatt_type};\n" .
" name=\"{$fileatt_name}\"\n" .
"Content-Disposition: attachment;\n" .
"Content-Transfer-Encoding: base64\n\n" .
$data . "\n\n" .
"--$mime_boundary\n";



// #1  //
if ($to = $_SESSION['recipients'][0]) {
    mail($to, $subject, $body,
    "From: " . $from . "\n" .
    "MIME-Version: 1.0\n" .
    "Content-Type: multipart/$mailtype;\n" .
    "     boundary=" . $mime_boundary_header);

    echo "Email sent to " . htmlentities($to) . ".<br />";
}


// #2  //
if ($to = $_SESSION['recipients'][1]) {
    mail($to, $subject, $body,
    "From: " . $from . "\n" .
    "MIME-Version: 1.0\n" .
    "Content-Type: multipart/$mailtype;\n" .
    "     boundary=" . $mime_boundary_header);
    echo "Email sent to " . htmlentities($to) . ".<br />";
}

// #3  //
if ($to = $_SESSION['recipients'][2]) {
    mail($to, $subject, $body,
    "From: " . $from . "\n" .
    "MIME-Version: 1.0\n" .
    "Content-Type: multipart/$mailtype;\n" .
    "     boundary=" . $mime_boundary_header);
    echo "Email sent to " . htmlentities($to) . ".<br />";
}

// #4  //
if ($to = $_SESSION['recipients'][3]) {
    mail($to, $subject, $body,
    "From: " . $from . "\n" .
    "MIME-Version: 1.0\n" .
    "Content-Type: multipart/$mailtype;\n" .
    "     boundary=" . $mime_boundary_header);
    echo "Email sent to " . htmlentities($to) . ".<br />";
}

// #5 //
if ($to = $_SESSION['recipients'][4]) {
    mail($to, $subject, $body,
    "From: " . $from . "\n" .
    "MIME-Version: 1.0\n" .
    "Content-Type: multipart/$mailtype;\n" .
    "     boundary=" . $mime_boundary_header);
    echo "Email sent to " . htmlentities($to) . ".<br />";
}

// CC self?  //
if ($_SESSION['cc_me']) {
    mail($from, $subject, $body,
    "From: " . $from . "\n" .
    "MIME-Version: 1.0\n" .
    "Content-Type: multipart/$mailtype;\n" .
    "     boundary=" . $mime_boundary_header);
    echo "Email sent to " . htmlentities($from) . ".<br />";
}

if ($fileatt) {
    unlink($fileatt);
}

echo "<a href='email_start.php'>Click here</a> to send another email.";
list($_SESSION['email'], $_SESSION['subject'], $_SESSION['bullets'], $_SESSION['message'], 
    $_SESSION['templateFile'], $_SESSION['template'], $_SESSION['cc_me'], $_SESSION['recipients']) = '';
?>

我想将此作为评论发布,但它太长了

// #1  //
if ($to = $_SESSION['recipients'][0]) {
    mail($to, $subject, $body,
    "From: " . $from . "\n" .
    "MIME-Version: 1.0\n" .
    "Content-Type: multipart/$mailtype;\n" .
    "     boundary=" . $mime_boundary_header);

    echo "Email sent to " . htmlentities($to) . ".<br />";
}


// #2  ... #3 ... #4 ... #5
或者,如果你真的想给每个人发邮件,或者

foreach ($_SESSION['recipients'] as $to ) {
    if (mail($to, $subject, $body,
    "From: " . $from . "\n" .
    "MIME-Version: 1.0\n" .
    "Content-Type: multipart/$mailtype;\n" .
    "     boundary=" . $mime_boundary_header)) {
        echo "Email sent to " . htmlentities($to) . ".<br />";
    }
}
foreach($\u会话['recipients']作为$to){
如果(邮件($收件人,$主题,$正文),
From:“.$From.”\n。
“MIME版本:1.0\n”。
“内容类型:multipart/$mailtype;\n”。
“boundary=“.mime_boundary_header)){
回显“电子邮件发送至”.htmlentities($to)。”
; } }
Pekka做得很对-使用Swiftmailer既简单又健壮

您是否考虑过使用像Swiftmailer这样的现成邮件类?佩卡-谢谢你!我太投入自己的工作了,有时会给自己带来压力!斯威夫特梅勒使工作变得简单,没有压力。非常感谢。你应该添加这个作为答案,我会选择它!谢谢你的回复。我应该发布一个指向此的链接:作为对我为何以这种方式进行迭代的解释。标题应以“\r\n”分隔。这是smtp标准的一部分,不是可选的。rfc 5321:2.3.8。线
foreach ($_SESSION['recipients'] as $to ) {
    if (mail($to, $subject, $body,
    "From: " . $from . "\n" .
    "MIME-Version: 1.0\n" .
    "Content-Type: multipart/$mailtype;\n" .
    "     boundary=" . $mime_boundary_header)) {
        echo "Email sent to " . htmlentities($to) . ".<br />";
    }
}