Cannot send HTML Emails - php

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

Related

Add 'From' Header on email using Codeigniter

I'm working with Codeigniter in CPanel and my code already sends a mail, but when it gets to the receiver, the hostname is shown on the sender.
I tried some answer to questions as : Change the sender name php mail instead of sitename#hostname.com but in Codeigniter, they don't work.
This is my code:
$config = Array(
'protocol' => 'ssmtp',
'smtp_host' => 'ssl://ssmtp.googlemail.com',
'smtp_port' => 465,
'smtp_user' => 'mail#domainiwant.com',
'smtp_pass' => 'password',
'mailtype' => 'html',
'charset' => 'iso-8859-1',
'useragent' => 'MY NAME',
);
$this->load->library('email', $config);
$this->email->set_newline("\r\n");
$this->email->from('mail#domainiwant.com', 'MY NAME');
$email_to = 'receiver#gmail.com';
$this->email->to($email_to);
$this->email->message('Message testing ...');
$this->email->send();
However, as I said, when the mail gets to the receiver, they appear with the hostname and a completely different mail address like the one I put on $config
I know this only sets the envelope sender but I want to set the mail address to be mail#domainiwant.com instead of receiving the mail with somemail#host.com.ex
According to the documentation of CodeIgniter's email library available HERE, your whole problem is a simple typo.
$config['protocol'] allows mail, sendmail and smtp as values. If you don't set the variable, or use a value which is not allowed, the whole library defaults to mail which attempts to use your own server as the mail gateway (which explains why your sender address shows as username#servername)
Change the protocol from ssmtp to smtp so that you actually use the Google SMTP server you intend to use and you'll get the results you expect

Configurable SMTP account in Codeigniter app

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

How to avoid storing mail account and database password in plain text in codeigniter 3

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)

PHP email : Weird chars

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

Codeigniter Email - `554 SMTP synchronisation error

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.

Categories