Welcome Guest, Not a member yet? Register   Sign In
styling output of emailing
#1

[eluser]brian88[/eluser]
So I use the typical email code like this

Quote:if( $this->input->post() ){
$this->load->library('email');
$this->email->from( $this->input->post('email'), $this->input->post('name'));
$this->email->to('[email protected]');
$this->email->subject('Email Test');
$this->email->message(
"Name: " . $this->input->post("name") . "\n" .
"Email: " . $this->input->post("email") . "\n" .
"Message: " . $this->input->post("message")
);
$this->email->send();
}

And you get an email plain and simple
Name: yourname
Email: [email protected]
Message: your message...

Is there anyway to style this output better so it looks better on an email client such as gmail?
#2

[eluser]Matalina[/eluser]
send as html and use html to format.

You'll have to change the config of the email library to send html.

http://ellislab.com/codeigniter/user-gui...email.html check email preferences to set up the config file.
#3

[eluser]rip_pit[/eluser]
the email lib can use views as "template".
So first prepare an html template in the views like this

/application/views/email_tpl_html.php
Code:
<html>
  <head>
    <style>/*your custom css styles*/</style>
  </head>
  <body>
    <h1>E-Mail from &lt;?php echo $sitename;?&gt;</h1>
    <p>click here to go to the website &lt;?php echo $siteurl ?&gt;.</p>
  &lt;/body&gt;
&lt;/html&gt;

Then load it with something like :
Code:
$view_data = array('sitename'=>'My website','siteurl'=>'mysite.com');
$this->load->library('email');
$this->email->from('user@mail', 'user name');
$this->email->reply_to('user@mail');
$this->email->to('dest@mail');
$this->email->subject('subject');
$this->email->message($this->load->view('email_tpl_html.php', $view_data, TRUE));
//if you want text only alternative included, enable and prepare another text only tpl:
//$this->email->set_alt_message($this->load->view('email_tpl_txt.php', $view_data, TRUE));
$this->email->send();
#4

[eluser]brian88[/eluser]
thanks fellas. got it to work
#5

[eluser]mact1079[/eluser]
Any idea why I would get this error? When I have the template in views/email_templates/welcome.php?

Severity: 4096
Message: Object of class CI_Email could not be converted to string
Filename: libraries/Email.php
Line Number: 383

Code:
$data = array('sitename'=>'Welcome','siteurl'=>'mydomain.com');
$message = $this->email->message($this->load->view('email_templates/welcome', $data, TRUE));
$this->email->from('[email protected]', 'Site'); $this->email->to($this->session->userdata('email'),$this->session->userdata('firstName') . " " . $this->session->userdata('lastName'));
$this->email->subject('Welcome, ' . $this->session->userdata('firstName'));
$this->email->message($message);
$this->email->send();

This is the content of welcome.php

Code:
&lt;html&gt;
&lt;body&gt;
<p>This is a test</p>

&lt;/body&gt;
&lt;/html&gt;
#6

[eluser]xerobytez[/eluser]
Change the code on line 2 from.

Code:
$message = $this->email->message($this->load->view('email_templates/welcome', $data, TRUE));

to:

Code:
$message = $this->load->view('email_templates/welcome', $data, TRUE);
#7

[eluser]mact1079[/eluser]
Wow. I'm an idiot. Seriously sorry for wasting your time on that one!




Theme © iAndrew 2016 - Forum software by © MyBB