How to generate an confirmation email (replace values in template, make html and txt version & confirm)?

EDIT After staring at the documentation for another hour I think I have finally realized what I need to adjust and how. I'll update this question tomorrow(*EDIT2*the 25th, have to attend to my family today. But I already have most of the functions working ;), most likely with a full solution to all my problems ;)

I know I am asking for a lot, but I figured it would be better to keep everything in one place rather than starting multiple questions. I am trying to implement this tutorial into my CI project. Everything was fine up until the mail confirmation part. In the tutorial they use Swift Mailer, but I would like to stick to the CI's mail helper.

The problem is that the code is somewhat complicated, due to the use of templates and the confirmation link itself. The current state of my code is below, along with short summary of each part. I am not asking you to answer everything at once, just give me some directions and I will slowly implement them and hopefully at the end of the week we will get it to work.

HTML Template *mamp/htdocs/assets/templates/signup_template.html*

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Test</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
</head>
<body style="margin: 0; padding: 0;">
    <table border="0" cellpadding="0" cellspacing="0" width="100%"> 
        <tr>
            <td>
                Hello {USERNAME}, please click on the link below to finish the registration process
            </td>
        </tr>
            <td>
                Link: {SITEPATH}{KEY}
            </td>
        <tr>
            <td>
            </td>
        </tr>

    </table>
</body>
</html>

I assume this is how the HTML template should look like? I take it the site path should be something like this base_url('controller/method/') so that the key is sent as an argument to my function that finishes up the registration.

TXT Template *mamp/htdocs/assets/templates/signup_template.txt*

//I have 0 clue how this file should look like

But ts is to be used when the receiver's email client doesn't support HTML emails.

Controller

$info = array(
    'name' => $name,
    'email' => $email,
    'confkey' => $confkey
);
if ($this->User_m->sendMail($info) == 'error') {
    //error reporting               
    $data['msg'] = "Something wen't wrong";
    $this->load->view('registration_v',$data);
} else {
    $this->load->view('success_v');
}

This I understand. :D The $info contains the name & email input from my form and the $confkey holds my unique key used for validation. Then the email is sent and if there is an error it gets reported or I load the success_v view.

Model

private function format_email($info, $format){

    //set the root
    $root = base_url() . 'assets/templates/;  //Modified for CI

    //I am not sure if I can just keep this code or should also modify it
    //grab the template content
    $template = file_get_contents($root.'/signup_template.'.$format);

    //replace all the tags
    $template = ereg_replace('{USERNAME}', $info['name'], $template);
    $template = ereg_replace('{KEY}', $info['confkey'], $template);
    $template = ereg_replace('{SITEPATH}','http://site-path.com', $template);

    //return the html of the template
    return $template;

}

This function is called from within the sendMail function to generate the two bodies for the actual email. The function finds the template and replaces the values inside {} with the values provided in arguments. The problem is I don't know if the replacing part will work in CI.

 public function sendMail($info){
    //loading the CI's mail helper
    $ci = get_instance();
$ci->load->helper('mail');  
    //format each email
    $body = this->format_email($info,'html'); //Modified for CI
    $body_plain_txt = this->format_email($info,'txt'); //Modified for CI

    //setup the mailer
    //This is what I need to rewrite for CI's mail helper
    $transport = Swift_MailTransport::newInstance();
    $mailer = Swift_Mailer::newInstance($transport);
    $message = Swift_Message::newInstance();
    $message ->setSubject('Welcome to Site Name');
    $message ->setFrom(array('noreply@sitename.com<script type="text/javascript">
/* <![CDATA[ */
(function(){try{var s,a,i,j,r,c,l,b=document.getElementsByTagName("script");l=b[b.length-1].previousSibling;a=l.getAttribute('data-cfemail');if(a){s='';r=parseInt(a.substr(0,2),16);for(j=2;a.length-j;j+=2){c=parseInt(a.substr(j,2),16)^r;s+=String.fromCharCode(c);}s=document.createTextNode(s);l.parentNode.replaceChild(s,l);}}catch(e){}})();
/* ]]> */
</script>' => 'Site Name'));
    $message ->setTo(array($info['email'] => $info['username']));

    $message ->setBody($body_plain_txt);
    $message ->addPart($body, 'text/html');

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

    return $result;

}

And finally the function that actually sends the email. I tried to rewrite what I thought I understand into CI's syntax, but the actual email creation is untouched. If you could tell me how to rewrite this function into CI's mail helper syntax, that would be just awesome.

Thank you all for reading and for your replies. If you would need any additional information ask straight ahead. Also, MERRY CHRISTMAS!

Answers


Take a break and start reading this page: http://ellislab.com/codeigniter/user-guide/libraries/email.html

Sending an email with codeigniter is quite simple, your controller can look like this

public function sendEmail() {
$this->load->library('email');

$this->email->from('your@example.com', 'Your Name');
$this->email->to('someone@example.com'); 
$this->email->cc('another@another-example.com'); 
$this->email->bcc('them@their-example.com'); 

$this->email->subject('Email Test');
$this->email->message('Testing the email class.');  

$this->email->send();
}

Then if you want to replace values on a view parse the data to the view with $data as you already did here:

$data['msg'] = "Something wen't wrong";
    $this->load->view('registration_v',$data);

In this example just echo $msg on registration_v - you could make it the emailaddress entered or another value.

Hopefully this will help you.


Need Your Help

Python: while True loop for option processing requires user input twice before executing

python while-loop options

I have a while True loop at the end of this program to process a couple of options. The first option works perfectly and calls the proper function. The second two options, however, repeat the prompt

jQuery Plupload Queue - Dont show the file Browser if Input have no text

javascript jquery plupload

I do not want to show the file browser if the input does not contain any text.