我想以纯文本发送php邮件,我不允许使用html但是我遇到了间距问题。输出应如下所示:
Item Qty Price
Whatever 1 5000USD
Whatever2 2 50USD
Subtotal 5050USD
Tax 50USD
TOTAL 5100USD
考虑到金额和产品名称总是不同,这很难看,因此最终看起来非常混乱。由于列宽总是不同,我想右对齐最后一列,所以一切看起来都是对齐和干净的。有没有实用的方法呢?
谢谢。
编辑:
这是我目前的代码:
function render($indent = "", InvoicePayment $payment = null)
{
$prefix = (!is_null($payment) && !$payment->isFirst()) ? 'second' : 'first';
$tm_added = is_null($payment) ? $this->tm_added : $payment->dattm;
$newline = "\r\n";
$price_width = max(mb_strlen(Am_Currency::render($this->{$prefix . '_total'}, $this->currency)), 8);
$column_padding = 3;
$column_title_max = 60;
$column_title_min = 20;
$column_qty = 15;
$column_num = 3;
$column_amount = $price_width;
$space = str_repeat(' ', $column_padding);
$max_length = 0;
foreach ($this->getItems() as $item) {
$max_length = max(mb_strlen(___($item->item_title)), $max_length);
}
$column_title = max(min($max_length, $column_title_max), $column_title_min);
$row_width = $column_num + $column_padding +
$column_title + $column_padding +
$column_qty + $column_padding +
$column_amount + $column_padding;
$column_total = $column_title +
$column_qty + $column_padding;
$total_space = str_repeat(' ', $column_padding + $column_num + $column_padding);
$border = $indent . str_repeat('-', $row_width) . "$newline";
$out = $indent . ___("Invoice") . ' #' . $this->public_id . " / " . amDate($tm_added) . "$newline";
$out .= $border;
$out .= $indent . sprintf("{$space}%{$column_num}s{$space}%-{$column_title}s{$space}%{$column_qty}s{$space}%{$price_width}s$newline",
" ", ___('Subscription/Product Title'), ___('Quantity'), ___('Unit Price'));
$out .= $border;
$num = 1;
foreach ($this->getItems() as $item) {
$title = explode("\n", $this->wordWrap(___($item->item_title), $column_title, "\n", true));
$out .= $indent . sprintf("{$space}%{$column_num}s{$space}%-{$column_title}s{$space}%{$column_qty}s{$space}%{$price_width}s$newline",
$num . '.', $title[0], $item->qty, Am_Currency::render($item->{$prefix . '_price'}, $this->currency));
for ($i=1; $i<count($title); $i++)
$out .= $indent . sprintf("{$space}%{$column_num}s{$space}%-{$column_title}s$newline", ' ', $title[$i]);
$num++;
}
$out .= $border;
if ($this->{$prefix . '_subtotal'} != $this->{$prefix . '_total'})
$out .= $indent . sprintf("{$total_space}%-{$column_total}s{$space}%{$price_width}s$newline", ___('Subtotal'), Am_Currency::render($this->{$prefix . '_subtotal'}, $this->currency));
if ($this->{$prefix . '_discount'} > 0)
$out .= $indent . sprintf("{$total_space}%-{$column_total}s{$space}%{$price_width}s$newline", ___('Discount'), Am_Currency::render($this->{$prefix . '_discount'}, $this->currency));
if ($this->{$prefix . '_shipping'} > 0)
$out .= $indent . sprintf("{$total_space}%-{$column_total}s{$space}%{$price_width}s$newline", ___('Shipping'), Am_Currency::render($this->{$prefix . '_shipping'}, $this->currency));
if ($this->{$prefix . '_tax'} > 0)
$out .= $indent . sprintf("{$total_space}%-{$column_total}s{$space}%{$price_width}s$newline", ___('Tax'), Am_Currency::render($this->{$prefix . '_tax'}, $this->currency));
$out .= $border;
$out .= $newline;
$out .= $indent . sprintf("{$total_space}%-{$column_total}s{$space}%{$price_width}s$newline", ___('Total'), Am_Currency::render($this->{$prefix . '_total'}, $this->currency));
if ($this->rebill_times) {
$terms = explode("\n", $this->wordWrap(___($this->getTerms()), $row_width, "\n", true));
foreach ($terms as $term_part)
$out .= $indent . $term_part . $newline;
$out .= $border;
}
return $out;
}
答案 0 :(得分:1)
间距将无法正常工作,因为管理代码非常困难。
而是将这些字段/值映射到HTML表格中并将边框设置为0,您也应该将您的邮件呈现为HTML,以使上述内容起作用。
要执行此操作..转到http://www.tablesgenerator.com/html_tables
并根据需要生成表格,点击生成,您将获得它的HTML源代码..
之后,您可以将这些来源粘贴到电子邮件中,不要忘记将边框设置为0. 将邮件标题呈现为HTML,将其作为HTML邮件发送
// To send HTML mail, the Content-type header must be set
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
抓取邮件代码
答案 1 :(得分:1)
如果在两个元素之间给出多个空格,则HTML将仅考虑单个空格。您可以使用 
作为间距。另一种最好的方法是为此编写一个HTML表。
答案 2 :(得分:1)
您可以使用表格
<table width="600" cellpadding="0" cellspacing="0" border="0" align="center">
<tr>
<td>Item</td> <td>Qty</td> <td>Price</td>
</tr>
<tr>
<td>Whatever1</td> <td>1</td> <td>5000USD</td>
</tr>
<tr>
<td>Whatever2</td> <td>2</td> <td>50USD</td>
</tr>
<tr><td> </td> <td> </td> <td> </td></tr>
<tr>
<td>Subtotal</td> <td> </td> <td>5050USD</td>
</tr>
<tr>
<td>Tax</td> <td> </td> <td>50USD</td>
</tr>
<tr><td> </td> <td> </td> <td> </td></tr>
<tr>
<td>Total</td> <td> </td> <td>5100USD</td>
</tr>
</table>