Page 1 of 1

How to include multiple variables within the body

Posted: Fri Apr 10, 2009 11:06 pm
by JIAM
Hi guys,

I have a simple website where customers can submit a form with some basic data and request a quote from a variety of suppliers. The form has several fields that I need to include in the body of the email (the content of these fields change every time the form is submitted).

In the end I would like to have the final email look something like:

Code: Select all

/*
Good day [b]$supplier_name[/b]:
 
You have received a quote request:
 
Product A: [b]$product_a_quantities[/b]
Product B: [b]$product_b_quantities[/b]
Customer: [b]$customer_name[/b]
email: [b]$customer_email[/b]
 
Thank you.
*/
 
The email is sent and received as I want to, but I'm having trouble including several variables in the body field. I've searched several forum posts but I'm not sure if I should use the Decorator or if there's a simpler way to pull these variables into the body.

I'm still learning PHP and new to Swiftmailer, so I will greatly appreciate any input/direction.

Juan

Re: How to include multiple variables within the body

Posted: Fri Apr 10, 2009 11:17 pm
by Chris Corbyn
Can you post the code that you've used so far? We'll try to adapt it from that.

Re: How to include multiple variables within the body

Posted: Sat Apr 11, 2009 1:09 am
by JIAM
Thanks Chris. This is what I got right now.

Code: Select all

 
                    <?php
                    
[color=#0000FF]/* I have 17 variables coming from a separate form that's pointing to this php file. I'm showing only three variables here for simplicity.*/[/color]
                    $toemailprov = $_POST['toemailprov'];
                    $nomprov = $_POST['nomprov'];
                    $comentario = $_POST['comentario'];                 
                    
                    require_once '../lib/swift_required.php';
                    $transport = Swift_SmtpTransport::newInstance('mail.******.com', 26) 
                      ->setUsername('*****')
                      ->setPassword('*****')
                      ;
                    $mailer = Swift_Mailer::newInstance($transport);
                    $message = Swift_Message::newInstance()
                      ->setSubject('Solicitud de cotización')
                      ->setFrom(array('cotizaciones@******n.com' => 'Cotizaciones ******.com'))
                      ->setTo(array($toemailprov))
                      ->setBcc(array('info@******.com' => '******.com'))
                      ->setBody('Gracias por utilizar el servicio de cotización en línea. Hemos enviado su solicitud a [b]$nompro[/b]v . A continuación el detalle:
 
- email: [b]$toemailprov[/b]
- comentario: [b]$comentario[/b]
 
Gracias por usar nuestro servicio.', 'text/plain');
                    $result = $mailer->send($message);
                    ?> 
[color=#0000FF]/*The content is much larger than what I include above, it has additional text + all variables in a list. The website is in spanish, you will see some words above in spanish. */[/color]
 
As I mentioned, the email works well, sent and received as expected, all content is showing when sent with the exception of the variables, which I don't know how to make them visible within the body.

Thanks and let me know if you would like to know any additional information.

Re: How to include multiple variables within the body

Posted: Sat Apr 11, 2009 7:00 pm
by Chris Corbyn
So really, you just need to concatenate the variables into the email. PHP uses dots to concatentate two strings together, or to concatenate variables into a string.

You drop out of the string by throwing in a double (or single) quote to match the opening one, then add a dot (which just means "append") and then add whatever you want to append (be it a variable, or another string) and so on...

http://au2.php.net/manual/en/language.o ... string.php

Code: Select all

<?php
                    
                    $toemailprov = $_POST['toemailprov'];
                    $nomprov = $_POST['nomprov'];
                    $comentario = $_POST['comentario'];                 
                    
                    require_once '../lib/swift_required.php';
                    $transport = Swift_SmtpTransport::newInstance('mail.******.com', 26) 
                      ->setUsername('*****')
                      ->setPassword('*****')
                      ;
                    $mailer = Swift_Mailer::newInstance($transport);
                    $message = Swift_Message::newInstance()
                      ->setSubject('Solicitud de cotización')
                      ->setFrom(array('cotizaciones@******n.com' => 'Cotizaciones ******.com'))
                      ->setTo(array($toemailprov))
                      ->setBcc(array('info@******.com' => '******.com'))
                      ->setBody('Gracias por utilizar el servicio de cotización en línea. Hemos enviado su solicitud a ' . $nomprov . ' . A continuación el detalle:
 
- email: ' . $toemailprov . '
- comentario: ' . $comentario . '
 
Gracias por usar nuestro servicio.', 'text/plain');
                    $result = $mailer->send($message);
                    ?>

Re: How to include multiple variables within the body

Posted: Sat Apr 11, 2009 11:58 pm
by JIAM
Ah! What a simple solution!

Thanks so much for taking the time to answer this basic question. I really appreciate it.

Juan