Mailing Service - php

I am doing a project using Codeigniter (Mobile Website). I want users to receive mails such as registration, forgot password, newsletters etc. Currently what I am doing is like :
function sendEmail($email) {
$config = Array(
'protocol' => 'smtp',
'smtp_host' => 'ssl://smtp.googlemail.com',
'smtp_port' => '465',
'smtp_user' => 'user#gmail.com',
'smtp_pass' => 'password',
'mailtype' => 'html',
'charset' => 'iso-8859-1'
);
$this->load->library('email', $config);
$this->email->set_newline("\r\n");
$this->email->from('user#gmail.com', 'user');
$this->email->to($email);
$this->email->subject('Thank You for Registering');
$this->email->message('Thank You for Registering .');
if($this->email->send()){
echo "Success";
}
else {
echo "failed";
}
}
This works well. But is this the right way of doing things when you want to send mails of 2-3 types ? Please help me on this.

I would recommend to create email.php file in ./application/config folder and put your config array in that file. So when you change your email settings it will reflect in entire application. For more details visit Email class in codeigniter.

I would recommend not using Gmail smtp for a web application in production. Instead use premium (paid) services like www.mailgin.com where you can also use your own domain for sending, etc.
Or set up your own email server.
I would also recommend using a framework for sending the emails, for example www.swiftmailer.org

Related

Codeigniter 3 Unable to send email using PHP SMTP. Your server might not be configured to send mail using this method

public function send_mail_verification(){
$this->load->library('email');
$config = array(
'protocol' => 'smtp',
'smtp_host' => 'smtp.gmail.com',
'smtp_port' => '587',
'smtp_crypto' => 'tls',
'smtp_user' => 'myacc#gmail.com',
'smtp_pass' => 'mypass',
'charset' => 'iso-8859-1'
);
$this->email->initialize($config);
$this->email->from('myemail3#gmail.com', 'Registration');
$this->email->to('tosomeonemail#gmail.com');
$this->email->subject('something');
$this->email->message('Testing email.');
if($this->email->send()){
echo "success";
}
else{
show_error($this->email->print_debugger());
}
}
I've been looking at other solution that I find in the internet and none of them worked for me.
I've also tried the mail function and changed some things in sendmail.ini and php.ini in xampp and that worked. However, the send mail configuration in codeigniter-3 is much better if I'm going to share my code to other so that they don't need to change some configuration in their sendmail.ini and php.ini. What do you think causes the error?
$this->email->set_newline("\r\n");
I added this one and it worked... wonder why.

Codeigniter 3: Email not sending via third party [duplicate]

This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 5 years ago.
my webiste is hosted in infinityfree and I tried send emails and I'm not receiving any. This is the code that I used
$config = Array(
'protocol' => 'smtp',
'smtp_host' => 'ssl://smtp.googlemail.com',
'smtp_port' => 465,
'smtp_user' => 'xxx',
'smtp_pass' => 'xxx',
'mailtype' => 'html',
'charset' => 'iso-8859-1'
);
$this->load->library('email', $config);
$this->email->set_newline("\r\n");
// Set to, from, message, etc.
$result = $this->email->send();
Do I have to setup something in google or in the control panel of my website? thank you
This is the simplest way to send a email in codeigniter
$this->load->library('email');
$this->email->from('your#example.com', 'Your Name');
$this->email->to('someone#example.com');
$this->email->cc('another#another-example.com');
$this->email->bcc('them#their-example.com');
$this->email->subject('Email Test');
$this->email->message('Testing the email class.');
$this->email->send();
By using this code, you can check the mail functionality. You can also follow the codeigniter email class
Codeigniter email class
Try this
First, load the library
$this->load->library('email');
$this->email->from('test#abc.com', 'Sender Name');
$this->email->to('to#gmail.com');
$this->email->subject('Sample');
$this->email->message('Email testing');
Use condition to check if email send or not
if($this->email->send()){
echo 'Scuuess';
}else{
echo 'Fail';
}

Sending Mail Using Gmail In Codeigniter

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.

Say Email sent but nothing is recieved when sent email using Codeigniter

I have used this several ways but can't seem to get it working. I am running it on live server.
I am using bigrock hosting and as per their document it says just use "localhost" as smtp host. Link
It says email sent but I get nothing..
public function index() {
$config = array(
'protocol' => 'smtp',
'smtp_host' => 'localhost',
'smtp_port' => '587',
// Other way it say Email sent
//'smtp_host' => 'ssl://localhost',
//'smtp_port' => '465',
'smtp_user' => 'support#abc.com',
'smtp_pass' => 'sdfsfsdfsd'
);
$this->load->library('email', $config);
$this->email->set_newline("\r\n");
$this->email->from('support#abc.com', 'ABC');
$this->email->to('abc#gmail.com');
$this->email->subject('Test');
$this->email->message('Success');
if ($this->email->send()) {
echo 'Email Sent';
} else {
show_error($this->email->print_debugger());
}
}
Thanks in advance.
This line: $this->email->to('abc#gmail'); make it 'abc#gmail.com' and make sure it is existing..If it still doesn't work, try a legit email from inside you email->from.

SMTP success but the email is not delivered

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!

Categories