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();
Related
I want to iterate array then It should be reset again in PHP .
The following logic I want to implement
I have an array of smtp and a second array from which I just want to send
email once per smtp .
Actually I have a list of array for which I want to send email but I have
few smtp hosts .
I am iterating a list of array in foreach loop.
I have a smtp array in which I have defined certain limit to send email.
function sendmail_test(){
return "Sent<br/>";
}
$email_arrays=array(
'test1#gmail.com',
'test2#gmail.com',
'test3#gmail.com',
'test4#gmail.com',
'test5#gmail.com',
'test6#gmail.com',
);
$smtp_array=array(
'gmail_smtp#gmailsmtp.com'=>10,
'yogya_smtp#yogyasmtp.com'=>15
);
$smtp_count=count($smtp_array);
$smtp_counter=0;
for($i=0;$i<=$smtp_count;$i++){
foreach($email_arrays as $ek=>$ev){
print_r($smtp_counter);
echo sendmail_test();
}
$smtp_counter++;
}
Actually I want exactly like this.
I have currently have two smtp in $smtp_array
First email should be fired by like this smtp
test1#gmail.com - >'gmail_smtp#gmailsmtp.com'
And second email should be fired by like this
test2#gmail.com',->'yogya_smtp#yogyasmtp.com'
and then third email should be fired like this
test3#gmail.com - >'gmail_smtp#gmailsmtp.com'
which Is then reset and will use first smtp in $smtp_array ..
I hope you will get my point now.
If you want to go though all the smtp servers and reset to the start when you had the last one, you might use current, end, reset, and next.
To get the value for the smtp, you could use key You can then add an smtp server to the list as well.
For example:
function sendmail_test()
{
return "Sent<br/>";
}
$email_arrays = array(
'test1#gmail.com',
'test2#gmail.com',
'test3#gmail.com',
'test4#gmail.com',
'test5#gmail.com',
'test6#gmail.com',
);
$smtp_array = array(
'gmail_smtp#gmailsmtp.com' => 10,
'yogya_smtp#yogyasmtp.com' => 15
);
$lastElement = end($smtp_array);
reset($smtp_array);
foreach ($email_arrays as $em) {
$current = current($smtp_array);
//sendmail_test();
echo "Send email $em with smtp: " . key($smtp_array) . PHP_EOL;
next($smtp_array);
if ($lastElement === $current) {
reset($smtp_array);
}
}
Result:
Send email test1#gmail.com with smtp: gmail_smtp#gmailsmtp.com
Send email test2#gmail.com with smtp: yogya_smtp#yogyasmtp.com
Send email test3#gmail.com with smtp: gmail_smtp#gmailsmtp.com
Send email test4#gmail.com with smtp: yogya_smtp#yogyasmtp.com
Send email test5#gmail.com with smtp: gmail_smtp#gmailsmtp.com
Send email test6#gmail.com with smtp: yogya_smtp#yogyasmtp.com
Php demo
You can approach this as following
$hosts = array_keys($smtp_array);
foreach($email_arrays as $k => $v){
$email = $v;
$host = $hosts[$k%2];
echo $email.'----'.$host;echo '<br/>';// use $email and $host to send the email
}
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
in there i have been create function for send an email, here my code like this :
$row=DB::table('orders')
->join("cms_members","cms_members.id","=","orders.id_cms_members")
->select("orders.*","cms_members.email as email","cms_members.name as name","cms_members.phone as phone")
->where('orders.id',$id_order)
->first();
$datas = array(
'name' => 'test',
'detail'=> 'test',
'sender' => 'adamprojo#gmail.com'
);
$customPaper = array(0,0,800,800);
$pdf = PDF::loadView('testing', $data)->setPaper($customPaper);
Mail::send('emails.temp', $datas, function($message) use($pdf)
{
$message->from("no-reply#crocodic.net","Invoice HOP Daily rent Orders");
$message->to($row->email)->subject('Invoice');
$message->attachData($pdf->output(), "invoice.pdf");
});
here my problem when ever i want to send an email i get response
Address in mailbox given does not comply with RFC 2822, 3.6.2.
but if i try to print the email its give me the email address. what should i do ?
Kindly, can someone give me any solution for mentioned problem?
This will be your code:
$row=DB::table('orders')
->join("cms_members","cms_members.id","=","orders.id_cms_members")
->select("orders.*","cms_members.email as email","cms_members.name as name","cms_members.phone as phone")
->where('orders.id',$id_order)
->first();
$datas = array(
'name' => 'test',
'detail'=> 'test',
'sender' => 'adamprojo#gmail.com'
);
$customPaper = array(0,0,800,800);
$pdf = PDF::loadView('testing', $data)->setPaper($customPaper);
Mail::send('emails.temp', $datas, function($message) use($pdf, $row)
{
$message->from("no-reply#crocodic.net","Invoice HOP Daily rent Orders");
$message->to($row->email)->subject('Invoice');
$message->attachData($pdf->output(), "invoice.pdf");
});
You need to set use($pdf, $row) by the Mail::send(), this will get the $row data that you use in the $message->to().
Hope this works!
i don't understand how send one email for all users, i do it this in my controller :
// Init
$data = $this->request->data['Email'];
$d = array(
'subject' => $data['subject'],
'message' => $data['message']
);
// QUERY
$all = $this->Spoutnik->find('all', array(
'conditions' => array(
'Spoutnik.role >=' => '1'
),
'fields' => array('Spoutnik.email')
));
$this->set(compact('all'));
// list
$bcc = '';
foreach ($all as $user) {
$bcc .= $user['Spoutnik']['email'].',';
}
// MAIL
App::uses('CakeEmail', 'Network/Email');
$CakeEmail = new CakeEmail('default');
$website_short_name = Configure::read('website.short_name');
$CakeEmail->bcc("$bcc");
$CakeEmail->subject(''.$website_short_name.' :: '.$d['subject'].'');
$CakeEmail->viewVars(array(
'message' => (''.$d['message'].'')
));
$CakeEmail->emailFormat('html');
$CakeEmail->template('message_direct');
// final
$CakeEmail->send();
But i have error "no valid mail" , and after the liste of user's mail
what is wrong in my code ?
Couple of things I've noticed at a quick glance...
foreach ($all as $user) {
$bcc .= $user['Spoutnik']['email'].',';
}
In that code, you're adding a comma after every email, so at the end of your string you'll have a comma. Try this:
$e = 0;
foreach ($all as $user) {
if($e > 0) $bcc .= ',';
$bcc .= $user['Spoutnik']['email'];
$e++;
}
--edit-- good point Deepak, Cake's documentation suggests you give BCC an array. It's easier and more efficient to produce so do that.
Second, $CakeEmail->bcc("$bcc"); doesn't need the quotes. It should work fine with them, but I've seen Cake do some pretty weird things... Try taking them out:
$CakeEmail->bcc($bcc);
Third, you're setting all those emails to BCC which is fine, but I can't see a to address. If you want to send out to a lot of email address without them seeing each other, you still need to send the email to somewhere, even if its noreply#yourdomain.com. Add a to address in before you send:
$CakeEmail->to('noreply#yourdomain.com');
I will just use the addBcc function of CakeEmail and modify the loop:
App::uses('CakeEmail', 'Network/Email');
$CakeEmail = new CakeEmail('default');
// list
foreach ($all as $user) {
$CakeEmail->addBcc($user['Spoutnik']['email']);
}
$website_short_name = Configure::read('website.short_name');
$CakeEmail->subject(''.$website_short_name.' :: '.$d['subject'].'');
$CakeEmail->viewVars(array(
'message' => (''.$d['message'].'')
));
Try changing your $bcc block to this:
// list
$bcc = array();
foreach ($all as $user) {
$bcc[]= $user['Spoutnik']['email'];
}
Also refer to CakeEmail Documentation
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.