I am in a little bit of trouble sending mail with attachments in CodeIgniter. Here is my code:
$to="mailtouser#gmail.com"
$fileUrl=array(base_url()."assets/pdf/sample.pdf", base_url()."assets/pdf/sample1.pdf");
$subject="TEST MAIL";
$message="TEST MESSAGE";
$this->load->library('email');
$from = "admin#gmail.com;
$config['wordwrap'] = TRUE;
$config['mailtype']='html';
$config['charset'] = 'utf-8';
$config['priority'] = '2';
$config['protocol'] = 'sendmail';
$config['mailpath'] = '/usr/sbin/sendmail';
$this->email->initialize($config);
$this->email->set_newline("\r\n");
$this->email->from($from, 'Little Bloom');
//$this->email->to();
if($cc!=''){
$this->email->cc('sendmail#gmail.com');
}
$this->email->bcc($to);
$this->email->subject($subject);
$this->email->message($message);
if(!empty($fileUrl))
{
foreach($fileUrl as $key)
{
$this->email->attach($key);
}
}
return $this->email->send();
There are multiple pdf files so I am using an array to save the URL but when I send this to my email function these files are not attached to the mail.
if you print you array $fileUrl, you'll se at once where the problem is. Using e.g.: echo '<pre>'; print_r($fileUrl);die;
the urls are missing a forward slash between your_site and assets: http://your_siteassets/pdf/sample.pdf
you can fix this either with:
$fileUrl=array(base_url("assets/pdf/sample.pdf"), base_url("assets/pdf/sample1.pdf"));
or with
$fileUrl= array(base_url()."/assets/pdf/sample.pdf", base_url()."/assets/pdf/sample1.pdf");
docs url helper base_url()
Related
I can't send email from my codeigniter. it always reload when i press send button. I don't know what wrong with this code. please help me, so i can send the attached email to destination How can i solve this. The code is below :
function send_quotation($no, $email_to, $id_ticket, $problem, $action, $nama)
{
$this->load->model('service_tracking/trans_service_model');
$this->load->library('email');
$config['protocol'] = 'smtp';
$config['smtp_host'] = 'smtp.gmail.com';
$config['smtp_port'] = 465;
$config['smtp_user'] = 'examplefrom#gmail.com';
$config['smtp_pass'] = 'password123';
$config['priority'] = 1;
$config['mailtype'] = 'html';
$config['charset'] = 'iso-8859-1';
$config['starttls'] = TRUE;
$data = $this->trans_service_model->get_emaildata($id_ticket);
$this->email->initialize($config);
$this->load->library('email', $config);
$this->email->set_newline('\r\n');
$this->email->from('examplefrom#gmail.com', 'Example corporation');
$this->email->to('exampleto#gmail.com');
$data = $this->trans_service_model->get_emaildata($id_ticket);
$this->email->initialize($config);
$this->load->library('email', $config);
$this->email->set_newline('\r\n');
$this->email->from('examplefrom#gmail.com', 'Example Corporation');
$this->email->to('exampleto#gmail.com');
$this->email->subject('['.$no.'] - '.$nama.' - Quotation Repair');
$message = "
This is the email contain quotation";
$this->email->message($message);
$this->email->attach('./quotation/'.$no.'.pdf');
$this->email->send();
echo $this->email->print_debugger();
}
do like this
if (!$this->email->send()) {
echo $this->email->print_debugger();
} else {
echo "Success";
//redirect();
}
i have a problem in sending email in codeigniter.
$ci = get_instance();
$ci->load->library('email');
$config['protocol'] = "mail";
$config['smtp_host'] = "ssl://mail.smsgt.com";
$config['smtp_port'] = "25";
$config['smtp_user'] = "myemail#smsgt.com";
$config['smtp_pass'] = "";
$config['charset'] = "utf-8";
$config['mailtype'] = "html";
$config['newline'] = "\r\n";
$config['crlf'] = "\r\n";
and here is my code when sending the email
public function send_email_accountability($C11,$C12)
{
date_default_timezone_set('Asia/Manila');
$this->load->library('email');
$this->email->set_mailtype("html");
$this->email->from('noreply#smsgt.com', 'company email');
$this->email->to($C11);
$this->email->subject('Accountability for'. " ". $C12);
$this->email->message("testing");
$check = $this->email->send();
//echo $this->email->print_debugger();
if ($check){
$data = "true";
}
else{
$data = "false";
}
}
when i'm sending email with plain text in MESSAGE it works fine. but the problem is when i'm sending email with HTML scripts, it will not produce error but it will not send to the user and the email will not be received using MS OUTLOOK. can someone help me with this problem? thanks guys !
It would help if you can provide the possible error being returned by echo $this->email->print_debugger();, so why not just enable it at the moment then run your current code.
Alternatively, try this:
public function send_email_accountability($C11,$C12)
{
date_default_timezone_set('Asia/Manila');
$ci =& get_instance();
$ci->load->library('email');
$config['protocol'] = "mail";
$config['smtp_host'] = "ssl://mail.smsgt.com";
$config['smtp_port'] = "25";
$config['smtp_user'] = "myemail#smsgt.com";
$config['smtp_pass'] = "";
$config['charset'] = "utf-8";
$config['mailtype'] = "html";
$config['newline'] = "\r\n";
$config['crlf'] = "\r\n";
$ci->email->initialize($config);
$ci->email->from('noreply#smsgt.com', 'company email');
$ci->email->to($C11);
$ci->email->subject('Accountability for'. " ". $C12);
$ci->email->message("testing");
$check = $ci->email->send();
//echo $ci->email->print_debugger();
if ($check)
$data = "true";
else
$data = "false";
}
EDIT
Since you mentioned on comment that the echo $ci->email->print_debugger(); returns "Your message has been successfully sent using the following protocol" it just simply means that there is no syntactically wrong in your script. Like I said, my thoughts would be mail server issue.
If I will suggest this will be how I will be debugging your issue:
I'll replace $C11 with a Gmail address in $ci->email->to($C11); then run my current script and see if the issue of delay is the same.
Replace your current SMTP server credentials with something like Mandrill as sure its reporting log will definitely give you a hint of what is happening.
But either way, I guess you'll still end up digging something on your mail server (#smsgt.com). If I were you, I will reach out to your server administrator and start looking for clues in the mail server logs.
Try adding this:
$this->email->priority(3);
You can find more information here: http://ellislab.com/codeigniter/user-guide/libraries/email.html
You haven't initialize your email library with config values that you have set.
You just need to add below line in your code.
$this->email->initialize($config);
public function send_email_accountability($C11,$C12)
{
date_default_timezone_set('Asia/Manila');
$this->load->library('email');
$config['protocol'] = 'sendmail';
$config['mailpath'] = '/usr/sbin/sendmail';
$config['charset'] = 'utf-8';
$config['wordwrap'] = TRUE;
$config['mailtype'] = 'html';
$this->email->initialize($config);
//$this->email->set_mailtype("html");
$this->email->from('noreply#smsgt.com', 'company email');
$this->email->to($C11);
$this->email->subject('Accountability for'. " ". $C12);
$this->email->message("testing");
$check = $this->email->send();
//echo $this->email->print_debugger();
if ($check){
$data = "true";
}
else{
$data = "false";
}
}
Use something like this
$this->load->library('email', array(
'protocol' => 'mail',
'smtp_host' => 'ssl://mail.smsgt.com',
'smtp_port' => '25',
'smtp_user' => 'myemail#smsgt.com',
'smtp_pass' => '',
'newline' => '\r\n',
'crlf' => '\r\n',
'mailtype' => 'html'
));
$this->email->to($C11);
$this->email->from('noreply#smsgt.com', 'company email');
$this->email->subject('Accountability for'. " ". $C12);
$this->email->message("testing");
if( $this->email->send() ) {
return TRUE;
} else {
return FALSE;
}
try this code here's my code i use in sending email
function emailformat(){
$config['protocol'] = 'smtp'; // mail, sendmail, or smtp The mail sending protocol.
$config['smtp_host'] = 'smtp.gmail.com'; // SMTP Server Address.
$config['smtp_user'] = 'test#yahoo.com.ph'; // SMTP Username.
$config['smtp_pass'] = '#password'; // SMTP Password.
$config['smtp_port'] = '25'; // SMTP Port.
$config['smtp_timeout'] = '5'; // SMTP Timeout (in seconds).
$config['wordwrap'] = TRUE; // TRUE or FALSE (boolean) Enable word-wrap.
$config['wrapchars'] = 76; // Character count to wrap at.
$config['mailtype'] = 'html'; // text or html Type of mail. If you send HTML email you must send it as a complete web page. Make sure you don't have any relative links or relative image paths otherwise they will not work.
$config['charset'] = 'utf-8'; // Character set (utf-8, iso-8859-1, etc.).
$config['validate'] = FALSE; // TRUE or FALSE (boolean) Whether to validate the email address.
$config['priority'] = 3; // 1, 2, 3, 4, 5 Email Priority. 1 = highest. 5 = lowest. 3 = normal.
$config['crlf'] = "\r\n"; // "\r\n" or "\n" or "\r" Newline character. (Use "\r\n" to comply with RFC 822).
$config['newline'] = "\r\n"; // "\r\n" or "\n" or "\r" Newline character. (Use "\r\n" to comply with RFC 822).
$config['bcc_batch_mode'] = FALSE; // TRUE or FALSE (boolean) Enable BCC Batch Mode.
$config['bcc_batch_size'] = 200; // Number of emails in each BCC batch.
$this->load->library('email');
$this->email->initialize($config);
$this->email->from('test#yahoo.com.ph', 'Robot');
$this->email->to('test#yahoo.com.ph');
$this->email->subject('Expiration Notification');
$this->email->message('<html><body>This Message is to notify!</body></html>');
$this->email->send();
}
I can't figure out why if I try to use the CI Email Class it doesn't send emails, while if I use the native PHP mail() Class works.
Has to be noted that sometimes I get "email sent" while is not actually sent and sometimes I get the error "my server is not setup".
I tried to figure out how to set it up but... nothing...
Controller code follows:
if($this->form_validation->run()){
//Set Language
$this->lang->load('site', $this->session->userdata('lang'));
//Random key
$user_valid_key = md5(uniqid());
//Prepare email
$this->load->library('email', array('mailtype' => 'html'));
$this->email->from($this->config->item('email_signup_from'), 'Wondermark.net');
$this->email->to($this->input->post('email'));
$this->email->subject($this->lang->line('email_signup_subject'));
//Format mail con %s per inserire i campi necessari
$signup_msg = sprintf($this->lang->line('email_signup_message'), $this->input->post('fname'), base_url().'main/signup_confirm/'.$user_valid_key);
$this->email->message((string)$signup_msg);
if($this->email->send()){
//TODO: load view...
echo "email sent";
}else{
$to = $this->input->post('email');
mail($to, 'test', 'Other sent option failed');
echo $this->input->post('email');
show_error($this->email->print_debugger());
}
//TODO: Add to db
}else{
// Form validation failed
}
Use this setup email..
$this->load->library('email');
$config['protocol'] = 'smtp';
$config['smtp_host'] = 'ssl://smtp.gmail.com';
$config['smtp_port'] = '465';
$config['smtp_timeout'] = '7';
$config['smtp_user'] = 'sender_mailid#gmail.com';
$config['smtp_pass'] = 'password';
$config['charset'] = 'utf-8';
$config['newline'] = "\r\n";
$config['mailtype'] = 'text'; // or html
$config['validation'] = TRUE; // bool whether to validate email or not
$this->email->initialize($config);
$this->email->from('sender_mailid#gmail.com', 'sender_name');
$this->email->to('recipient#gmail.com');
$this->email->subject('Email Test');
$this->email->message('Testing the email class.');
$this->email->send();
echo $this->email->print_debugger();
I faced this problem and found the following solution. Just a little change in the email config and it's working 100%:
$config['protocol'] = 'ssmtp';
$config['smtp_host'] = 'ssl://ssmtp.gmail.com';
Codeigniter User Guide: https://www.codeigniter.com/user_guide/libraries/email.html
This setup works for me:
$config = Array(
'protocol' => 'smtp',
'smtp_host' => 'Your SMTP Server',
'smtp_port' => 25,
'smtp_user' => 'Your SMTP User',
'smtp_pass' => 'Your SMTP Pass',
'mailtype' => 'html'
);
$this->load->library('email', $config);
$this->email->set_newline("\r\n");
//Add file directory if you need to attach a file
$this->email->attach($file_dir_name);
$this->email->from('Sending Email', 'Sending Name');
$this->email->to('Recieving Email address');
$this->email->subject('Email Subject');
$this->email->message('Email Message');
if($this->email->send()){
//Success email Sent
echo $this->email->print_debugger();
}else{
//Email Failed To Send
echo $this->email->print_debugger();
}
I am using much time Run my configure code in localhost but it always gives me an error (Unable to send email using PHP SMTP. Your server might not be configured to send mail using this method.)
But when run this Below code in my server it works for me.
application>controller>Sendingemail_Controller.php
public function send_mail() {
$this->load->library('email');
$config = array();
$config['protocol'] = "smtp"; // you can use 'mail' instead of 'sendmail or smtp'
$config['smtp_host'] = "ssl://smtp.googlemail.com";// you can use 'smtp.googlemail.com' or 'smtp.gmail.com' instead of 'ssl://smtp.googlemail.com'
$config['smtp_user'] = "my#gmail.com"; // client email gmail id
$config['smtp_pass'] = "******"; // client password
$config['smtp_port'] = 465;
$config['smtp_crypto'] = 'ssl';
$config['smtp_timeout'] = "";
$config['mailtype'] = "html";
$config['charset'] = "iso-8859-1";
$config['newline'] = "\r\n";
$config['wordwrap'] = TRUE;
$config['validate'] = FALSE;
$this->load->library('email', $config); // intializing email library, whitch is defiend in system
$this->email->set_newline("\r\n"); // comuplsory line attechment because codeIgniter interacts with the SMTP server with regards to line break
$from_email = $this->input->post('f_email'); // sender email, coming from my view page
$to_email = $this->input->post('email'); // reciever email, coming from my view page
//Load email library
$this->email->from($from_email);
$this->email->to($to_email);
$this->email->subject('Send Email Codeigniter');
$this->email->message('The email send using codeigniter library'); // we can use html tag also beacause use $config['mailtype'] = 'HTML'
//Send mail
if($this->email->send()){
$this->session->set_flashdata("email_sent","Congragulation Email Send Successfully.");
echo "email_sent";
}
else{
echo "email_not_sent";
echo $this->email->print_debugger(); // If any error come, its run
}
}
and my view page where I defined f_email and email comes through post method.
application>view>emailtesting.php
<html>
<head>
<title> Send Email Codeigniter </title>
</head>
<body>
<?php
echo $this->session->flashdata('email_sent');
echo form_open('/Sendingemail_Controller/send_mail');
?>
<input type = "email" name = "f_email" placeholder="sender email id (from)" required />
<input type = "email" name = "email" placeholder="reciever email id (to)" required />
<input type = "submit" value = "SEND MAIL">
<?php
echo form_close();
?>
</body>
if some error comes again please visit official documentation below:
https://codeigniter.com/user_guide/libraries/email.html
After fighting with this same problem for a couple hours I finally decided to change my config to send through a different server. My original server for some reason would send to some addresses but not others (in same domain). As soon as I changed to sendgrid it worked as expected.
If you are not getting the results you expect, try a different smtp server. The problem may not be your code...
I had the same problem and I try below code instead of Codeignitor's mail function.
mail('mypersonalmail#domainserver.com' , 'Test', 'Test Email');
It works and mail is send to the email address. That mail has sent by already created email address (As i think). In my case it is:
gtt28651ff#p3plcpnl0552.srod.ahx3.domainserver.com
So I copy this email address and try it with below code.
$this->email->from('gtt28651ff#p3plcpnl0552.srod.ahx3.domainserver.com', 'www.domainserver.com');
And it work fine. It seems to be some servers need already created email address to send the email while others are NOT.
Hope this is clear and helpful.
Lately, sending emails through Google SMTP can be very tricky due to their security checks.
Verify if:
you have the rDNS on your server and it's matching your server IP
disable the 2-step auth on the email you are using to send emails from (on Google settings)
have IMAP and POP enabled on the email you are using to send emails from (on Google settings)
allow less secure apps on the email you are using to send emails from (on Google settings)
Change the CI settings as follows:
//Google settings for CI
$config['protocol'] = 'ssmtp'; //smtp not working
$config['smtp_host'] = 'ssl://ssmtp.gmail.com'; //notice ssmtp
$config['smtp_user'] = 'anyemail#server.com'; //must use Google
$config['smtp_pass'] = '***********';
$config['smtp_port'] = '465';
$config['smtp_crypto'] = 'ssl';
//general settings
$config['_smtp_auth'] = TRUE; //important
$config['smtp_timeout'] = 30;
$config['charset'] = 'utf-8';
$config['mailtype'] = 'html';
//optional
$config['wrapchars'] = 76;
$config['wordwrap'] = TRUE;
I hope this will save you the 2 days I have struggled with this issue.
I've spent hours trying to change the port,enabling the ssl extension in php.ini file etc nothing worked only ended up in a message SMTP server is not configured properly.I use WAMP as a localhost,when i turn off my antivirus avast the same config worked!!! the was send successfully.so it may be your antivirus program blocking on a local machine.
I tried the code below. At one time the code was working fine. I was able to send the emails. After a few minutes, when I tried it again without even changing anything, I get this error message Unable to send email using PHP mail(). Your server might not be configured to send mail using this method.
I dont know whats wrong. Lately I am facing a lot of similar bugs with codeigniter.
public function email($message = NULL, $subject=NULL, $email=NULL){
if(!isset($email)){
$to = $this->session->userdata('email');
}else{
$to = $email;
}
$this->load->library('email');
$this->email->from('noreply#mydomain.com', 'Mydomain');
$this->email->to($to);
$this->email->subject($subject);
$this->email->message($message);
$this->email->send();
}
i face this problem and work hard and i find solution this
just little change in email config its working 100%
$config['protocol'] = 'ssmtp';
$config['smtp_host'] = 'ssl://ssmtp.gmail.com';
I added the following lines and the mail is working again.
$config['protocol'] = 'sendmail';
$config['mailpath'] = '/usr/sbin/sendmail';
$config['charset'] = 'iso-8859-1';
$config['wordwrap'] = TRUE;
$this->email->initialize($config);
I am trying to figure out why my users are receiving duplicate copies of email messages being sent from my web application. Here is the code which sends the email:
function _send_user_email($to, $subject, $message) {
$this->load->library('email');
$config['charset'] = 'utf-8';
$config['wordwrap'] = TRUE;
$config['mailtype'] = 'html';
$config['protocol'] = 'sendmail';
$this->email->initialize($config);
$this->email->from('support#mysite.com', 'Customer Service');
$this->email->reply_to('support#mysite.com', 'Customer Service');
$this->email->to($to);
$this->email->bcc('support#mysite.com');
$this->email->subject($subject);
$this->email->message($message);
$this->email->send();
if ( ! $this->email->send())
{
echo $this->email->print_debugger();
exit;
}
}
Is there anything wrong with this code that might be causing the message to be sent twice?
Obviously, because of
$this->email->send();
if ( ! $this->email->send())
You are sending email twice. Remove the first call, leave only the one in if statement