Email class - CodeIgniter PHP - Send email to multiple members - php

I have been trying to send an email to multiple members within my database. I managed to get the application to send one email to specific users emails, but when I use a list function I get errors (undefined index) that are supposedly in the libraries/email.php file but as far as I am aware I should not have to change the email library file. I am working on my localhost and here is the code:
function send(){
$this->load->model('mail_model', 'emails');
$emails=$this->emails->get_emails();
foreach($emails as $row){
if($row['email']){
$this->email->set_newline("\r\n");
$this->email->from('myemail#test.com', 'my name');
$this->email->to($row['email']);
$this->email->subject('Test multiple receivers');
$this->email->message('Test');
$this->email->send();
echo $this->email->print_debugger();
$this->email->clear();
$path = $this->config->item('server_root');
}
if($this->email->send())
{
//echo 'Your email was sent.';
$data['main_content'] = 'signup_confirmation';
$this->load->view('includes/template', $data);
}
else
{
show_error($this->email->print_debugger());
}
}
}
I have already autoloaded the email library in the config files. This is the code from the model:
class Mail_model extends CI_Model
{
function get($id = null)
{
$this->db->select('mailing_member_id', 'mailing_list_id', 'email_address');
$this->db->from('mailing_members');
if (!is_null($id)) $this->db->where('mailing_member_id', $id);
$this->db->order_by('mailing_member_id', 'desc');
return $this->db->get()->result();
}

Your model's get()method returns an object instead of an array. Try:
return $this->db->get()->result_array();
Also, you need $row['email_address']not $row['email'] for the ifstatement.

Related

Show the subject and message sending email with Mail::to

I have the code below to send emails using the Mail::to function. But Im not understanding how to set the subject and body of the message with the Mail::to function. I have the code below that is working to send emails but without subject and the $message is also not appearing in the email.
Do you know how to properly achieve that? (Have subject and the $request->message in the email using Mail::to)
public function send(Request $request, $id){
$conference = Conference::find($id);
if($request->send_to == "participant"){
// if is to send only 1 email the Mail::to is called directly here
Mail::to($request->participant_email)->send(new Notification($conference));
return;
}
if($request->send_to == "all"){
// $sendTo = query to get a set of emails to send the email
}
else{
// $sendTo = query to get a another set of emails to send the email
}
foreach($sendTo as $user){
$usersEmail[] = $user->email;
}
$message = $request->message;
$subject = $request->subject;
foreach ($usersEmail as $userEmail){
Mail::to($userEmail)->send(new Notification($conference, $message));
}
}
In the class Notification I have:
class Notification extends Mailable
{
public $conference;
public function __construct(Conference $conference)
{
$this->conference = $conference;
}
public function build()
{
return $this->markdown('emails.notification');
}
}
In the view notifications.blade.php I have:
#component('mail::message')
# Notification relative to {{$conference->name}}
{{$message}}
Thanks,<br>
{{ config('app.name') }}
#endcomponent
Try something like this:
$emailData = array(
/* Email data */
'email' => 'user#email.com',
'name' => 'User name',
'subject' => 'Email subject',
);
Mail::send('emails.template_name', ['emailData' => $emailData], function ($m) use ($emailData) { // here it is a closure function, in which $emailData data is available in $m
$m->from('info#domain.com', 'Domain Name');
$m->to($emailData['email'], $emailData['name'])->subject($emailData['subject']);
});
try
{
Mail::send('emails.contact_form', ['data' => $data],
function($message) use ($data)
{
$message->from('emailasinenv#hosting.com', 'ShortName');
$message->to( $data['adminEmail'] )->subject("Contact Form" );
});
return true;
}
catch (\Exception $ex) {
$ex->getMessage();
return false;
}
As you already have one template in your code use that template, Pass the message to template
$subject = 'Email Subject';
Mail::send('emails.notification', ['message' => $message], function ($mail) use ($userEmail, $subject) {
$mail->from('info#domain.com', 'Domain Name');
$mail->to($userEmail)->subject($subject);
});

Why am I getting undefined variable errors in Laravel method?

I have created a Laravel method in my PageController to send emails when form data is sent like the following:
public function sendMessage(Request $request)
{
$name = $request->input('name');
$email = $request->input('email');
$message_content = $request->input('message');
// email message
Mail::raw($message_content, function ($message)
{
$message->from($email, $name);
// $message->to(env('APP_ADMIN_EMAIL'));
$message->to("myemail#mail.com");
$message->subject('Website Message');
});
return "message sent";
}
But I get this error when the method is called:
ErrorException Undefined variable: email
Can someone explain what I'm doing wrong?
You need to pass the variables to the closure:
Mail::raw($message_content, function ($message) use ($email, $name)
{
$message->from($email, $name);
$message->to("myemail#mail.com");
$message->subject('Website Message');
});
See docs: https://secure.php.net/manual/en/functions.anonymous.php

Forget Email not send the email to customer in magento 1 but in code the mailer function sent the email

I am facing a issues with forget password issues the in magneto 1.9 .i am using the default forget password Transactional email template . i have also debug the code there in core file the mailer function send the email but i did not get it in email account.
In addition, i have created a custom template forget password.and setup it from the system-> comfiguration-> customer settings . than it will worked. it will send the email . but am concerns with default forgetpassword email template why it will not send the email to custom email.
I have debug the code at core file:-
app/code/core/Mage/Customer/controllers/AccountController.php
public function forgotPasswordPostAction()
{
$email = (string) $this->getRequest()->getPost('email');
if ($email) {
//echo "email".$email;
if (!Zend_Validate::is($email, 'EmailAddress')) {
$this->_getSession()->setForgottenEmail($email);
$this->_getSession()->addError($this->__('Invalid email address.'));
$this->_redirect('*/*/forgotpassword');
return;
}
/** #var $customer Mage_Customer_Model_Customer */
$customer = $this->_getModel('customer/customer')
->setWebsiteId(Mage::app()->getStore()->getWebsiteId())
->loadByEmail($email);
if ($customer->getId()) {
//echo "email".$customer->getID();die;
try {
$newResetPasswordLinkToken = $this->_getHelper('customer')->generateResetPasswordLinkToken();
$customer->changeResetPasswordLinkToken($newResetPasswordLinkToken);
$customer->sendPasswordResetConfirmationEmail();
} catch (Exception $exception) {
$this->_getSession()->addError($exception->getMessage());
$this->_redirect('*/*/forgotpassword');
return;
}
}
$this->_getSession()
->addSuccess( $this->_getHelper('customer')
->__('If there is an account associated with %s you will receive an email with a link to reset your password.',
$this->_getHelper('customer')->escapeHtml($email)));
$this->_redirect('*/*/');
return;
} else {
$this->_getSession()->addError($this->__('Please enter your email.'));
$this->_redirect('*/*/forgotpassword');
return;
}
}
/app/code/core/Mage/Customer/Model/Customer.php
protected function _sendEmailTemplate($template, $sender, $templateParams = array(), $storeId = null)
{
/** #var $mailer Mage_Core_Model_Email_Template_Mailer */
$mailer = Mage::getModel('core/email_template_mailer');
$emailInfo = Mage::getModel('core/email_info');
$emailInfo->addTo($this->getEmail(), $this->getName());
$mailer->addEmailInfo($emailInfo);
$mailer->setSender(Mage::getStoreConfig($sender, $storeId));
$mailer->setStoreId($storeId);
$mailer->setTemplateId(Mage::getStoreConfig($template, $storeId));
$mailer->setTemplateParams($templateParams);
$mailer->send();
// debug code
if($mailer->send()){
echo "mailer sent";
}else{
echo "not sent";
}
return $this;
}
Can any one help me to resolved it.

Error Sending Email in Codeigniter 2

I am new in codeigniter framework. I'm trying to send email but I have the problem:
Call to undefined method CI_Loader::libary()
here is my script on controller :
class Email extends CI_Controller{
function __construct()
{
parent::__construct();
}
function index()
{
$config = array(
'protocol' => 'smtp',
'smtp_host'=>'ss://smtp.googlemail.com',
'smtp_port'=>465,
'smtp_user'=>'thaitea.lumajang#gmail.com',
'smtp_pass'=>'thaitealumajang1'
);
$this->load->libary('email', $config);
$this->email->set_newline("\r\n");
$this->email->from('thaitea.lumajang#gmail.com','Tri Wicaksono');
$this->email->to('cha8agust#gmail.com');
$this->email->subject('This is an email test');
$this->email->message('It is working bro');
if($this->email->send())
{
echo 'Your email send';
} else {
show_error($this->email->print_debbuger());
}
}
}
I already find out the answer.
There is wrong typing in $this->load->libary.
It should be $this->load->library - there is a missing r in library word.

Codeigniter Form Validation Callback Function

I'm trying to create a form validation callback function but I'm having a little trouble getting my head around it.
What I am trying to do is create a contact form where with a join the mailing list option. If the option to join the mailing list is checked I want the name and email of the person to be added to the mailing list database. This part works perfectly however I also want the function to check the database to ensure that the email address being added is unique and this is the bit that I just can't get my head around.
Controller:
public function contact()
{
$this->load->helper('form');
$this->load->library('form_validation');
$this->form_validation->set_rules('name', 'your name', 'required', array('required'=>"<p class='required'>Please provide %s</p><br>"));
$this->form_validation->set_rules('email', 'your email address', 'required', array('required'=>"<p class='required'>Please provide %s</p><br>"));
if($this->form_validation->run() == FALSE)
{
$this->load->view('templates/headder');
$this->load->view('contact');
$this->load->view('templates/footer');
}
else
{
$this->load->library('email');
$name = $this->input->post('name');
$email = $this->input->post('email');
$phone = $this->input->post('phone');
$message = $this->input->post('message');
$list = $this->input->post('mailing_list');
$email_message = "Name: $name<br>Email: $email<br>Phone: $phone<br>Message:<br>$message";
$this->email->initialize();
$this->email->from($email, $name);
$this->email->to('myaddress#mydomain.co.uk');
$this->email->subject('New Query');
$this->email->message($email_message);
$this->email->send();
if($this->email->send()){
$this->load->view('send_error');
}
else
{
if($list == 'no')
{
$this->load->view('sent');
}
else
{
$this->form_validation->set_rules('email', 'Email', 'is_unique[mail_list, email]');
if($this->form_validation->run() == FALSE)
{
$this->load->model('mailing_listm');
$this->mailing_listm->add_name();
$this->load->view('sent');
}
else
{
$this->load->view('contact');
}
}
}
}
}
Error Message:
A Database Error Occurred
Error Number: 1064
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ' email 'myaddress#mydomain.co.uk' LIMIT 1' at line 3
SELECT * FROM `mail_list`, `email` WHERE mail_list, email 'myaddress#mydomain.co.uk' LIMIT 1
Filename: libraries/Form_validation.php
Line Number: 1134
Hopefully someone will be able to let me know what daft thing I've done this time.
Also, This function is turning into a bit of a monster, it's the most complicated thing I've every tried to write. Is there any way that I can split it out so that it is made up of several smaller functions instead of one gargantuan one?
Thanks,
EDIT
I have updated my code in line with the comment below about using is_unique however now I am receiving an error message.
EDIT
Model:
Public function add_name()
{
$this->name = $this->input->post('name');
$this->email = $this->input->post('email');
$this->db->insert('mail_list', $this);
}
for checking unique field there is a validation rule in codeigniter.
is_unique[table.field]

Categories