Codeigniter sending mail with empty content - codeigniter

I'm using codeigniter email function for sending mail.
Here is the code.
$this->email->to($to);
$this->email->subject($subject);
$this->email->attach($_FILES['attachments']['tmp_name']);
$this->email->message($mailcontent);
if($this->email->send())
{
return true;
}
when I printed the content just before sending it like this, I found its fine till there.
$this->email->message($mailcontent);
print_r($mailcontent);exit;
if($this->email->send())
{
return true;
}
But the mail I'm getting is empty.It has subject but the body is empty.
Why is this happening ?

Thanks #Niloy Saha.. I found that this happens when I tried to send mail without attachment.So I just put the code for adding attachment in an if condition and it worked.
if($_FILES['attachments']['tmp_name'])
{
$this->email->attach($_FILES['attachments']['tmp_name']);
}
$this->email->message($mailcontent);
if($this->email->send())
{
return true;
}

Firstly load the email library:
$this->load->library('email');
Secondly just after sending the mail check:
if($this->email->send()){
echo $this->email->print_debugger();die;
}

Related

How To Check If Notification Is Sent Successfully In Laravel

I am unable to find in the documentation of how to handle the notification (what to do if notification is sent successfully or not)
NotificationController
public function AskForLevelUp()
{
$admin = User::where('is_admin', 1)->first();
$sendNotification = Notification::send($admin, new LevelUpNotification(Auth::user()->id));
if ($sendNotification->success()) // **HOW TO DO THIS CORRECTLY**
{
return back()->with('success', 'Your request has been submitted, please wait for admin confirmation.');
}
else
{
return back()->with('failure', 'Something went wrong, your request is not submitted. Please try again later.');
}
}
I've also read that send() has void return value here. Does anyone know how to handle this?

Enter the wrong email address, the email will still go to the correct email address

If I am sending the email to "test+10#gmail.com", it will still send to "test#gmail.com"
Here is my code, please tell me what I'm doing wrong?
$single_email = $this->single_email;
try {
Mail::send('welcome_mail',['user'=>$single_email], function($message) use($single_email) {
$name = "Name";
if($single_email->first_name){
$name = $single_email->first_name;
}
$message->to($single_email->email,$name)
->subject("Subject")
->from('from#gmail.com',"From")
->replyTo('replyto#gmail.com',"Reply to");
});
MailSend::where('id',$single_email->id)->update(['is_sent'=>3]);
} catch (\Exception $e) {
MailSend::where('id',$single_email->id)->update(['error'=>$e->getMessage()]);
}
Thanks.
That is how emails work. Gmail and most providers allow you multiple "sibling" emails by appending + to the end. For example, test+1#gmail.com and test+stackoverflow#gmail.com will both end up at test#gmail.com - One of many things people use this is to track which service leaked your email. E.g. test+facebook#gmail.com, if you start getting spam emails to test+facebook#gmail.com, you will know they revealed it.
So, there is nothing wrong with your code.

Magento issue with email charset

I have a little issue. I'm using to send mail in my custom module this piece of code:
$content = 'Wiadomość testowa ąśźć';
$mail = Mage::getModel('core/email');
$mail->setToName($name);
$mail->setToEmail($email);
$mail->setBody($content);
$mail->setSubject($subject);
$mail->setFromEmail('test#test.com');
$mail->setFromName("Test");
$mail->setType('html');
try {
$mail->send();
Mage::getSingleton('customer/session')->setData('success',Mage::helper('adminhtml')->__('Your request has been sent'));
}
catch (Exception $e) {
Mage::getSingleton('customer/session')->setData('error',Mage::helper('adminhtml')->__('Unable to send email.'));
}
How can I set utf-8 charset before sending email in magento mail function. ?
I get email with no utf-8 charset in email body.
SOLVED
I created sendZendMail function in helper.
I used this function, instead Magento core/email:
public function sendZendMail($name,$email,$subject,$content){
$mail = new Zend_Mail('UTF-8');
$mail->setBodyHtml($content);
$mail->setFrom('your#frommail.com');
$mail->addTo($email, 'No reply');
$mail->setSubject($subject);
try {
$mail->send();
Mage::getSingleton('core/session')->addSuccess('Mail was sucessfully send.');
}
catch(Exception $ex) {
Mage::getSingleton('core/session')->addError('Unable to send email.');
}
}
kristoff, I had the same issue before with Portuguese, which also has special characters. I've sorted it out by using the function htmlentities() for accented words. The Zend Mail Object is a bit retarded. I hope it helps.

CodeIgniter - UserAgent

I have the below code to check if the user is coming from a mobile, if they are I want to load a different page but it doesn't seem to be working...
Can anyone shed any light on the below:
if($xmlRefresh==1) {
$viewData['content'] = $this->load->view('newquote/policy_xml', $viewData);
$this->load->view('layout', $viewData);
} elseif ($this->agent->is_mobile()) {
$this->_showPage("newquote/policy_mobile", 'Customise', $viewData);
} else {
$this->_showPage("newquote/policy", 'Customise', $viewData);
}
I keep getting an internal server error, can anyone spot the problem...
Look at your system/core/Loader.php, function view() that will be $this->load->view returns void unless you add third parameter. try with:
$viewData2['content'] = $this->load->view('newquote/policy_xml', $viewData1, TRUE);
and
'''
$this->_showPage("newquote/policy_mobile", 'Customise', $viewData2);
Note: $viewData2 is different with $viewData1
are you calling this in controller?
What is the 500 (internal server) error??
Did you load the library user_agent??
$this->load->library('user_agent');
if($xmlRefresh==1) {
$viewData['content'] = $this->load->view('newquote/policy_xml', $viewData);
$this->load->view('layout', $viewData);
} elseif ($this->agent->is_mobile()) {
$this->_showPage("newquote/policy_mobile", 'Customise', $viewData);
} else {
$this->_showPage("newquote/policy", 'Customise', $viewData);
}
Finally if you want serve the mobile pages, use post post or pre hooks to determine the user agent before view is executed (if you want to play around)
i have one idea, may be help u
you can replace redirect()/$this->load->view() to $this->_showPage("newquote/policy_mobile", 'Customise', $viewData);

Swift Mailer : Address in mailbox given [] does not comply with RFC 2822, 3.6.2

I have a little problem here, when I try to send an email using swiftmailer, I got an error.
Here's my code:
<?php
session_start();
include("config.php");
$dir = dirname(__FILE__);
$session = $_SESSION[serialize];
// Load the SwiftMailer files
require_once($dir.'/swift/swift_required.php');
$hold = 'TIDAK';
// Kondisi - Kondisi yang ada
if($_GET[phase]=='confirm'){
$to = "test#127.0.0.1";
$kalimat = "Invoice Dengan nomor ".$_GET[nosc]." Telah di confirm, mohon di cek di sistem";
$kalimat2 = "Berikut terlampir data Invoice Customer , mohon di pastikan.";
$title = "CONFIRMATION NOTIFICATION";
}
// Convert string dlu
$mailer = new Swift_Mailer(new Swift_MailTransport()); // Create new instance of SwiftMailer
$message = Swift_Message::newInstance()
->setSubject($title) // Message subject
->setTo(array($to)) // Array of people to send to
->setBcc(array($bcc)) // Bcc:
->setFrom(array($_SESSION[mailadmin] => $_SESSION[namauser].''.store('namasite'))) // From:
->setBody($kalimat, 'text/html'); // Attach that HTML message from earlier
// Send the email, and show user message
if($hold<>'YA'){
if ($mailer->send($message)){
echo json_encode(array('success'=>true));
} else {
echo json_encode(array('success'=>false));
}
}
?>
And then when I try to post the data to this file through jquery I got an error, this error I got from firebug.
<br />
<b>Fatal error</b>: Uncaught exception 'Swift_RfcComplianceException' with message 'Address in mailbox given [] does not comply with RFC 2822, 3.6.2.' in C:\xampp\htdocs\Colosus\core\swift\classes\Swift\Mime\Headers\MailboxHeader.php:319
Stack trace:
#0 C:\xampp\htdocs\Colosus\core\swift\classes\Swift\Mime\Headers\MailboxHeader.php(249): Swift_Mime_Headers_MailboxHeader->_assertValidAddress(NULL)
#1 C:\xampp\htdocs\Colosus\core\swift\classes\Swift\Mime\Headers\MailboxHeader.php(107): Swift_Mime_Headers_MailboxHeader->normalizeMailboxes(Array)
#2 C:\xampp\htdocs\Colosus\core\swift\classes\Swift\Mime\Headers\MailboxHeader.php(71): Swift_Mime_Headers_MailboxHeader->setNameAddresses(Array)
#3 C:\xampp\htdocs\Colosus\core\swift\classes\Swift\Mime\SimpleHeaderFactory.php(74): Swift_Mime_Headers_MailboxHeader->setFieldBodyModel(Array)
#4 C:\xampp\htdocs\Colosus\core\swift\classes\Swift\Mime\SimpleHeaderSet.php(87): Swift_Mime_SimpleHeaderFactory->createMailboxHeader('Bcc', Array)
#5 C:\xampp\htdocs\Colosus\core\swift\classes\Swift\Mime in <b>C:\xampp\htdocs\Colosus\core\swift\classes\Swift\Mime\Headers\MailboxHeader.php</b> on line <b>319</b><br />
So guys, anybody can help me here? I really doesn't know what happening here.
It's better if you use a try/catch around the send instruction:
try {
if ($mailer->send($message)){
...
}
} catch (Exception $e) {
$log_error->write_error($e->getMessage() . ';');
}
Or if you validate the email before sending it:
if(!Swift_Validate::email($email)){ //if email is not valid
//do something, skip them or log them
$log_error->log($email);
}
Please make sure there is no white space before or after the email address given.

Resources