I am working on booking app in codeigniter. I am stuck in email sending from localhost.
I am using gmail smtp for sending email. I am using email/booking_email.php view file as email template. But, When I run website page that contains email sending script in localhost, it keeps loading but email is not sent.
$email = "atomambition#gmail.com";
$name = "Wiyo HK";
$email_config = Array(
'protocol' => 'smtp',
'smtp_host' => 'ssl://smtp.gmail.com',
'smtp_port' => 465,
'smtp_user' => 'atomambition#gmail.com',
'smtp_pass' => '********',
'mailtype' => 'html',
'charset' => 'iso-8859-1'
);
$this->load->library('email', $email_config);
$this->email->from($email, $name);
$this->email->to($this->bookings_class->get_email());
$this->email->reply_to($email, $name);
$subject = 'This is Booking email from Wiyo HK';
$this->email->subject($subject);
$data = array('customer_name' => 'Avash Poudel');
$message = $this->load->view('email/booking_email', $data, TRUE);
$this->email->message($message);
echo $result = $this->email->send();
Localhost does not support sending emails directly unless configured.
In the localhost it does not work like this . For testing use mail() protocol on localhost and smtp on server.
$config['crlf'] = "\r\n";
$config['newline'] = "\r\n";
Add this in $email_config.
After that you need to initialise your config array.
So write this $this->email->initialize($email_config);
Also use $this->email->print_debugger() to debug
Related
I've setup my email sender using outlook office365 with codeigniter 3. Below code works but sometimes it doesn't and I don't know why. Sometimes it will not work the first time I push my repo but when I tried it again, it works. I've logged the error below.
Error:
Failed to authenticate password. Error: <br />Unable to send email using PHP SMTP. Your server might not be configured to send mail using this method.<br />
Code:
public function send_mail($to_email = null, $name = '')
{
$config = array(
'protocol' => 'smtp',
'smtp_crypto' => 'tls',
'smtp_host' => 'smtp.office365.com',
'smtp_port' => 587
'smtp_user' => EMAIL_SENDER,
'smtp_pass' => EMAIL_SENDER_PASSWORD,
'mailtype' => 'html',
'charset' => 'utf-8',
'wordwrap' => TRUE,
'newline' => "\r\n",
'crlf' => "\n",
'starttls' => true
);
$from_email = EMAIL_SENDER;
$this->load->library('email', $config);
$this->email->set_newline("\r\n");
$this->email->from($from_email, 'Power App');
$this->email->to($to_email);
$this->email->subject('Password Reset');
$this->email->set_crlf("\r\n");
$data["user"] = $to_email;
$data["name"] = $name;
$this->email->message($this->load->view('email/email_reset_password_template', $data, true));
//Send mail
if ($this->email->send())
return "Email sent successfully.";
else {
return $this->email->print_debugger();
}
}
UPDATE:
Its now always working when I use PHPMailer. Haven't really discover why its not always sending email using email library.
Here is the controller code for sending email
$dataset = $this->Admin_model->getSubscribers();
$outputArray = [];
foreach($dataset as $row) {
$outputArray[] = $row['subscription_email'];
}
$to_email = implode(', ', $outputArray);
$config = Array(
'protocol' => PROTOCOL,
'smtp_crypto' => SMTP_CRYPTO,
'smtp_host' => SMTP_HOST,
'smtp_port' => SMTP_PORT,
'smtp_user' => SMTP_USERNAME,
'smtp_pass' => SMTP_PASSWORD,
'smtp_timeout' => SMTP_TIMEOUT,
'charset' => "utf-8",
'mailtype' => "html",
'newline' => "\r\n",
'wordwrap' => TRUE
);
$fromemail = FROM_EMAIL; //website mail
$name = FROM_NAME; //website name
$subject = 'Test';
$message = ''; //title & 1 image
$this->load->library('email', $config);
$this->email->set_crlf("\r\n");
$this->email->set_newline("\r\n");
$this->email->from($fromemail, $name);
$this->email->to($to_email);
$this->email->cc("");
$this->email->bcc("");
$this->email->subject($subject);
$this->email->message($message);
$this->email->send(); //send email
$this->email->clear();
it shows all the recipients email address but i want to hide the other recipient email address when one of the receiver is getting the email
Like #RiggsFolly said, or you can send one email with everyone as a BCC recipient instead of a To, this way no one will see any of the other's emails. The BCC stands for Blind Carbon Copy. That means no one can see who the email is going to.
$this->email->to("");
$this->email->cc("");
$this->email->bcc($to_email);
I will try to send mail using gmail in codeigniter. I write code as given bellow. When i upload it on c panel it will work properly for 2 days but after that it stop working.
//Load email library
$this->load->library('email');
//SMTP & mail configuration
$config = array(
'protocol' => 'smtp',
'smtp_host' => 'ssl://smtp.googlemail.com',
'smtp_port' => 465,
'smtp_user' => 'xxxxxx',
'smtp_pass' => 'xxxxxx',
'mailtype' => 'html',
'charset' => 'utf-8'
);
$this->email->initialize($config);
$this->email->set_mailtype('html');
$this->email->set_newline("\r\n");
//Email content
$htmlContent = '<h1>MY WEBSITE</h1>';
$htmlContent .= '<p>OTP For Reset Your Password.</p>';
$this->email->to($uemail);
$this->email->from('xxxxxxxx','MyWebsite');
$this->email->subject('MESSAGE');
$this->email->message($htmlContent);
//Send email
$this->email->send();
I experienced the same thing where the email was not sent and there was no error, it turned out that when I checked my Gmail account it wasn't always on the Primary tab (make sure it wasn't in the Spam folder either). Sent but in the Promotions tab.
I'm a newbie to CodeIgntier. I'm trying to setup SMTP on CodeIgniter. I receive a success message on page but email is not delivered. Can somebody help me?
$config = Array(
'protocol' => 'smtp',
'smtp_host' => 'ssl://smtp.gmail.com',
'smtp_port' => 465,
'smtp_user' => 'xxx#gmail.com',
'smtp_pass' => 'xxx',
'mailtype' => 'html',
'charset' => 'iso-8859-1'
);
$this->load->library('email', $config);
$this->email->from('xxx#gmail.com', 'Admin Team');
$sendTo = $this->input->post('email');
$message= 'Hello';
$this->email->to($sendTo);
$this->email->subject('Verification Email');
$this->email->message($message);
if($this->email->send()){
echo "Success";
}
You have mentioned xxx#gmail.com. In google there is Security protection call 2-Step Verification. Make sure its there,If there remove it and try
There is no fault in your code. . To remove it check this link
your code seems ok, make sure your server has openssl extension up and running , and check your firewall for the used smtp port (465 in your case). I would also recommend you using the third party library phpmailer from https://github.com/ivantcholakov/codeigniter-phpmailer whic (mostly for sending html in your message and links )
cheers!
I use CI library for sending HTML email through smtp. When i try to send from my computer, it send right HTML. but when i send it from server, why i get HTML tag. not formatted as html ? I already set 'mailtype' to 'html'.
I wonder why it work at my local but not at server. Is there any configuration in php ? or i should send email header or something?
public function index()
{
$config = array(
'protocol' => 'smtp',
'smtp_host' => 'tls://smtp.gmail.com',
'smtp_port' => 465,
'smtp_user' => 'xxxx',
'smtp_pass' => 'xxxx',
'mailtype' => 'html',
'charset' => 'utf-8'
);
$this->load->library('email', $this->config->item('email'));
$this->email->set_newline("\r\n");
$this->email->from('xxxx', 'xxxx');
$this->email->to("xxxx");
$this->email->subject('Test Email');
$body = $this->load->view('welcome_message',null,TRUE);
$this->email->message($body);
if (!$this->email->send()){
echo 'fail to load email';
}
else {
echo 'success to send email';
}
}
Setting config options doesn't work unless you initialize them. Add this after your $config array:
$this->email->initialize($config);
Based on what I think you're trying to do:
$this->load->library('email', $this->config->item('email'));
should be
$this->load->library('email', $config);
Try and let us know.