Applying SendGrid filters to CodeIgniter emails - php

I'm trying to integrate SendGrid with my CodeIgniter application to send transactional emails. I've been able to send plain text emails, using the native email library in CodeIgniter and the SendGrid API with this code provided by SendGrid documentation:
<?php
$this->load->library('email');
$this->email->initialize(array(
'protocol' => 'smtp',
'smtp_host' => 'smtp.sendgrid.net',
'smtp_user' => 'sendgridusername',
'smtp_pass' => 'sendgridpassword',
'smtp_port' => 587,
'crlf' => "\r\n",
'newline' => "\r\n"
));
$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();
echo $this->email->print_debugger();
?>
In my version, I've filled in all the parameters, so this works.
What I am trying to do, but haven't been able to, is to apply a filter to the email with a template_id. I've designed a template in SendGrid and want these emails to be sent with that design. How do I tell SendGrid to apply the template?
PHP version: 7,
CodeIgniter version: 3

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

Sending mail to a gmail account via codeigniter fails

I tried to send a email to a Gmail account (both receiver and sender accounts were created by me and both allow access to less secured apps) via CodeIgniter, using the following settings (from my localhost WAMP sever):
$config['smtp_user']='my gmail address';
$config['smtp_pass']='my password';
$config['smtp_port']=465;
$config['smtp_host']='ssl://smtp.googlemail.com';
$config['charset']='utf-8';
$config['protocol'] ='smtp';
The code snippet looks like this :
$this->email->from(my gmail address... , 'admin');
$this->email->to($reciever_email);
$this->email->subject(...);
$this->email->message(...);
$this->email->send();
But the mail does not get send (No weird error messages like fatal error etc. is displayed only the custom message I decided to show on failure is shown).
Can any one tell me why and how to get rid of this problem.
Try this instead:
$config = Array(
'protocol' => 'smtp',
'smtp_host' => 'ssl://smtp.googlemail.com',
'smtp_port' => '465',
'smtp_user' => 'my user',
'smtp_pass' => 'my password',
'smtp_charset' => 'utf-8'
);

Yii framework SMTP mail with gmail from email id not changed

I am using this extension.
http://www.yiiframework.com/extension/smtp-mail
I would work properly. But From Email id in mail not set that i have defined in "SetFrom()" function but it takes Gmail username (myemail#gmail.com).
Below is my code for sending mail in my Controller.
$mail = Yii::app()->Smtpmail;
$mail->SetFrom("otherid",$from_name); // This id not coming in my response mail
$mail->Subject = $subject;
$mail->MsgHTML($message);
$mail->AddAddress($to, "");
config/main.php
'Smtpmail'=>array(
'class' => 'application.extensions.smtpmail.PHPMailer',
'Host' => "smtp.gmail.com",
'Username' => 'myemail#gmail.com',
'Password' => 'password',
'Mailer' => 'smtp',
'Port' => 465,
'SMTPAuth' => true,
'SMTPSecure' => 'ssl'
),
Gmail doesn't seem to allow sending the email via a different email-id if its not registered to the primary gmail account.
When logged in to the primary gmail account -> go to Account tab (top-right).
Search for Add another email address under Send mail as.
Add the new email address there. Confirm the link sent to the additional email account.
On confirmation, your account can send emails from either of the address.
Try the new email address now in $mail->SetFrom("new_gmail_id#gmail.com", $from_name); and it will work fine.
I have tried that and it works. Let us know if you have done all this already.

Cannot send HTML Emails

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

Categories