PHP电子邮件-纯文本格式的间距

PHP电子邮件-纯文本格式的间距,php,email,format,alignment,spacing,Php,Email,Format,Alignment,Spacing,我想发送纯文本的php邮件,我不允许使用html,但我有间距问题。输出应如下所示: Item Qty Price Whatever 1 5000USD Whatever2 2 50USD Subtotal 5050USD Tax 50USD TOTAL 5100USD 考虑到数量和产品名称

我想发送纯文本的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;
}
函数呈现($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'.\u total'},$this->Currency)),8);
$column_padding=3;
$column_title_max=60;
$column\u title\u min=20;
$column\u数量=15;
$column_num=3;
$column\u amount=$price\u width;
$space=str_repeat(“”,$column_padding);
$max_length=0;
foreach($this->getItems()作为$item){
$max_length=max(mb_strlen($item->item_title)),$max_length;
}
$column\u title=max(min($max\u length,$column\u title\u max),$column\u title\u min);
$row\u width=$column\u num+$column\u padding+
$column\u title+$column\u padding+
$column\u数量+$column\u填充+
$column\u amount+$column\u padding;
$column\u总计=$column\u标题+
$column\u数量+$column\u填充;
$total_space=str_repeat(“”,$column_padding+$column_num+$column_padding);
$border=$indent.str_repeat('-',$row_width)。“$newline”;
$out=$indent.\uuuuuuuuuuuuuuu(“发票”).\u35;'.$this->public\u id./“.amDate($tm\u已添加)。“$newline”;
$out.=$border;
$out.=$indent.sprintf(“{$space}%{$column_num}s{$space}%-{$column_title}s{$space}%{$column_qty}s{$space}%{$price_width}s$newline”,
“,”认购/产品名称“,”数量“,”单价“;
$out.=$border;
$num=1;
foreach($this->getItems()作为$item){
$title=explode(“\n”,$this->wordWrap($item->item\u title),$column\u 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\u Currency::render($item->{$prefix.\u price'},$this->Currency));
对于($i=1;$i{$prefix.\u小计'}!=$this->{$prefix.\u总计'})
$out.=$indent.sprintf(“{$total\u space}%-{$column\u total}s{$space}%{$price\u width}s$newline”,{$price\u('Subtotal'),Am\u Currency::render($this->{$prefix.\u Subtotal'},$this->Currency));
如果($this->{$prefix.\u折扣'}>0)
$out.=$indent.sprintf(“{$total\u space}%-{$column\u total}s{$space}%{$price\u width}s$newline”、{$price\u('Discount')、Am\u Currency::render($this->{$prefix.\u Discount'},$this->Currency));
如果($this->{$prefix.\u shipping'}>0)
$out.=$indent.sprintf(“{$total\u space}%-{$column\u total}s{$space}%{$price\u width}s$newline”、{$price\u('Shipping')、Am\u Currency::render($this->{$prefix.\u Shipping'},$this->Currency));
如果($this->{$prefix.\u tax'}>0)
$out.=$indent.sprintf(“{$total\u space}%-{$column\u total}s{$space}%{$price\u width}s$newline”、{$price\u('Tax')、Am\u Currency::render($this->{$prefix.\u Tax'},$this->Currency));
$out.=$border;
$out.=$newline;
$out.=$indent.sprintf(“{$total\u space}%-{$column\u total}s{$space}%{$price\u width}s$newline”,{$price\u('total'),Am\u Currency::render($this->{$prefix.\u total'},$this->Currency));
如果($this->rebill\u次){
$terms=explode(“\n”,$this->wordWrap($this->getTerms()),$row\u width,“\n”,true));
foreach($terms作为$term\u部分)
$out.=$indent.$term\u part.$newline;
$out.=$border;
}
退回$out;
}

间距永远不会起作用,因为管理代码非常困难

相反,将这些字段/值映射到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";

中获取的邮件代码。如果在两个元素之间给出多个空格,那么HTML只考虑单个空间。您可以使用Nbsp <代码>进行间距。另一个最好的方法是编写一个HTML表。

您可以使用这个

表。
<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>&nbsp;</td> <td>&nbsp;</td> <td>&nbsp;</td></tr>

   <tr>
     <td>Subtotal</td>  <td>&nbsp;</td>  <td>5050USD</td>
  </tr>

   <tr>
     <td>Tax</td>  <td>&nbsp;</td>  <td>50USD</td>
  </tr>

   <tr><td>&nbsp;</td> <td>&nbsp;</td> <td>&nbsp;</td></tr>

   <tr>
     <td>Total</td>  <td>&nbsp;</td>  <td>5100USD</td>
  </tr>



</table>

项目数量价格
5000美元是多少
50美元是多少
小计5050美元
税款50美元
总计5100美元

Im不允许使用html,如果我是的话,我从一开始就使用html。@CainNuke,告诉你的老板或你的团队领导html在这里是非常必要的:),因为如果你使用手动间距,维护代码将非常困难!嗨,不幸的是这不是关于某人的命令。脚本本身已经是这样做的。Im对不起,不允许使用html。我必须用纯文本,这就是问题所在。