What is the best way to use Email Template in Zend/PHP - php

I am working on a website where Users create their accounts. I need to send email to users on many oceans. For example when signup, forgot password, order summary etc. I want to use emails templates for this. I need your suggestions for this. I want to use a way that If I change any email template or login in less time and changes.
I have thought about the following way:
I have a table for email templates like this:
id
emailSubject
emailBody
emailType
For example when user forgot password:
id:
1
emailSubject:
ABC: Link for Password Change
emailBody:
<table border=0>
<tr><td>
<b> Dear [[username]] <b/>
</td></tr>
<tr><td>
This email is sent to you in response of your password recovery request. If you want to change your password, please click the following link:<br />
[[link1]]
<br/>
If you don't want to change your password then click the following link:<br/>
[[link2]]
</tr></td>
<tr><td>
<b>ABC Team</b>
</td></tr>
</table>
emailType:
ForgotPassword
Prepare email data:
$emailFields["to"] = "user#abc.com";
$emailFields["username"] = "XYZ";
$emailFields["link1"] = "http://abc.com/changepassword?code='123'";
$emailFields["link2"] = "http://abc.com/ignorechangepasswordreq?code='123'";
$emailFields["emailTemplate"] = "ForgotPassword";
Now Pass the all fields to this function to send email:
sendEmail( $emailFields ){
// Get email template from database using emailTemplate name.
// Replace all required fields(username, link1,link2) in body.
// set to,subject,body
// send email using zend or php
}
I planed to use above method. Can you suggest better way or some change in above logic.
Thanks.

I'd use Zend_View. Store your templates in /views/email/EMAILNAME.phtml, create a Zend_View object with the required email template and pass it the required data.

Off the top of my head, so untested... but something similar should work:
$view = new Zend_View();
$view->setScriptPath( '/path/to/your/email/templates' );
$view->assign( $yourArrayOfEmailTemplateVariables );
$mail = new Zend_Mail();
// maybe leave out phtml extension here, not sure
$mail->setBodyHtml( $view->render( 'yourHtmlTemplate.phtml' ) );
$mail->setBodyText( $view->render( 'yourTextTemplate.phtml' ) );

As previously mentioned, Zend_View is the way. Here is how I do this:
$template = clone($this->view);
$template->variable = $value;
$template->myObject = (object)$array;
// ...
$html = $template->render('/path/filename.phtml');
Use Markdownify to covert to plain text:
$converter = new Markdownify_Extra;
$text = $converter->parserString($html);
Setup mail:
$mail = new Zend_Mail();
$mail->setBodyHtml($html);
$mail->setBodyText($text);
Then setup Transport and send.

Related

send HTML markup from Custom Field to the Email Campaign

I have an E-commerce website and is integrated to InfusionSoft
What currently happening is that...
Customer completes an order on my website and purchases, let say 2 products.
Upon Order Completion, Order Received tag is added and customer's info is added to InfusionSoft, and the Order Detail HTML Markup (which is generated on my website via code) is added to a Custom Field, ~_myCustomField~
There is already a campaign running which send simple Thank You Email to customer.
What I want is that I can send the Order Detail(which is stored in ~_myCustomField~) along with the Thank You Email
What I have tried that, I have added the custom field into Campaign's Email just like ~Contact._myCustomField~ but it sends the only HTML, not generated one!
_myCustomField contains for example
<table>
<tr>
<td>This is your order detail</td>
</tr>
</table>
Instead of saving the HTML into a custom field, you can just send the thank you email at that point.
You can use the email service to send an email:
https://developer.infusionsoft.com/docs/read/Email_Service
example:
$title = "template API test";
$category = "API Category";
$fromAddress = "from#infusionsoft.com";
$toAddress = "~Contact.Email~";
$ccAddress = "";
$bccAddresses = "";
$subject = "My Fancy Subject Line";
$textBody = "This is the text email body";
$htmlBody = "HTML"
$contentType = "Contact";
$mergeContext = "";
$tmpId = $app->addEmailTemplate($title, $category, $fromAddress, $toAddress, $ccAddress, $bccAddresses, $subject, $textBody, $htmlBody, $contentType, $mergeContext);
If you want to keep the interaction with the campaign, you can use the goal feature. Learn more about goals here.
You will be after the funnel service to see if a goal has been completed (such as an order) and send the Thank you email at that point.

Using CodeIgniter's word_censor() to replace values in a file

I would like to know if this is the best practice with CI or if there is some better way.
In my project I am sending a confirmation email during the registration process. I have a HTML and TXT template. They both consist of something like this:
Hello {USERNAME}, please click on the activation link below to finish the registration. Link: {LINK}
And in my method I open the templates using the file helper and then replace the {} strings with the actual values using the text helper's word_censor() method like this:
$tmpName = '{USERNAME}';
$tmpLink = '{LINK}';
$name = 'Jon' //retrieved from registration from
$link = 'mysite.com/activate/239dwd01039dasd' //runs the activate function providing the unique ID as an argument
$template = word_censor($template, $tmpName, $name);
$template = word_censor($template, $tmpLink, $link);
return $template
Then I just take the $template and put it inside the CI's mail helper like this:
$this->email->message($template);
What I would like to know is if this is the best way to replace contents of html/txt files with my own values or if there is any better and more efficient way to achieve the same result. I just don't like that I am using the word_censor() function to do something other than what it was intended for..
The better way is to store the email template as a view file.
view
Hello <?php echo $username; ?>, please click on the activation link below to finish the registration. Link: <?php echo $link; ?>
Controller
$data['username'] = 'Jon';
$data['link'] = 'mysite.com/activate/239dwd01039dasd';
$email_template = $this->load->view('myfile', $data, true);
$this->email->message($email_template);
Setting the third parameter on view() to true will return the template rather then echo it out.

wordpress contact form 7 to sync to mailchimp list using api

I am trying to send contact form 7 data to mailchimp list. So far this works well following this tutorial http://www.limecanvas.com/a-mailchimp-opt-in-field-for-contact-form-7/
I am trying to modify the php to collect a telephone number and post that as a merge tag into the mailchimp list.
function wpcf7_send_to_mailchimp($cfdata) {
$formtitle = $cfdata->title;
$formdata = $cfdata->posted_data;
// Opt-in field checked?
if ( $formdata['mailchimp-optin'] ) {
$names = explode(' ',trim($formdata['first-name']));
$firstName = $names[0];
$lastName = '';
if (count($names)>1){
// more than one word in name field
$lastName = array_pop($names);
}
$send_this_email = $formdata['your-email'];
$mergeVars = array(
'FNAME'=>$firstName,
'LNAME'=>$lastName
);
// MCAPI.class.php needs to be in theme/includes folder
require_once('core/includes/MCAPI.class.php');
// grab an API Key from http://admin.mailchimp.com/account/api/
$api = new MCAPI('apikey');
// grab your List's Unique Id by going to http://admin.mailchimp.com/lists/
// Click the "settings" link for the list - the Unique Id is at the bottom of that page.
$list_id = 'listid';
// Send the form content to MailChimp List without double opt-in
$retval = $api->listSubscribe($list_id, $send_this_email, $mergeVars, 'html', false,true);
}
}
add_action('wpcf7_mail_sent', 'wpcf7_send_to_mailchimp', 1);
I have setup the form field in mailchimp and am trying this:
$telephone = $formdata['you-tell'];
and adding the merge tag to the mergetag array:
'TELL'=>$telephone
I am not a php guy (more comfortable with jquery) so I might be approaching this wrong?
essentially I need to extract the data from contact form 7 and add to the mailchimp mergetag array.
Thanks for the pointers
MailChimp for WordPress does just that. It uses the MailChimp API in combination with the WordPress HTTP API which makes it less error prone.
The following Contact Form 7 template will feed the entered values to your MailChimp list.
[text* mc4wp-FNAME]
[text* mc4wp-LNAME]
[tel mc4wp-TELL]
[mc4wp_checkbox "Sign-up to our newsletter."]
Hope that helps!
ok, Figured it out... really simple:
$tell = $formdata['your-tell'];
$mergeVars = array(
'FNAME'=>$firstName,
'LNAME'=>$lastName,
'TELL'=>$tell
);
Ammended these lines

Best way to use an email signature with CodeIgniter Email Class

How can I add an signature to emails I send using CodeIgniter's Email Class, for example add the following at the bottom of each email:
Joe Bloggs, Company, City, Tel Number
I'd use views for this functionality.
In your views, you can add your signatures directly:
// views/email_message.php
<h1>My Email Message</h1>
<p>Content</p>
<hr />
<p>Joe Bloggs, Company, Blah</p>
Or, better yet, I'd create a signature view and pass the data to it:
// views/email_message.php
<?= $this->load->view('email/message', $message_data(), TRUE) ?>
<?= $this->load->view('email/signature', $signature_data(), TRUE) ?>
Either way, you can pass this to your email message:
// in your controller
$message = $this->load->view('views/email_message', $data(), TRUE);
// configure email options, etc.
...
$this->email->message($message);
// send the email
I would do something like this for simplicity:
$this->email->message('Whatever your message is.' . '\n\n Joe Bloggs, Company, Blah');

How can I add HTML formatting to 'Swift Mail tutorial based' PHP email?

I have developed a competition page for a client, and they wish for the email the customer receives be more than simply text. The tutorial I used only provided simple text, within the 'send body message'. I am required to add html to thank the customer for entering, with introducing images to this email.
The code is:
//send the welcome letter
function send_email($info){
//format each email
$body = format_email($info,'html');
$body_plain_txt = format_email($info,'txt');
//setup the mailer
$transport = Swift_MailTransport::newInstance();
$mailer = Swift_Mailer::newInstance($transport);
$message = Swift_Message::newInstance();
$message ->setSubject('Thanks for entering the competition');
$message ->setFrom(array('info#examplemail.com' => 'FromEmailExample'));
$message ->setTo(array($info['email'] => $info['name']));
$message ->setBody('Thanks for entering the competition, we will be in touch if you are a lucky winner.');
$result = $mailer->send($message);
return $result;
}
This function.php sheet is working and the customer is recieving their email ok, I just need to change the
('Thanks for entering the competition,
we will be in touch if you are a lucky
winner.')
to have HTML instead...
Please, if you can, provide me with an example of how I can integrate HTML into this function.
You can also just add 'text/html' to setBody (ref):
->setBody($this->renderView('YouBundleName:Default:email.html.twig'), 'text/html');
$message = Swift_Message::newInstance();
$message->setContentType('text=html');
Will the same text remain or should it be styled somehow?
Editing your emails in html requires inline css styles eg:
('<p style="font-size:1.2em; color:#f0f0f0;">Thanks for entering the competition, we will be in touch if you are a lucky winner.</p>')
if you need a table just add:
('<table style="font-size:1.2em; color:#f0f0f0;"><tr><td>Thanks for entering the competition, we will be in touch if you are a lucky winner.</td></tr></table>')
or to make it more simpler
$message_body'
<table style="font-size:1.2em; color:#f0f0f0;">
<tr>
<td>Thanks for entering the competition, we will be in touch if you are a lucky winner.</td>
</tr>
</table>
';
$message ->setBody($message_body);
I know that when I need to send html emails I need to set the content-type to html which I believe you did on the following line
$body = format_email($info,'html');
I hope this is what you were looking for. if not let me know

Categories