I am trying to send email on codeigniter with attach file.
I always receive email successfully. However , I never receive with attach file. Below is code and highly appreciate for all comments.
$ci = get_instance();
$ci->load->library('email');
$config['protocol'] = "smtp";
$config['smtp_host'] = "ssl://smtp.gmail.com";
$config['smtp_port'] = "465";
$config['smtp_user'] = "test#gmail.com";
$config['smtp_pass'] = "test";
$config['charset'] = "utf-8";
$config['mailtype'] = "html";
$config['newline'] = "\r\n";
$ci->email->initialize($config);
$ci->email->from('test#test.com', 'Test Email');
$list = array('test2#gmail.com');
$ci->email->to($list);
$this->email->reply_to('my-email#gmail.com', 'Explendid Videos');
$ci->email->subject('This is an email test');
$ci->email->message('It is working. Great!');
$ci->email->attach( '/test/myfile.pdf');
$ci->email->send();
$this->email->attach()
Enables you to send an attachment. Put the file path/name in the first parameter. Note: Use a file path, not a URL. For multiple attachments use the function multiple times. For example:
public function setemail()
{
$email="xyz#gmail.com";
$subject="some text";
$message="some text";
$this->sendEmail($email,$subject,$message);
}
public function sendEmail($email,$subject,$message)
{
$config = Array(
'protocol' => 'smtp',
'smtp_host' => 'ssl://smtp.googlemail.com',
'smtp_port' => 465,
'smtp_user' => 'abc#gmail.com',
'smtp_pass' => 'passwrd',
'mailtype' => 'html',
'charset' => 'iso-8859-1',
'wordwrap' => TRUE
);
$this->load->library('email', $config);
$this->email->set_newline("\r\n");
$this->email->from('abc#gmail.com');
$this->email->to($email);
$this->email->subject($subject);
$this->email->message($message);
$this->email->attach('C:\Users\xyz\Desktop\images\abc.png');
if($this->email->send())
{
echo 'Email send.';
}
else
{
show_error($this->email->print_debugger());
}
}
i have this problem before , the problem with the path file , so i change the path file to
$attched_file= $_SERVER["DOCUMENT_ROOT"]."/uploads/".$file_name;
$this->email->attach($attched_file);
And it works fine with me
With Codeigniter 3.1.0 I had same problem. Seems that there is missing a "\r\n":
Content-Type: application/pdf; name="test.pdf"<br>
Content-Disposition: attachment;<br>
Content-Transfer-Encoding: base64<br>
JVBERi0xLjYNJeLjz9MNCjQzNyAwIG9iag08PC9MaW5lYXJpemVkIDEvTCA3OTUyMTYvTyA0Mzkv<br>
RSA2ODEwODcvTiA0L1QgNzk0ODA3L0ggWyA1NjQgMjYxXT4+DWVuZG9iag0gICAgICAgICAgICAg<br>
should be:
Content-Type: application/pdf; name="test.pdf"<br>
Content-Disposition: attachment;<br>
Content-Transfer-Encoding: base64<br>
<br>
JVBERi0xLjYNJeLjz9MNCjQzNyAwIG9iag08PC9MaW5lYXJpemVkIDEvTCA3OTUyMTYvTyA0Mzkv<br>
RSA2ODEwODcvTiA0L1QgNzk0ODA3L0ggWyA1NjQgMjYxXT4+DWVuZG9iag0gICAgICAgICAgICAg<br>
I changed line 725 in system/libraries/Email from
'content' => chunk_split(base64_encode($file_content)),<br>
to
'content' => "\r\n" . chunk_split(base64_encode($file_content)),<br>
It works for me, but not the perfect fix.
Try putting the full path in $ci->email->attach();
On windows this would like something like
$ci->email->attach('d:/www/website/test/myfile.pdf');
This method has worked well for me in the past.
If you want to send attachment in email Without uploading file on server, please refer below.
HTML View file
echo form_input(array('type'=>'file','name'=>'attach_file','id'=>'attach_file','accept'=>'.pdf,.jpg,.jpeg,.png'));
Controller file
echo '<pre>'; print_r($_FILES); shows below uploaded data.
[attach_file] => Array
(
[name] => my_attachment_file.png
[type] => image/png
[tmp_name] => C:\wamp64\tmp\php3NOM.tmp
[error] => 0
[size] => 120853
)
We will use temporary upload path [tmp_name] where attachment is uploaded, because we DO NOT want to upload attachment file on server.
$this->email->clear(TRUE); //any attachments in loop will be cleared.
$this->email->from('your#example.com', 'Your Name');
$this->email->to('someone#example.com');
$this->email->subject('Email Test');
$this->email->message('Testing the email class.');
//Check if there is an attachment
if ( $_FILES['attach_file']['name']!='' && $_FILES['attach_file']['size'] > 0 )
{
$attach_path = $_FILES['attach_file']['tmp_name'];
$attach_name = $_FILES['attach_file']['name'];
$this->email->attach($attach_path,'attachment',$attach_name);
}
$this->email->send();
use path helper
$this->load->helper('path');
$path = set_realpath('./images/');
on email line
$this->email->attach($path . $your_file);
here i am using phpmailer to send mail
here full code is mention below
$this->load->library('My_phpmailer');
$mail = new PHPMailer();
$mailBody = "test mail comes here2";
$body = $mailBody;
$mail->IsSMTP(); // telling the class to use SMTP
$mail->Host = "ssl://smtp.gmail.com"; // SMTP server
$mail->SMTPDebug = 1;// enables SMTP debug information (for testing)
// 1 = errors and messages
// 2 = messages only
$mail->SMTPAuth = true;// enable SMTP authentication
$mail->Host = "ssl://smtp.gmail.com"; // sets the SMTP server
$mail->Port = 465;// set the SMTP port for the GMAIL server
$mail->Username = "YourAccountIdComesHere#gmail.com"; // SMTP account username
$mail->Password = "PasswordComesHere";// SMTP account password
$mail->SetFrom('SetFromId#gmail.com', 'From Name Here');
$mail->AddReplyTo("SetReplyTo#gmail.com", "Reply To Name Here");
$mail->Subject = "Mail send by php mailer";
$mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
$mail->MsgHTML($body);
$mail->AddAttachment($cdnStorage . '/' . $fileName);
$address ='WhomeToSendMailId#gmail.com';
$mail->AddAddress($address, "John Doe");
if (!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}
Here Is Full Source Code
$validation_rules = array(
array('field' => 'name', 'rules' => COMMON_RULES),
array('field' => 'email', 'rules' => COMMON_RULES),
array('field' => 'message', 'rules' => COMMON_RULES),
);
$this->validation_errors($validation_rules);
$name = $this->input->post('name');
$email = $this->input->post('email');
$message = $this->input->post('message');
$this->load->library('email');
//upload file
$attachment_file = "";
if (!empty($_FILES) && isset($_FILES["attachment_file"])) {
$image_name = $_FILES["attachment_file"]['name'];
$ext = pathinfo($image_name, PATHINFO_EXTENSION);
$new_name = time() . '_' . $this->get_random_string();
$config['file_name'] = $new_name . $ext;
$config['upload_path'] = "uploads/email/";
$config['allowed_types'] = "*";
$this->load->library('upload', $config);
$this->upload->initialize($config);
if ($this->upload->do_upload('attachment_file')) {
$finfo = $this->upload->data();
$attachment_file = base_url() . 'uploads/email/' . $finfo['file_name'];
} else {
$error = $this->upload->display_errors();
$this->msg = $error;
$this->_sendResponse(5);
}
}
$this->email->from($email, "$name")
->to("example#gmail.com")
->subject("FeedBack From $name")
->message($message)
->attach($attachment_file);
if ($this->email->send()) {
// temp pass updated.
$this->msg = "Email send successfully.";
$this->_sendResponse(1);
} else {
$this->msg = "Internal server error/Something went wrong.";
$this->_sendResponse(0);
}
$this->load->library('email'); // Loading the email library.
$this->email->clear(TRUE);
$this->email->from($user_email, $name);
$this->email->to('email#gmail.com');
$this->email->subject("Some subject");
$this->email->message("Some message");
if($_FILES['userfile']['name']!='' && $_FILES['userfile'['size'] > 0){
$attach_path = $_FILES['userfile']['tmp_name'];
$attach_name = $_FILES['userfile']['name'];
$this->email->attach($attach_path,'attachment',$attach_name);
}
$this->email->send();
Related
I have a running CodeIgniter email configuration using office 365 SMTP TLS Encryption.
But my HTML message was broken
$email_message = [
'name' => "USERNAME",
'code' => "03940",
];
$message = $this->load->view('emails/code_email', $email_message, true);
$this->load->library('email');
$config["protocol"] = "smtp";
$config["smtp_host"] = "smtp.office365.com";
$config["smtp_port"] = "587";
$config[ "smtp_crypto"] = "tls";
$config["smtp_timeout"] = "7";
$config["smtp_user"] = "some#email.com";
$config["smtp_pass"] = "someemailpassword";
$config["charset"] = "utf-8";
$config["newline"] = "\r\n";
$config["mailtype"] = "html"; // or text
$config["validation"] = true;
$this->email->initialize($config);
$this->email->set_newline("\r\n");
$this->email->from('some#email.com', 'Some Email');
$this->email->to('user#email.com');
$this->email->subject('Test Lang');
$this->email->message($message);
if ($this->email->send()) {
echo 'OKS';
} else {
echo $this->email->print_debugger();
}
This is what I got:
email i got
This what I expected to show:
email expected
I am trying to send email from live server using domain email id. I tried with different port id, every time I got same error.My controller code is given below:
public function send_mail() {
$this->load->library('email');
$name = $this->input->post("name");
$email = $this->input->post("email");
$config['protocol'] = 'smtp';
$config['smtp_host'] = 'mail.*******.com';
$config['smtp_port'] = '465';
$config['smtp_timeout'] = '7';
$config['smtp_user'] = 'info#*******.com';
$config['smtp_pass'] = '*********';
$config['charset'] = 'utf-8';
$config['newline'] = '\r\n';
$config['mailtype'] = 'text'; // or html
$config['validation'] = TRUE;
$this->email->initialize($config);
$this->email->from('info#*******.com', 'Hima');
$this->email->to($email);
$this->email->subject('email verification');
$this->email->message('Verification Code');
$send = $this->email->send();
if ($send) {
$this->session->set_flashdata('msg','success');
} else {
echo $error = $this->email->print_debugger();
}
}
I have configured like following :
public function send_mail() {
$this->load->library('encrypt');
$config = array(
'protocol' => 'smtp',
'smtp_host' => 'smtp.gmail.com',
'smtp_port' => '465',
'smtp_user' => '*****#gmail.com',
'smtp_pass' => '**********',
'smtp_crypto' => 'ssl',
'crlf' => "\r\n",
);
$this->load->library('email',$config);
$send = $this->email->send();
if ($send) {
return $send;
} else {
$error = show_error($this->email->print_debugger());
return $error;
}
}
Hope this helps!
I am using Amazon ses account to send email.
All the credentials are working fine in core php with class.phpmailer.php
But not working with codeigniter email class on the same server
And not showing me any error with echo $this->email->print_debugger();
Please take a look at code
core php code
//include phpmailer
require_once('class.phpmailer.php');
//SMTP Settings
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->SMTPAuth = true;
$mail->SMTPSecure = "tls";
$mail->Host = "email-smtp.us-west-2.amazonaws.com";
$mail->Username = "AKIAIF4U4***5J36ROIA";
$mail->Password = "AhI9Nn********GSo4u8r0xNK/OI";
//
$mail->SetFrom('donotreply#*****.com', 'medi***'); //from (verified email address)
$mail->Subject = "Email Subject"; //subject
//message
$body = "This is a test message vipul.";
$body = eregi_replace("[\]",'',$body);
$mail->MsgHTML($body);
//recipient
$mail->AddAddress("vipul.kumar#******.com", "Test Recipient");
//Success
if ($mail->Send()) {
echo "Message sent!"; die;
}
//Error
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
}
codeigniter code
public function test_aws_email($data=array()){
$config = array(
'protocol' => 'smtp',
'smtp_host' => 'tls://email-smtp.us-west-2.amazonaws.com',
'smtp_port' => 465,
'smtp_user' => 'AKIAIF4U4***5J36ROIA',
'smtp_pass' => 'AhI9Nn********GSo4u8r0xNK/OI'
);
$config['crlf'] = "\r\n";
$this->load->library('email', $config);
$this->email->set_newline("\r\n");
$this->email->from("donotreply#******.com", 'Medi***');
$this->email->to('vipul.kumar#******.com');
$this->email->subject('TEST FOR AWS ');
$this->email->message('AWS AUTHENTICATION CLEAR ');
$this->email->set_mailtype("html");
$this->email->send();
echo $this->email->print_debugger();
}
I have application where I want to send mails to clients but i am getting error from server.
Controller code for sending mail
$this->load->library('email');
$config['protocol'] = 'smtp';
$config['smtp_host'] = 'mail.lmsweb.in';
$config['smtp_port'] = '25';
$config['smtp_user'] = 'support#lmsweb.in';
$config['smtp_pass'] = '****';
// $config['charset'] = 'iso-8859-1';
$this->email->initialize($config);
$this->email->from('support#lmsweb', 'LMS');
$this->email->to($this->input->post('txtvisitoremail'));
$this->email->subject('Client Login Details');
$this->email->subject('Congratulation');
$this->email->send();
echo $this->email->print_debugger();
redirect('telecaller/telecaller/update');
Same code works when using on localhost but on server I get this error.
After changing protocol to mail I get:
Message: mail() [function.mail]: SMTP server response: 503 This mail server requires authentication when attempting to send to a non-local e-mail address. Please check your mail client settings or contact your administrator to verify that the domain or address is defined for this server.
Filename: libraries/Email.php
Line Number: 1539
I then created a new test mail account, test#lmsweb.in and tried sending mail to it and it worked. So its clear that using mail protocol I cannot send mail to other like Gmail.
I used this code to send mail from server:
function sendEmail($data, $templateName) {
//echo "<pre>";
//print_r($data);
$this->load->library('email');
$this->email->set_mailtype('html');
$this->email->from($data['from_address']);
$this->email->to($data['to_address']);
//$this->email->cc($data['cc_address']);
$this->email->subject($data['subject']);
$body = '--------'
$this->email->message($body);
$this->email->send();
$this->email->clear();
//redirect('web_welcome/member_signup');
}
In this line you don't have valid address $this->email->from('support#lmsweb', 'LMS'); You are lacking .in: support#lmsweb .in
Anyway you can use the PHP Mailer class for sending emails from codeigniter. I've done it by putting the files class.phpmailer.php and class.smtp.php in third party folder and then creating custom library for mail sending:
class Mail_sender
{
protected $ci;
var $mail;
public function __construct()
{
include (APPPATH. 'third_party/class.phpmailer.php');
$this->ci =& get_instance();
$this->mail = new PHPMailer ();
$this->init();
}
public function init(){
$this->mail->IsSMTP ();
$this->mail->IsHTML(true);
$this->mail->Mailer = 'smtp';
$this->mail->SMTPAuth = false;
$this->mail->Host = 'smtp.t-home.mk'; // "ssl://smtp.gmail.com" didn't worked
$this->mail->Port = 25;
$this->mail->SMTPSecure = '';
$this->mail->SMTPAuth = true;
$this->mail->Username = 'user#example.com'; // SMTP username
$this->mail->Password = 'secret'; //SMTP password
$this->mail->SingleTo = true; // if you want to send a same email to multiple users. multiple emails will be sent one-by-one.
}
public function setUsername($username){
$this->mail->Username = $username;
}
public function setFrom($email, $sender_name) {
$this->mail->From = $email;
$this->mail->FromName = $sender_name;
}
public function setAddress($to){
$this->mail->addAddress ($to);
}
public function setReplyTo($replyTo)
{
$this->mail->AddReplyTo($replyTo);
}
public function setSubject($subject){
$this->mail->Subject = $subject;
return $this->mail->Subject;
}
public function setBody($messageBody){
$this->mail->Body = $messageBody;
}
public function sendMail(){
return $this->mail->Send();
}
}
And used a controller for sending mail:
$nameSurname=$this->input->post('name');
$email = $this->input->post('email');
$subject = $this->input->post('subject');
$temp_message = $this->input->post('message');
$message = 'FROM: '.$nameSurname.'('.$email.')<br><br>'.$temp_message;
$array = array(
'ime' => $nameSurname,
'mail' => $email,
'sub' => $subject,
'msg' => $message
);
$this->load->library('mail_sender');
$this->mail_sender->setFrom('[from address]','[title]');
$this->mail_sender->setSubject($subject);
$this->mail_sender->setAddress("[receiving address]");
$this->mail_sender->setBody($message);
$this->mail_sender->sendMail();
Hope it helps.
$config = Array(
'protocol' => 'smtp',
'smtp_host' => 'ssl://smtp.googlemail.com',
'smtp_port' => 465,
'smtp_user' => 'something#mail.com', // change it to yours
'smtp_pass' => '*******', // change it to yours
'wordwrap' => TRUE,
'crlf' => "\r\n",
'newline' => "\r\n" );
it is worked for me after adding last two lines ''crlf' => "\r\n",
'newline' => "\r\n"'
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();
}