Using a HTML Mail template with codeigniter - php

I am using the codeigniter email class to send emails when the user register to my website or perform some activity inside my website.
The thing is that, with this simple code below I can only send very simple HTML emails. And also I will have to pass the entire HTML message to the function. Is there any way so that I could load the entire template in the function itself and just pass a message which is to be placed inside the template as we do in codeigniter with views.
$message = 'Thank you for your order \n';
$message .= 'We will contact you soon';
$this->email($message);
public function email($message = NULL){
$this->load->library('email');
$this->email->mailtype('html');
$this->email->from('abbbba#gmail.com', 'Sohan Kc');
$this->email->to('abbbbaa#gmail.com');
$this->email->subject('Email Test');
$this->email->message($message);
$this->email->send();
}

Why don't you just do as you already stated? :)
The email classes message method requires a string to be passed. Just load a View and pass the returned string to the method.
public function email($message = NULL){
$this->load->library('email');
$mydata = array('message' => 'I am a message that will be passed to a view');
$message = $this->load->view('my_email_template', $mydata, true);
$this->email->mailtype('html');
$this->email->from('sohanmax2#gmail.com', 'Sohan Kc');
$this->email->to('sohanmax02#gmail.com');
$this->email->subject('Email Test');
$this->email->message($message);
$this->email->send();
}

I send emails but got the html code as body, I had to add the headers to get correct content like below and it will works out.
$this->email->set_header('MIME-Version', '1.0; charset=utf-8');
$this->email->set_header('Content-type', 'text/html');

Related

Sending multiple emails in YII using YiiMail extension

I am using YiiMail Extension to send mails.
I have my default contact.php file as my view. I am able to send mails for individuals but multiple emails are not allowed here.
mycontroller-
public function actionContact()
{
$model=new ContactForm;
if(isset($_POST['ContactForm']))
{
$message = new YiiMailMessage;
$message->Body=$_POST['body'];
$message->subject = $_POST['subject']
$message->addTo($_POST['email']);
$message->from = "frommail#gmail.com";
if(Yii::app()->mail->send($message) )
echo 'mail sent';
else
echo 'error while sending email';
}
}
I have tried the following too-
foreach ($model as $value)
{
$message->addTo($model[$value]);
}
It does not accept multiple email id's. How can this be resolved?
I have checked code in Yii Mailer. They haven't given such facility. If you like to achieve then you need to extend Yii Mail. It's ultimately useless because looping in your new extended class OR looping in your controller are same.
Regards...
You can pass multiple recipients mail ids as array
$recipients = array('test1#example.com','test2#example.com','test3#example.com');
$message->addTo('test1#example.com');
foreach($recipients as $email) {
$mail->AddBCC($email); // if you want more than one email
}

Sending html email in Codeigniter

I am sending a template html in codeigniter and in fact my email is working perfectly fine.
My problem is how i send a template of html to the email. I got an error because of the variables passed to.
public function send_email_accountability($C11, $C4, $C8,$C12)
{
date_default_timezone_set('Asia/Manila');
$message = $this->load->view('sample_html',$data,TRUE);
$this->load->library('email');
$this->email->set_mailtype("html");
$this->email->from('noreply#email.com', 'EMAIL');
$this->email->to($C11);
$this->email->subject('Accountability for'. $C12);
$this->email->message($message);
}
I receive the email but inside the email is php error because the $data cannot be passed to. I looked at other stackoverflow problems and it didn't help my problem.
public function send_email_accountability($C11, $C4, $C8,$C12)
{
date_default_timezone_set('Asia/Manila');
$this->load->library('email');
$data['title']='HELLO WORLD';
$this->email->from('noreply#email.com', 'EMAIL');
$this->email->to($C11);
$this->email->subject('Accountability for'. $C12);
$this->email->set_mailtype("html");
$msg = $this->load->view('sample_html',$data,TRUE);
$this->email->message($msg);
$this->email->send();
}
Try this one
use PHPmailer. It will help You. I also use it. You can get all information about PHPmailer from here
you are using a data array ($data) to the view, but no values is assigned to it,
just assign any value to array to avoid the error,
$data['logo_img'] = 'images/logo.png'; // add this line before loading the view
This logo image is located as $logo_img in the view sample_html...

codeigniter email weirdness

I am trying to send email via codeigniter framework via 2 different ways and on one of them my email goes to junk folder but its really weird...
so my first try was to create a Controller and put inside the index function the following code:
$this->load->library('email');
$this->email->from('no-reply#domain.example.com', 'Your Name');
$this->email->to('email#example.com');
$this->email->subject('Email Test');
$this->email->message('Testing the email class.');
$this->email->send();
echo $this->email->print_debugger();
this way the email arrives fine and not as spam...
when i create a model so i can use it later on for other features to send emails i replace the above code with the following one:
$this->load->library('email');
$this->load->model("email_model");
$this->email_model->sendEmail(null, 'email#gmail.com', 'title', 'message');
and email_model Model contains the following code :
<?php
class email_model extends CI_Model {
function __construct()
{
// Call the Model constructor
parent::__construct();
}
public function sendEmail($from = null, $to = null, $subject = null, $message = null){
$this->email->from('no-reply#domain.com', 'from user');
$this->email->to($to);
$this->email->subject($subject);
$this->email->message($message);
$this->email->send();
}
}
why does that happen?
check your spf records and RDNS records
SPF
here is an article # mediatemple kb: http://kb.mediatemple.net/questions/658/How+can+I+create+an+SPF+record+for+my+domain%3F#gs
RDNS
here is an article on RDNS:
http://www.crucialp.com/resources/tutorials/web-hosting/how-reverse-dns-works-rdns.php
seems content was the reason on the 2nd try... adding more text solved the issue...

email sending code shows an error in codeigniter

This is email sendind code
function send_letter()
{
$description = $this->input->post('description',true);
$this->load->model('newsletter_model');
$this->data['mail_list'] = $this->newsletter_model->getmaillist();
$this->email->from('ashitha10#gmail.com', 'Imageinit');
$this->email->subject('Email Test');
$this->email->message($description);
foreach($this->data['mail_list'] as $val)
{
$this->email->set_newline("\r\n");
$this->email->to($val['emailid']);
$this->email->send();
}
$this->index();
}
The email sends successfully to all emailids..But subject like "nosubject"
Also an error shows
Security:Notice
Message:Undefined Index:subject
FileName:libraries/email.php
line Number:941
content in line 941 in email.php is
if ($this->protocol == 'mail')
{
$this->_subject = $this->_headers['Subject'];
unset($this->_headers['Subject']);
}
Apparently the code which unsets the Subject header is in the method _write_headers, which is called from _build_message which is called from send. So every time you call send with mail as backend your subject gets erased (but the first mail is okay, since the subject gets stored in $this->_subject but that gets overwritten with an empty string in a subsequent call too).
This seems like a bug in the email.php, but as a workaround you could just set the subject in every iteration of the loop (so just move $this->email->subject('Email Test'); inside of the foreach-loop, maybe after the to-call).
Use this code
function send_letter()
{
$description = $this->input->post('description',true);
$this->load->model('newsletter_model');
$this->data['mail_list'] = $this->newsletter_model->getmaillist();
foreach($this->data['mail_list'] as $val)
{
$this->email->from('ashitha10#gmail.com', 'Imageinit');
$this->email->subject('Email Test');
$this->email->message($description);
$this->email->set_newline("\r\n");
$this->email->to($val['emailid']);
$this->email->send();
}
$this->index();
}

Using Email Class in Codeigniter (Advanced Way)

I want to send emails using Email Class(CodeIgniter) Problem is that
Email is sending so simple in text format...
I want to design email with (html, tables and colors) or What is the advanced-way for Emailing with CodeIgniter.
function signup(){
#stripped out the validation code
#stripped out the db insert code
$data = array(
'some_var_for_view'=>'Some Value for View'
);
$htmlMessage = $this->parser->parse('user/email/signup_html', $data, true);
$txtMessage = $this->parser->parse('user/email/signup_txt', $data, true);
#send the message
$this->email->from('test#webdevkungfu.com', 'CSSNinja');
$this->email->to($this->input->post('email_address'));
$this->email->subject('Account Registration Confirmation');
$this->email->message($htmlMessage);
$this->email->alt_message($txtMessage);
$this->email->send();
}
My problem is now solved with this code found from link below
http://www.webdevkungfu.com/email-templates-with-codeigniter/

Categories