I'm looking for a nice way to send mails on my site.
I've been struggling for hours now, because i randomly get chars in my mails and sometimes it breaks the HTML.
I'm using codeigniter (V3.0) but it was the same deal when i was in 2.x.
Here's the most important part of my code :
$configs = array(
'protocol' => 'smtp',
'smtp_host' => 'smtp.my_server.fr',
'smtp_user' => 'contact#my_server.fr',
'smtp_pass' => 'my_pass',
'smtp_port' => 'my_port',
'mailtype' => 'html',
'_bit_depths' => 'base64',
'_encoding' => 'base64'
);
$this->CI->load->library("email", $configs);
$this->CI->email->set_newline("\r\n");
$this->CI->email->to($mail);
$this->CI->email->from("contact#my_server.fr", "My server");
$this->CI->email->subject( $objet );
$this->CI->email->message( $mail_html)) );
$this->CI->email->send();
I just don't get it ... I'm pretty sure, if i was able to send mail in base_64 it would be okay but i'm missing something !
If someone have any idea about that, i would really appreciate.
Thanks in advance.
If you have the same problem, i found an amazing php library on GitHub.
This saved my day : https://github.com/PHPMailer/PHPMailer
Really simple to use and mails are well encoded ...
Hope this help some day,
cheers
Related
I'd like to let the user edit the email account that will be sending notifications on a CodeIgniter web app. I usually use something like:
$config = Array(
'protocol' => 'smtp',
'smtp_host' => 'ssl://smtp.googlemail.com',
'smtp_port' => 465,
'smtp_user' => 'xxx',
'smtp_pass' => 'xxx', // hardcoded unhashed password
'mailtype' => 'html',
'charset' => 'utf8'
);
$this->load->library('email', $config);
$this->email->set_newline("\r\n");
But that is hardcoded and won't let the user configure or edit on admin's options. I was thinking on calling this from the database but then the email password will be exposed (not hashed) in the table, right?
Is there another way to make it configurable and secure?
Because you need to pass the raw password via SMTP, a hashed password will not work. You will need to use 2-way encryption. You can use the codeigniter encryption/decryption class for more security: http://codeigniter.com/user_guide/libraries/encryption.html
Should be as simple as:
$plain_text = 'This is a plain-text message!';
$ciphertext = $this->encryption->encrypt($plain_text);
// Outputs: This is a plain-text message!
echo $this->encryption->decrypt($ciphertext);
Additionally, asking for GMAIL passwords is rather insecure. I'd look to using an OAUTH approach, to have users approve your application: https://github.com/google/gmail-oauth2-tools
First of all, I would like to thank StackOVerflow and its users for helping me solve a number of technical problems. I created this account today since I could not see similar problem being addressed.
The problem - I am stuck with a problem with sending emails in PHP CodeIgniter. The function $this->email->send() is taking around 3-4 seconds each time to execute.
We have a social platform where users come and post things. Every time the user uploads a new update we want to send an email to him/her.
The problem is the send email function takes around 3-4 seconds and we would love to bring it down under 1 second.
IS there any way wherein we can execute the send email process in parallel ? Say a python code which runs continuously to fetch new updates and send emails to those users. Any better method than that ?
Technology stack - PHP CI, MySQL, Apache, Windows
This is the email code that gets called on every new update -
$config = Array(
'protocol' => 'smtp',
'smtp_host' => 'smtp.gmail.com',
'smtp_port' => 587,
'smtp_user' => 'mailid#gmail.com',
'smtp_pass' => 'password',
'mailtype' => 'html',
'charset' => 'utf-8',
'smtp_crypto' => "tls"
);
$this->load->library('email', $config);
$this->email->set_newline("\r\n");
$this->email->set_mailtype("html");
$this->email->from('mailid#gmail.com', 'Support');
$this->email->to($this->session->userdata['email']);
$this->email->subject('You have posted a new update');
$this->email->message('Please login to check your new update. Do not reply to this email. For any issues reach out to email#emailid.com');
if (!$this->email->send())
{
show_error($this->email->print_debugger());
}
else
{
echo true;
}
If your function is working faster but email sending is slower, it must be problem for your smtp connection. Either your server is not much faster to connect SMTP quickly or the SMTP you are using bit slower to response.
In that case, try with a gmail SMTP and see the result.
It's my suggestion to store the emails in a database and send the emails using a cron job.
The uploading user should not wait to send emails one at a time .
If you need code for a cron job or an smtp mailer, add a comment to this answer.
I have created a codeigniter 3 helper to send mails from different modules of application. I am using my gmail account to send all the mails and it works perfectly fine except that i have to store my gmail password inside the helper php file.
$config = Array(
'protocol' => 'smtp',
'smtp_host' => 'ssl://smtp.googlemail.com',
'smtp_port' => 465,
'smtp_user' => 'saurabh#gmail.com',
'smtp_pass' => 'my password',
'mailtype' => 'html',
'charset' => 'iso-8859-1'
);
$this->load->library('email', $config);
$this->email->set_newline("\r\n");
$result = $this->email->send();
How can i avoid storing the plain text password in the above code while still able to use my gmail account. Same is true for my database password, I am storing plaintext database password in the codeigniter's database.php configuration file. How this can be avoided ?
Database password cannot be hidden or connvert it in other way. But how ever no one can access your config files with the browser.
And in email password can be done. You can keep it in database and on mail sending place you can call it.
FYI - your project files cant access with browser or any other. Thats the reason we use frameworks. Bcz its security level is higher than normal oop pograms.
So your password and other stuff will not be public at any how.
The only way I see it possible is to use Gmails OAUTH mechanism instead of smtp method. here's the link to the api https://developers.google.com/identity/protocols/OAuth2 and some examples https://developers.google.com/gmail/xoauth2_libraries#php_sample. Rest assured you will have to put your plain password somewhere in your app (either in DB or at a location in file system which no one has access to except apache) or write a crypt function (even this function requires/ outputs the plain password)
I'm trying to send email using codeIgniter's email library.
Below is my code
$config = Array(
'protocol' => 'smtp',
'smtp_host' => 'smtp.myserver.com',
'smtp_user' => 'xxx#myserver.com',
'smtp_pass' => '********',
'mailtype' => 'html',
'wordwrap' => false,
'charset' => 'utf-8'
);
$this->email->initialize($config);
$this->email->set_newline("\r\n");
$this->email->from('xxx#yourserver.com', 'xxx');
$this->email->subject('blah blah');
$this->email->message('a simple html message');
$this->email->to('any#validemail.com');
$this->email->send();
Code is on diffrent server & it is using different server's mail (Yes, two different domains)
For example code is on yourserver.com and it is using smtp of myserver.com
It was working fine till morning. but now i'm getting
554 SMTP synchronisation error (see attachment for full print_debugger() output, I hide some sensitive information. i can trust you guys but not all)
Thanks.
I tried very hard to get the issue, but i have not enough access to get details of error log or mail logs, So i changed my code as below and it worked
$config = Array(
'mailtype' => 'html',
'wordwrap' => false,
'charset' => 'utf-8'
);
$this->email->initialize($config);
$this->email->set_newline("\r\n");
$this->email->from('xxx#yourserver.com', 'xxx');
$this->email->subject('blah blah');
$this->email->message('a simple html message');
$this->email->to('any#validemail.com');
$this->email->send();
I would like to say thanks to #cherrysoft for his help offering. I'm posting it here so it may help someone.
thanks to all.
This isn't really a CodeIgniter problem and there could be quite a few things going on here but the first thing I would do is to make sure that the hostname on the web server is set correctly and that your mail server recognises this host. You should tail the mail.log on your mail server (if you have access to do this) whilst you are sending mail from your web server and you will get some more insight as to what is going on. When you have the mail log entries post here and we can look at the issue in more depth.
Well I'm trying to send a HTML email using gmail smtp from CI, and it seems to reject my emails when they have any amount of tables. No error is given, they just do not appear in my inbox.
If I send an email with light HTML and no tables, they go through. Anyone have any insight?
$config = Array(
'protocol' => 'smtp',
'smtp_host' => 'ssl://smtp.googlemail.com',
'smtp_port' => 465,
'smtp_user' => '------#gmail.com',
'smtp_pass' => '--------',
'mailtype' => 'html',
'charset' => 'iso-8859-1'
);
$this->load->library('email', $config);
$this->email->set_newline("\r\n");
$this->email->from('myEmail', 'myName');
$this->email->to($this->input->post('email'));
$this->email->subject('mySubject');
$msg = $this->load->view('partials/email', '', true);
$this->email->message($msg);
$this->email->send();`
Using PHP double quotes evaluate variables and control characters (e.g. \n or \r), whereas single quotes do not. Also open the way to different interpretation = (equal sign).
It not using TABLE is generation problem it is any tag with parameter ="value".
Make sure you have right string in single quotes, test this on basic A tag
The content of view 'partial/email' is the key
Include also the text/plain version of the message. Maybe it will go through, if you say that light html works