Page 1 of 1

Swiftmailer Variable in setSubject

Posted: Wed Sep 21, 2016 4:03 pm
by Sabo
Hi All,

I am using SwiftMailer 5.4.3 and am having an issue with posting a variable into the subject line. I am trying to post the last name of the form submitter dynamically into the email so it can be identified easily. I have the variable insertion this working for the "CC:" recipient using php explode, but I cannot get it to work for the subject line. When the email is sent there is no subject. Here's my code.
Thanks for any input

Sabo

Code: Select all

<?php


/* This is the email portion of the Customer Complaint web form. filemame = send.php
 * The email transport used is Swiftmailer http://swiftmailer.org you must include the LIB folder 
 * in the root directory of this site.
 * to change the outgoing email use the $transport function
 * to change the email content define the string operator(s) in the $mailer function then edit the $message function 'body'
 * 
 */


// Load  the SwiftMailer library 

require_once 'lib/swift_required.php';

// Create the swiftmail transport 

$transport = Swift_SmtpTransport::newInstance('smtp.gmail.com', 465, 'ssl')
  ->setUsername(example@example.com')
  ->setPassword(somepassword);

// Create the swiftmail mailer using the transport created above 

$mailer = Swift_Mailer::newInstance($transport);

/* define the variables to import into the message 
*  This is where we pull the info from the HTML form into the message, 
* any definitions in the form need to match exactly here */

$submitterName = $_POST['submitterName'];
$submitterEmail = $_POST['submitterEmail'];
$firstName = $_POST['firstName'];
$lastName = $_POST['lastName'];
$customerPhone = $_POST['customerPhone'];
$streetAddress = $_POST['streetAddress'];
$cityTown = $_POST['cityTown'];
$state = $_POST['state'];
$zipCode = $_POST['zipCode'];
$complaintDate = $_POST['complaintDate'];
$complaintDetails = $_POST['complaintDetails'];
$firstResponse = $_POST['firstResponse'];
$suspectedCause = $_POST['suspectedCause'];
$actionTaken = $_POST['actionTaken'];
$considerationsOfCause = $_POST['considerationsOfCause'];
$dateOfCorrection = $_POST['dateOfCorrection'];
$status = $_POST['status'];
//the following variables are used to import the submitter email address and submitters last name
//I could not get SwiftMailer to take a variable directly from POST so we must explode the contents first
//"explode" is used to grab the contents of the original variable into an array.

    $cc = $_POST['submitterEmail'];
    $ccArr = explode ("," , $cc );
    $userCC = array_values($ccArr);
    
    $subjectName = $_POST['lastName'];
    $subjectArr = explode("," , $subjectName);
    $subjectData = array_values($subjectArr);
    

// Create the email message 

$message = Swift_Message::newInstance()
  ->setContentType('text/html')
    ->setEncoder(Swift_Encoding::getBase64Encoding())
  -> setSubject ($subjectData)
  ->setFrom(array(example@example.com' => 'your Name'))
  ->setTo(array('receiverEmailAddress@email.com' => 'Receiver Name'))
  //->setcc($ccArr)
  ->setcc($userCC)
  ->setBody(  
 '<h2 style="color:green;text-align:center;">Customer Complaint Submission</h2>'. "<br>"
    .'<strong>Submitter Name:</strong>  '  ."$submitterName"."<br>"
    .'<strong>Submitter eMail:</strong>  ' ."$submitterEmail" ."<br>"    
    .'<h3 style="color:green;">Customer Information:</h3>'
        .'<strong>First Name:</strong> ' ."$firstName"."<br>"
        .'<strong>Last Name:</strong>  ' ."$lastName" . "<br>"
        .'<strong>Customer Phone:</strong> ' .$customerPhone ."<br>"
        .'<strong>Street Address:</strong> ' .$streetAddress ."<br>"
        .'<strong>City:</strong> ' .$cityTown ."<br>"   
        .'<strong>State:</strong> ' .$state ."<br>"
        .'<strong>Zip Code:</strong> ' .$zipCode ."<br> <br>"
            .'<h3 style="color:green;">Complaint Information:</h3>'
                .'<h4 style="color:green;">Complaint Date:</h4> ' .$complaintDate ."<br>"
                .'<h4 style="color:green;">Complaint Details:</h4> ' . $complaintDetails ."<br>"
                .'<h4 style="color:green;">First Response:</h4> ' . $firstResponse ."<br>"
                .'<h4 style="color:green;">Suspected Cause:</h4> ' . $suspectedCause ."<br>"
                .'<h4 style="color:green;">Action Taken:</h4> ' . $actionTaken ."<br> <br>"
                .'<h4 style="color:green;">Considerations of Cause:</h4> ' . $considerationsOfCause ."<br>"
                .'<h4 style="color:green;">Date of Correction:</h4> ' . $dateOfCorrection ."<br>"
                .'<h4 style="color:green;">Status:</h4> ' . $status );


// Verification of email sent or failure 
// If the script fails it probably won't reach this point and the failure message will not display. Dont' panic.

$result = $mailer->send($message);

if ( $result > 0 )
{
    ECHO "<img alt='Logo' src='Logo.png'>";
    ECHO "<br><br><br><h3>Thank you for submitting your Customer Complaint form.</h3><br>" ;
    ECHO "<h3>Your form has been sent to the appropriate managers.</h3><br>";
    ECHO "<h3>A copy of your submission has been emailed to the address you provided.</h3><br>";
    ECHO "<h4>You can safely close this window on your browser</h4>";
}
else
{
    ECHO "Sorry... something went wrong, please check the info you provided.\n";
}

?>

Re: Swiftmailer Variable in setSubject

Posted: Wed Sep 21, 2016 4:36 pm
by Celauran
Mismatched quotes notwithstanding, you're passing an array to setSubject rather than a string.

https://github.com/swiftmailer/swiftmai ... hp#L65-L79

Re: Swiftmailer Variable in setSubject

Posted: Thu Sep 22, 2016 3:53 pm
by Sabo
Thanks for the input, I 'm relatively new to programming so please forgive the format errors. Can you give an example of something that will work? I will need to have some subject text like "Complaint received from - (last name), where the last name is pulled from the form.

Thanks,

Sabo

Re: Swiftmailer Variable in setSubject

Posted: Thu Sep 22, 2016 4:15 pm
by Celauran
What does $_POST['lastName'] look like? If it's just a string with the user's last name, you could do something like this:

Code: Select all

$message = Swift_Message::newInstance()
    ->setSubject("Complaint received from - {$_POST['lastName']}")
    ->setTo(...)
    // etc

Re: Swiftmailer Variable in setSubject

Posted: Thu Sep 22, 2016 4:22 pm
by Sabo
Celauran, I appreciate the help, I resolved the problem once you pointed me in the right direction. Thanks!

removed these lines:
$subjectName = $_POST['lastName'];
$subjectArr = explode("," , $subjectName);
$subjectData = array_values($subjectArr);

Added this line:(double quoted string to expand variable in curly braces)

-> setSubject ("Customer Complaint - ${lastName}")

Re: Swiftmailer Variable in setSubject

Posted: Thu Sep 22, 2016 4:23 pm
by Celauran
Cool. I'm glad you got it sorted.