0

I am trying to make the body of the mail with this [huge concatenation]

$body='<table  border=1>'.'<tr>'.'<th>Description</th>'.'<th>Quantity</th>'.'<th>Cost</th>'.'</tr>';
for($x=0;$x<=$length-1;$x++)
    {
    $body .='<tr>'.'<td>'.$json2[$x]["description"].'</td>'.'<td>'.$json2[$x]["qty"].'</td>'.'<td>'.$json2[$x]["cost"].'</td>'.'</tr>';
    }

$body.='</table>';

And send it using Swift Mailer

$message = Swift_Message::newInstance('Thanks for your Order')
->setFrom(array('abc@gmail.com' => 'Sender')) 
->setTo(array('xzy@gmail.com' => 'Receiver Name')) 
->setBody($body,'html');

The mail is received successfully but I the table is not seen.

For Feroz

 $mailer = Swift_Mailer::newInstance($transport);
 $message = Swift_Message::newInstance('DialSwap - Thanks for your Order')
 ->setFrom(array('rorrykeys2@gmail.com' => 'DialSwap')) 
 ->setTo(array('rorrykeys@gmail.com' => 'Receiver Name')) 
 ->setBody($body,'text/html');
 $headers = $message->getHeaders();
 $headers->addTextHeader('Content-Type', 'text/html');
shivram
  • 469
  • 2
  • 10
  • 26
  • 1
    Look at your message source and see if data is there. Some times this kind of behavior is caused by broken headers of message. – makallio85 Mar 23 '14 at 15:55
  • just checked it up. It attaches the contents of $body as a file which i am able to download. What should be done , for it to work. – shivram Mar 23 '14 at 15:59
  • Yes, that means message structure is broken. You should read manual of SwiftMailer more carefully. Unfortunately, I am not very familiar with it. – makallio85 Mar 23 '14 at 16:03

2 Answers2

2

html is not a valid Content-Type:

->setBody($body,'html');

You probably want text/html.

You can find some usage examples in the Creating Messages chapter.

Álvaro González
  • 142,137
  • 41
  • 261
  • 360
1

you can add the headers like below

$message = Swift_Message::newInstance();

$headers = $message->getHeaders();

$headers->addTextHeader('Content-Type', 'text/html');
ɹɐqʞɐ zoɹǝɟ
  • 4,342
  • 3
  • 22
  • 35
  • i do not think you got my question. I want to send a table as a message which is contained in the $body variable. – shivram Mar 23 '14 at 16:07
  • if you dont see any table in your received message, and raw HTML data it means you mail headers are not set. So you have to mention the mail server that this message is an HTML type message – ɹɐqʞɐ zoɹǝɟ Mar 23 '14 at 16:09
  • is this okay? $mailer = Swift_Mailer::newInstance($transport); $message = Swift_Message::newInstance('DialSwap - Thanks for your Order') ->setFrom(array('rorrykeys2@gmail.com' => 'DialSwap')) ->setTo(array('rorrykeys@gmail.com' => 'Receiver Name')) ->setBody($body,'text/html'); $headers = $message->getHeaders(); $headers->addTextHeader('Content-Type', 'text/html'); – shivram Mar 23 '14 at 16:17
  • why dont you simply test it by sending the mail :? – ɹɐqʞɐ zoɹǝɟ Mar 23 '14 at 16:18
  • what are getting in your mail? – ɹɐqʞɐ zoɹǝɟ Mar 23 '14 at 16:21
  • got it feroz. thank you. Like you said it was the header problem. just changed html to text/html. thanks for the help! – shivram Mar 23 '14 at 16:23