Swift Mailer Variable Problems

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
gkwhitworth
Forum Commoner
Posts: 85
Joined: Tue Sep 05, 2006 8:28 pm
Location: Wasilla, Alaska

Swift Mailer Variable Problems

Post by gkwhitworth »

I went out and downloaded Swift Mailer and am trying to do two things.

1. Send an email to myself from a form that a user submits that shows the variables from the session.
2. Send that exact same email to the user that created the form.

1. I am currently recieving an email the way I want to, but it on says Name: $fname $lname

Code: Select all

<? session_start();
header("Location: http://www.gkwinspired.com");
require('../Swift/Swift.php');
require('../Swift/Swift/Connection/SMTP.php');

$mailer = new Swift(new Swift_Connection_SMTP('127.0.0.1'));
$recipients = array('contact@gkwinspired.com','$email');

if ($mailer->isConnected())

{
   $mailer->addPart ('This is plain text');
   $mailer->addPart ('
   <table width="637px" style="font: Arial 12px;">
       <tr height="105px">
           <td colspan="2"><img src="http://www.gkwinspired.com/images/template/top.jpg">
           </td>
       </tr>

       <div style="border-right: 1px solid #9ea9bf; border-left: 1px solid #9ea9bf;">
            <tr>
                <td width="25%" align="right" style="vertical-align: top">Name:<br>
                </td>

                <td width="75%" style="vertical-align: top">$fname $lname
                </td>
            </tr>
       </div>

       <tr height="65px">
           <td colspan="2"><img src="http://www.gkwinspired.com/images/template/bottom.jpg">
           </td>
       </tr>


   </table>','text/html');
   $mailer->send(
        $recipients,
        '"Greg Whiworth" <contact@gkwinspired.com>',
        'Web Order');

   $mailer->close();
}
else echo "The mailer failed to connect. Errors: <pre>".print_r($mailer->errors, 1)."</pre><br />
    Log: <pre>".print_r($mailer->transactions, 1)."</pre>";

?>
2. As you can see I am using the session to register the second email address but the second email isn't being sent. Should I be using something different for this scenario?
User avatar
aaronhall
DevNet Resident
Posts: 1040
Joined: Tue Aug 13, 2002 5:10 pm
Location: Back in Phoenix, missing the microbrews
Contact:

Post by aaronhall »

PHP interprets text literally when passed between single quotes. Try using double quotes instead. This script demonstrates the difference.
User avatar
gkwhitworth
Forum Commoner
Posts: 85
Joined: Tue Sep 05, 2006 8:28 pm
Location: Wasilla, Alaska

Post by gkwhitworth »

I have tried everything under the sun to get this dumb thing to work. I have used double quotes and single quotes, I have include the list of variables so the variable in the html message no what I am talking about, it just seems that swift has a problem with sessions.

--
Greg
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

I seriously doubt Swift has any problems with sessions.

Aside from the session_start() call, the code posted above doesn't appear to reference sessions.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Swift doesn't use sessions, it's all stream based.

The single quotes would break it yes:

Code: Select all

<?php

session_start();
header("Location: http://www.gkwinspired.com");

require('../Swift/Swift.php');
require('../Swift/Swift/Connection/SMTP.php');

$mailer = new Swift(new Swift_Connection_SMTP('127.0.0.1'));
$recipients = array('contact@gkwinspired.com', $email);

if ($mailer->isConnected())

{
   $mailer->addPart ('This is plain text');

    $body = <<<EOD
   <table width="637px" style="font: Arial 12px;">
       <tr height="105px">
           <td colspan="2"><img src="http://www.gkwinspired.com/images/template/top.jpg">
           </td>
       </tr>

       <div style="border-right: 1px solid #9ea9bf; border-left: 1px solid #9ea9bf;">
            <tr>
                <td width="25%" align="right" style="vertical-align: top">Name:<br>
                </td>

                <td width="75%" style="vertical-align: top">$fname $lname
                </td>
            </tr>
       </div>

       <tr height="65px">
           <td colspan="2"><img src="http://www.gkwinspired.com/images/template/bottom.jpg">
           </td>
       </tr>


   </table>
EOD;

   $mailer->addPart ($body,'text/html');
   $mailer->send(
        $recipients,
        '"Greg Whiworth" <contact@gkwinspired.com>',
        'Web Order');

   $mailer->close();
}
else echo "The mailer failed to connect. Errors: <pre>".print_r($mailer->errors, 1)."</pre><br />
    Log: <pre>".print_r($mailer->transactions, 1)."</pre>";

?>
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

You can't blindly switch the single quotes with double quotes, all internal doublequotes would then have to be replaced. Use d11wtq's code and use heredoc syntax. Personally, I'd move the HTML code to its own file, load it in, and then perform str_replace on the variables (or run it through a templating system).
User avatar
gkwhitworth
Forum Commoner
Posts: 85
Joined: Tue Sep 05, 2006 8:28 pm
Location: Wasilla, Alaska

Post by gkwhitworth »

It actually does work with sessions and I am getting emails perfectly fine, loud and clear.

Code: Select all

<? session_start();
header("Location: http://www.gkwinspired.com");
require('../Swift/Swift.php');
require('../Swift/Swift/Connection/SMTP.php');

$mailer = new Swift(new Swift_Connection_SMTP('127.0.0.1'));
$recipients = array ('contact@gkwinspired.com','.$email.');

if ($mailer->isConnected())

{
   $mailer->addPart ('This is plain text');
   $mailer->addPart ('
   
	<link rel="stylesheet" type="text/css" href="http://www.gkwinspired.com/Styles/fonts.css">
	<link rel="stylesheet" type="text/css" href="http://www.gkwinspired.com/Styles/sub_temps.css">
	<link rel="stylesheet" type="text/css" href="http://www.gkwinspired.com/Styles/nonFont.css">
	<link rel="stylesheet" type="text/css" href="http://www.gkwinspired.com/Styles/contact.css">

   
   <table width="637px" class="normal" cellspacing="0" cellpadding="3" align="center">
   
      <tr height="105px">
           <td colspan="2"><img src="http://www.gkwinspired.com/images/template/top.jpg">
           </td>
       </tr>
       
          	<tr>
                <td colspan="2"><h2>Personal Information</h2>
                </td>
            </tr>

	         <tr >
                <td width="10%" align="right" class="normal">Name:
                </td>

                <td width="90%" class="normal">'.$fname.' '.$lname.'
                </td>
            </tr>
            
            <tr>
                <td width="10%" align="right" class="normal">Email:
                </td>

                <td width="90%" class="normal">'.$email.'
                </td>
            </tr>
            
             <tr >
                <td width="10%" align="right" class="normal">Phone:
                </td>

                <td width="90%" class="normal">'.$phone.'
                </td>
            </tr>
            
            <tr>
                <td width="10%" align="right" class="normal">Address:
                </td>

                <td width="90%" class="normal">'.$address.'
                </td>
            </tr>
            
            <tr >
                <td width="10%" align="right" class="normal">City:
                </td>

                <td width="90%" class="normal">'.$city.'
                </td>
            </tr>
            
            <tr>
                <td width="10%" align="right" class="normal">State:
                </td>

                <td width="90%" class="normal">'.$state.'
                </td>
            </tr>
            
            <tr >
                <td width="10%" align="right" class="normal">Zip:
                </td>

                <td width="90%" class="normal">'.$zip.'
                </td>
            </tr>
            
            <tr>
                <td colspan="2"><h2>Order Information</h2>
                </td>
            </tr>
            
             <tr>
                <td width="10%" align="right" class="normal">Package:
                </td>

                <td width="90%" class="normal">'.$package.'
                </td>
            </tr>
            
             <tr >
                <td width="10%" align="right" class="normal">Flash:
                </td>

                <td width="90%" class="normal">'.$flash.'
                </td>
            
            </tr>
            
             <tr>
                <td width="10%" align="right" class="normal">PHP:
                </td>

                <td width="90%" class="normal">'.$phpForm.'
                </td>
            </tr>
            
             <tr >
                <td width="10%" align="right" class="normal">ASP:
                </td>

                <td width="90%" class="normal">'.$asp.'
                </td>
            
            </tr>
            
             <tr>
                <td width="10%" align="right" class="normal">CMS:
                </td>

                <td width="90%" class="normal">'.$cms.'
                </td>
         	</tr>
            
           <br>           
           <tr >
                <td width="10%" align="right" class="normal">Estimate:
                </td>

                <td width="90%" class="normal">'.$answer.'
                
                </td>
            </tr>          
   			<br>
           	<br>
       <tr height="65px">
           <td colspan="2"><img src="http://www.gkwinspired.com/images/template/bottom.jpg">
           </td>
       </tr>

   </table>','text/html');
   $mailer->send(
        $recipients,
        '"Greg Whiworth" <contact@gkwinspired.com>',
        'Web Order');

   $mailer->close();
}
else echo "The mailer failed to connect. Errors: <pre>".print_r($mailer->errors, 1)."</pre><br />
    Log: <pre>".print_r($mailer->transactions, 1)."</pre>";

?>
How would you go about subbing the html out into another file, like you were talking about?

--
Greg
User avatar
aaronhall
DevNet Resident
Posts: 1040
Joined: Tue Aug 13, 2002 5:10 pm
Location: Back in Phoenix, missing the microbrews
Contact:

Post by aaronhall »

Put the HTML into a separate file, and in that file, put the HTML into a variable using HEREDOC (search the manual if you're unfamiliar with it). In the original file (the one you posted), include that second file before the Swift::addPart method is called. Replace the HTML in the your original file's $mailer->addPart call with the variable name in the second file.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Code: Select all

'.$email.'
That's wrong. Are you using an editor which does syntax highlighting?
User avatar
gkwhitworth
Forum Commoner
Posts: 85
Joined: Tue Sep 05, 2006 8:28 pm
Location: Wasilla, Alaska

Post by gkwhitworth »

Code: Select all

'.$email.'
It is kinda frustrating.....but that is the only way I can get my other variables to work....but my host uses MS Sharepoint, which is kinda nice but it makes it so I have to edit my files within Frontpage. I do use HAPEdit for most of my editing though. It is just quicker when I have one period to change to do it within FP. I own Dreamweaver as well and I am planning on setting up FTP on my site so I can use that religously, none of this FP crap.

--
Greg
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

gkwhitworth wrote:

Code: Select all

'.$email.'
It is kinda frustrating.....but that is the only way I can get my other variables to work....
:? I think you should post the code where the "other variables" don't work. All you need to do is change:

Code: Select all

'.$email.'
to just:

Code: Select all

$email
User avatar
gkwhitworth
Forum Commoner
Posts: 85
Joined: Tue Sep 05, 2006 8:28 pm
Location: Wasilla, Alaska

Post by gkwhitworth »

I did that already. I had to join them together in order for them to work right....don't ask me why. If I don't put quotes around them all I get is this:
Name: $fname $lname
Oh well, it works. I will post the code at lunch time. Talk to you guys later

--
Greg
Post Reply