Codeigniter email library sending blank emails or html source code - php

I've never had issues sending emails before through CodeIgniter, but all of a sudden it's an issue on my latest project.
I'm using this mail server tool to send mail on my local-host, which has never given me any issues before, and then the CodeIgniter email library.
I can get one of 2 results: either the email sends, but all the raw HTML source code is displayed, or the email sends and has a subject line, but the entire body is blank.
This is my email_helper.php
<?php defined('BASEPATH') OR exit('No direct script access allowed');
function send_email($to, $subject, $template)
{
$ci = &get_instance();
$ci->load->library('email');
$config = array(
'mailtype' => 'html',
'newline' => '\r\n',
'crlf' => '\r\n'
);
//If I comment out this line, it sends raw HTML, otherwise it sends a blank body.
$ci->email->initialize($config);
$ci->email->from($ci->config->item('from_email_address'), $ci->config->item('from_email_name'));
$ci->email->reply_to($ci->config->item('from_email_address'), $ci->config->item('from_email_name'));
$ci->email->to($to);
$ci->email->subject($subject);
$ci->email->message($ci->load->view('email/' . $template . '_html', $data, TRUE));
$ci->email->send();
}
this is my test_html.php
<!DOCTYPE html>
<html>
<head><title>Test</title></head>
<body>
<div style="max-width: 800px; margin: 0; padding: 30px 0;">
TEST!
</div>
</body>
</html>
And then I'm calling the email helper from my controller with this:
$this->load->helper('email_helper');
send_email($this->input->post('email'), 'Test Subject', 'test');

Hope this will help you :
You r missing $data there in your load view section and also try with $ci->load->library('email', $config); instead of $ci->email->initialize($config);
send_email should be like this :
function send_email($to, $subject, $template)
{
$ci = & get_instance();
$config = array(
'mailtype' => 'html',
'charset' => 'iso-8859-1',
'newline' => '\r\n',
'crlf' => '\r\n'
);
$data = '';
$body = $ci->load->view('email/' . $template . '_html', $data, TRUE);
echo $body; die;
$ci->load->library('email', $config);
$ci->email->set_mailtype("html");
$ci->email->from($ci->config->item('from_email_address'), $ci->config->item('from_email_name'));
$ci->email->reply_to($ci->config->item('from_email_address'), $ci->config->item('from_email_name'));
if ($to)
{
$ci->email->to($to);
$ci->email->subject($subject);
$ci->email->message($body);
if ($ci->email->send())
{
return TRUE;
}
else
{
echo $ci->email->print_debugger();die;
}
}
}
For more : https://www.codeigniter.com/user_guide/libraries/email.html

Related

Calendar invite is received as ICS file in outlook - Laravel

I am sending calendar invite using Laravel's mail api.
The calendar looks good on gmail but shows an attachment on outlook instead of proper calendar invitation.
Gmail's output:
while on outlook it seems to be an attachment:
I am creating a file with name invite.ics and I put the content inside the invite.ics file, I attach the file while sending the email.
$to = $row->to;
$subject = $row->subject;
$attachments = $row->attachment;
$cc = $row->cc;
$body = $row->body;
$calendar_invitation = $row->calendar_invitation;
\Mail::send(
'emailTemplates.dummy',
['emailBody'=>$row->body],
function(Message $message) use ($to,$subject,$attachments,$cc, $body, $calendar_invitation, $companyEmail)
{
$message->from($companyEmail, '');
$message->replyTo($companyEmail, 'Email Agent Evmeetings');
$message->to($to, '')->subject($subject);
$file = fopen("invite.ics","w");
echo fwrite($file,$calendar_invitation);
fclose($file);
$message->attach('invite.ics', array('mime' => "text/calendar"));
});
That's how I made it work
$message->from($companyEmail, '');
$message->replyTo($companyEmail, 'Email Agent Evmeetings');
$message->to($to, '')->subject($subject);
$message->setBody($calendar_invitation, 'text/calendar; charset="utf-8"; method=REQUEST');
$message->addPart($body, "text/html");
Added the calendar in body and changed the mime type to 'text/calendar; charset="utf-8"; method=REQUEST'
and used addPart($body, "text/html"); method to add html body in the email.
Full code:
\Mail::send('emailTemplates.dummy', ['emailBody'=>$row->body], function(Message $message) use ($to,$subject,$attachments,$cc, $body, $calendar_invitation, $companyEmail,$replyTo)
{
$message->from($companyEmail, trim(env("email_agent_name")));
$message->replyTo($replyTo, trim(env("email_agent_email")));
$message->to($to, '')->subject($subject);
$message->setBody($calendar_invitation, 'text/calendar; charset="utf-8"; method=REQUEST');
$message->addPart($body, "text/html");
$attachments = unserialize($attachments);
foreach($attachments as $attachment){
if(file_exists(public_path()."/".$attachment['location'])){
$message->attach(public_path()."/".$attachment['location'], array('as'=>$attachment['name'].".".pathinfo(parse_url($attachment['location'])['path'], PATHINFO_EXTENSION),
'mime' => mime_content_type ( public_path()."/".$attachment['location']) ));
}
}
$cc = unserialize($cc);
foreach($cc as $anotherEmail){
$message->cc($anotherEmail);
}
});

Email a PDF generated with html2pdf from a dynamically generated page

So I work with codeigniter and I generate a page.
I want to send a mail with the page attached as PDF but I also want not to save it on the server.
I found 2 code resources that look like doing exactly what I need:
<?php
$content = "
<page>
<h1>Exemple d'utilisation</h1>
<br>
Ceci est un <b>exemple d'utilisation</b>
de <a href='http://html2pdf.fr/'>HTML2PDF</a>.<br>
</page>";
require_once(dirname(__FILE__).'/html2pdf/html2pdf.class.php');
$html2pdf = new HTML2PDF('P','A4','fr');
$html2pdf->WriteHTML($content);
$html2pdf->Output('exemple.pdf');
?>
For creating a PDF using html2pdf library
<?php
require_once (dirname)(__FILE__).'/html2pdf/html2pdf.class.php');
require_once (dirname)(__FILE__).'/pjmail/pjmail.class.php');
?>
<?php
$pdf = new HTML2PDF ('P', 'A4');
$pdf->WriteHTML($content);
$content_pdf = $pdf->Output('document.pdf', true);
$mail = new PJmail();
mail->setAllFrom('webmaster#my_site.net', "My Site");
$mail->addrecipient('mymail#my_site.net');
$mail->addsubject("test");
$mail->text"Insert some text here...";
$mail->addbinattachement('test.pdf', $content_pdf);
echo $mail->sendmail();
?>
For sending the email with a PDF attachment.
Now I have to say that I am a beginner in PHP and I KIND OF understand what the code does. I don't know how to pass the link to the $content variable.
Someone told me that I should use a function like curl or file_get_contents but it is not clear to me at all. Could someone explain me a little?
I send you code with dom pdf in which email send with pdf and after sent email pdf delete
ob_start();
set_include_path(get_include_path() . PATH_SEPARATOR . "/path/to/dompdf");
require_once APPPATH . "core/dom_pdf/dompdf_config.inc.php";
$dompdf = new DOMPDF();
$dompdf->set_paper(DEFAULT_PDF_PAPER_SIZE, 'A4');
$dompdf->load_html($message);
$dompdf->render();
$output = $dompdf->output();
file_put_contents('pdf_name.pdf', $output);
$this->load->library('email');
$config = array (
'mailtype' => 'html',
'charset' => 'utf-8',
'priority' => '1'
);
$this->email->initialize($config);
$this->email->from('admin#admin.com', 'Admin');
$this->email->to(Reciever email);
$this->email->subject('Subject');
$this->email->message($message);
$this->email->attach(FCPATH."pdf_name.pdf");
$this->email->send();
<!-- If you want to delete file after send email -->
unlink(FCPATH."pdf_name.pdf");

php Mail_MIME - Generating and Attaching PDF

I am trying to write a PHP script that will generate a PDF and email it. My PDF generator works perfectly as an independent URL, but for some reason when I try to make the script email the generated PFD, the received file can't be opened. Here is the code:
include_once('Mail.php');
include_once('Mail/mime.php');
$attachment = "cache/form.pdf";
// vvv This line seems to be where the breakdowns is vvv
file_put_contents( $attachment, file_get_contents( "http://www.mydomain.com/generator.php?arg1=$arg1&arg2=$arg2" ) );
$message = new Mail_mime();
$message->setTXTBody( $msg );
$message->setHTMLBody( "<html><body>$msg</body></html>" );
$message->addAttachment( $attachment );
$body = $message->get();
$extraheaders = array( "From" => $from,
"Cc" => $cc,
"Subject" => $sbj );
$mail = Mail::factory("mail");
$headers = $message->headers( $extraheaders );
$to = array( "Jon Doe <jon#mydomain.com>",
"Jane Doe <jane#mydomain.com>" );
$addresses = implode( ",", $to );
if( $mail->send($addresses, $headers, $body) )
echo "<p class=\"success\">Successfully Sent</p>";
else
echo "<p class=\"error\">Message Failed</p>";
unlink( $attachment );
The line that I marked does generate a PDF file in the cache folder, but it will not open, so that seems to be a problem. However, when I try to attach a PDF file already exists, I have the same problem. I have tried $message->addAttachment( $attachment, "Application/pdf" ); as well, and it does not seem to make a difference.
Typically web server directories should have write permissions locked down. This is probably why you are experiencing problems with file_put_contents('cache/form.pdf').
// A working example: you should be able to cut and paste,
// assuming you are on linux.
$attachment = "/var/tmp/Magick++_tutorial.pdf";
file_put_contents($attachment, file_get_contents(
"http://www.imagemagick.org/Magick++/tutorial/Magick++_tutorial.pdf"));
Try changing where you are saving the pdf to a directory that allows write and read permissions to everyone. Also make sure this directory is not on your web server.
Also try changing the following three things
From
$message = new Mail_mime();
To
// you probably don't need this the default is
// $params['eol'] - Type of line end. Default is ""\r\n""
$message = new Mail_mime("\r\n");
From
$extraheaders = array(
"From" => $from,
"Cc" => $cc,
"Subject" => $sbj,
);
To
$extraheaders = array(
"From" => $from,
"Cc" => $cc,
"Subject" => $sbj,
'Content-Type' => 'text/html'
);
From
$message->addAttachment($attachment);
To
// the default second argument is $c_type = 'application/octet-stream'
$isAttached = $message->addAttachment($attachment, 'aplication/pdf');
if ($isAttached !== true) {
// an error occured
echo $isAttached->getMessage();
}
And you always want to make sure that you call
$message->get();
before
$message->headers($extraheaders);
or the whole thing wont work
I am pretty sure it must be an ini problem blocking file_get_contents(). However I came up with a better solution. I reworked the generator.php file and turned it in to a function definition. So I've got:
include_once('generator.php');
$attachment = "cache/form.pdf";
file_put_contents( $attachment, my_pdf_generator( $arg1, $arg2 ) );
...
$message->addAttachment( $attachment, "application/pdf" );
This way I do not need to write the file first. It works great (although I am still having slight issues with Outlook/Exchange Server, but I think that is a largely unrelated issue).

How do i send an array in an email message (CodeIgniter)

I was wondering how to send an array within an email message. I'm using pre tags to format it in the webpage. But im not able to send any data in the email. Here is the controller that im using:
<?php
class Notification extends CI_Controller {
public function index()
{
$this->db->select('product_name,project_code');
$this->db->from('user');
$this->db->like('product_name', 'Test');
$array = $this->db->get()->result();
$size = count($array);
echo 'The number of test are: ';
echo $size;
echo ' ';
echo "<pre>";
print_r($array);
echo "</pre>";
$config = Array(
'protocol' => 'smtp',
'smtp_host' => 'ssl://smtp.googlemail.com',
'smtp_port' => 465,
'smtp_user' => 'Email',
'smtp_pass' => 'Password',
'mailtype' => 'text',
'charset' => 'iso-8859-1'
);
$this->load->library('email', $config);
$this->email->set_newline("\r\n");
// Set to, from, message, etc.
$this->email->from('sender', 'Name');
$this->email->to('reciever');
$this->email->subject(' Test Updates');
$this->email->message($array);
$result = $this->email->send();
}
}
?>
I want to send the array $array in the email which is formatted similar to the pre tags.
[Note: I've edited out the email details. Other email functions work as expected]
I don't think the message method takes an array as a parameter. Try
$this->email->message(print_r($array, true));
Just as an alternative option for you, or anyone else reading this thread.
You can also create a view file for the email, passing the array as data.
In your controller:
$data['my_array'] = $array;
$this->email->message($this->load->view('my_email', $data, TRUE));
In your my_email.php view:
<pre><?php print_r($my_array);?></pre>
Has you defined the email receiver ? like you#gmail.com
For sure what the problem matter you can use
echo $this->email->print_debugger();

How to set email template in html?

I need help to create an Email template. My Email template is in HTML. This template is used to send plain text. My Email template is a HTML table tag.
$mail = new email();
$mail->setMailTemplate('emailVerification',array('email' => $email, 'password' => $password, ));
$mail->sendMail($request->getParameter('email'),sfConfig::get('app_email_verification_subject'));
How can I send a HTML email ?
This is my solution:
$instance = sfContext::getInstance();
sfProjectConfiguration::getActive()->loadHelpers( array("Partial") );
$message = $istanza->getMailer()->compose();
$message->setTo( $emailDest );
$message->setFrom($sender);
ob_start();
include_partial('templateName', array('data' => $dati, 'title' => $title, 'testo' => $testo));
$html = ob_get_contents();
ob_clean();
$message->setBody($html, 'text/html');
$instance->getMailer()->send($message);
Of course, in your global template folder, you'll have to create a file named "_templateName.php" with your email html content.

Categories