I am trying to send email via codeigniter framework via 2 different ways and on one of them my email goes to junk folder but its really weird...
so my first try was to create a Controller and put inside the index function the following code:
$this->load->library('email');
$this->email->from('no-reply#domain.example.com', 'Your Name');
$this->email->to('email#example.com');
$this->email->subject('Email Test');
$this->email->message('Testing the email class.');
$this->email->send();
echo $this->email->print_debugger();
this way the email arrives fine and not as spam...
when i create a model so i can use it later on for other features to send emails i replace the above code with the following one:
$this->load->library('email');
$this->load->model("email_model");
$this->email_model->sendEmail(null, 'email#gmail.com', 'title', 'message');
and email_model Model contains the following code :
<?php
class email_model extends CI_Model {
function __construct()
{
// Call the Model constructor
parent::__construct();
}
public function sendEmail($from = null, $to = null, $subject = null, $message = null){
$this->email->from('no-reply#domain.com', 'from user');
$this->email->to($to);
$this->email->subject($subject);
$this->email->message($message);
$this->email->send();
}
}
why does that happen?
check your spf records and RDNS records
SPF
here is an article # mediatemple kb: http://kb.mediatemple.net/questions/658/How+can+I+create+an+SPF+record+for+my+domain%3F#gs
RDNS
here is an article on RDNS:
http://www.crucialp.com/resources/tutorials/web-hosting/how-reverse-dns-works-rdns.php
seems content was the reason on the 2nd try... adding more text solved the issue...
Related
I need some help pls.
I'm building this website using C.I 3 and it has like 3 different forms with 3 different pages (career, quote, contact) where users can fill them up and i will receive an email but only contact page is working and the remain two pages prompt and error (Unable to send email using PHP mail(). Your server might not be configured to send mail using this method) while they all fall under the same controller. can anyone help me solve this issue pls?
below is the link to my codes
http://pastebin.com/REFjPsUf
Thanks
defined('BASEPATH') OR exit('No direct script access allowed');
class Site extends CI_Controller {
public function index(){
$this->home();
}
public function home(){
$this->load->view("home_header");
$this->load->view("site_nav");
$this->load->view("slider");
$this->load->view("home_content");
$this->load->view("footer");
}
public function aboutUs(){
$this->load->view("home_header");
$this->load->view("site_nav");
$this->load->view("slider");
$this->load->view("content_about");
$this->load->view("footer");
}
public function projects(){
$this->load->view("home_header");
$this->load->view("site_nav");
$this->load->view("slider");
$this->load->view("content_project");
$this->load->view("footer");
}
public function services(){
$this->load->view("home_header");
$this->load->view("site_nav");
$this->load->view("slider");
$this->load->view("content_service");
$this->load->view("footer");
}
public function careers(){
$this->load->view("home_header");
$this->load->view("site_nav");
$this->load->view("slider");
$this->load->view("content_career");
$this->load->view("footer");
}
public function gallery(){
$this->load->view("home_header");
$this->load->view("site_nav");
$this->load->view("slider");
$this->load->view("content_gallery");
$this->load->view("footer");
}
public function register(){
$this->load->view("home_header");
$this->load->view("site_nav");
$this->load->view("slider");
$this->load->view("quote");
$this->load->view("footer");
}
public function contact(){
$this->load->view("home_header");
$this->load->view("site_nav");
$this->load->view("contact_header");
$this->load->view("content_contact");
$this->load->view("footer");
}
public function sendmail(){
$this->load->library('email');
$this->email->from($this->input->post("email"), $this->input->post("name"));
$this->email->to("me#example.com");
$this->email->subject('from website contact form');
$this->email->message($this->input->post("message"));
if ( ! $this->email->send())
{
echo $this->email->print_debugger(); // Generate error
}
else{
$this->load->view("home_header");
$this->load->view("site_nav");
$this->load->view("contact_header");
$this->load->view("contact_success");
$this->load->view("footer");
}
}
public function quotation(){
$this->load->library('email');
$this->email->from($this->input->post("email"));
$this->email->to("me#example.com");
$this->email->subject($this->input->post("subject"));
$this->email->message($this->input->post("message"));
if ( ! $this->email->send())
{
echo $this->email->print_debugger(); // Generate error
}
else{
$this->load->view("home_header");
$this->load->view("site_nav");
$this->load->view("slider");
$this->load->view("quote_success");
$this->load->view("footer");
}
}
public function mycareer(){
$this->load->library('email');
$this->email->from($this->input->post("email"));
$this->email->to("me#example.com");
$this->email->subject($this->input->post("subject"));
$this->email->message($this->input->post("message"));
if (!$this->email->send()){
echo $this->email->print_debugger();
}
else{
echo "Email sent";
}
}
}
there is an error in your code in last two functions "quotation() and mycareer()" missing of 2nd argument in $this->email->from()
reference link
$this->email->from($this->input->post("email") , 'Your Name');
hope it works for you.
**try on smtp config in ci as
$this->email->initialize(array(
'protocol' => 'smtp',
'smtp_host' => 'xxxx.ipage.com',//SMTP servern name
'smtp_user' => 'user#xxxx.com',
'smtp_pass' => 'SMTP user password',
'smtp_port' => 587,//port
'crlf' => "\r\n",
'mailtype' => 'html',
'newline' => "\r\n",
'wordwrap' => TRUE
));
$this->email->from($email,$name);
$this->email->to('admin#xxxx.com');
$this->email->cc('user#xxxx.com');
$this->email->bcc('xxxxx#gmail.com');
$this->email->subject('Email Test');
$this->email->message($message1);
if($this->email->send())
echo "<script>alert('Thank you... mail Sent');</script>";
else
echo "<script>alert('Not Sent Please Try again');</script>";
redirect('xxxx/abc');
OR you can use PHP-mail class also.. just download from GitHub or in php official site. then paste it in view folder then include it.. that's it.
<?php
//SMTP needs accurate times, and the PHP time zone MUST be set
//This should be done in your php.ini, but this is how to do it if you don't have access to that
//date_default_timezone_set('Etc/UTC');
require __DIR__.'/phpmailer/PHPMailerAutoload.php';
//Create a new PHPMailer instance
$mail = new PHPMailer();
//Tell PHPMailer to use SMTP
$mail->isSMTP();
//Enable SMTP debugging
// 0 = off (for production use)
// 1 = client messages
// 2 = client and server messages
$mail->SMTPDebug = 2;
//$mail->DKIM_domain = '160.153.33.231';
//Ask for HTML-friendly debug output
$mail->Debugoutput = 'html';
//Set the hostname of the mail server
$mail->Host = "mail.xxxx.com";
//Set the SMTP port number - likely to be 25, 465 or 587
$mail->Port = 465;
//Whether to use SMTP authentication
$mail->SMTPAuth = true;
//Username to use for SMTP authentication
$mail->Username = "info#xxxxx.com";
//Password to use for SMTP authentication
$mail->Password = "xxxxx";
$mail->SMTPSecure = 'ssl';
if(isset($from,$sender)){
//Set who the message is to be sent from
$mail->setFrom($from, $sender);
}
else $mail->setFrom("info_#xxxxx.com", 'from Website');
//Set an alternative reply-to address
//$mail->addReplyTo('info_#xxxx.com', 'First Last');
//Set who the message is to be sent to
$mail->addAddress('care#xxxxxx.com', 'xxxxxx.com');
//$mail->AddBCC('xxxx#gmail.com','');
//Set the subject line
$mail->Subject = $subject;
//convert HTML into a basic plain-text alternative body
$mail->msgHTML($body_tmp);
//Replace the plain text body with one created manually
$mail->AltBody = 'This is a plain-text message body';
//Attach an image file
//$mail->addAttachment('images/phpmailer_mini.png');
//send the message, check for errors
if (!$mail->send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}
//echo $body_tmp;
?>**
You also have a very common error a lot of people have with "Contact Us" forms.
$this->email->from($this->input->post("email"));
This will break SPF and also cause DMARC to fail and you will never get the message from some people, if your mail server you use has DMARC enabled on it.
Since DMARC is a more recent protocol, a lot of the old cookie cutter code for contact us forms - doesn't take this into account.
You can read more about that here: "DMARC - Contact Form Nightmare"
The suggested workaround will be to do:
$this->email->from("me#example.com");
$this->email->subject($this->input->post("subject") . $this->input->post("email") );
This way - you avoid the issue outline in the article. You won't quickly be able to hit the "Reply" button, but at least you'll get the emails from those customers who have DMARC enabled.
I am sending a template html in codeigniter and in fact my email is working perfectly fine.
My problem is how i send a template of html to the email. I got an error because of the variables passed to.
public function send_email_accountability($C11, $C4, $C8,$C12)
{
date_default_timezone_set('Asia/Manila');
$message = $this->load->view('sample_html',$data,TRUE);
$this->load->library('email');
$this->email->set_mailtype("html");
$this->email->from('noreply#email.com', 'EMAIL');
$this->email->to($C11);
$this->email->subject('Accountability for'. $C12);
$this->email->message($message);
}
I receive the email but inside the email is php error because the $data cannot be passed to. I looked at other stackoverflow problems and it didn't help my problem.
public function send_email_accountability($C11, $C4, $C8,$C12)
{
date_default_timezone_set('Asia/Manila');
$this->load->library('email');
$data['title']='HELLO WORLD';
$this->email->from('noreply#email.com', 'EMAIL');
$this->email->to($C11);
$this->email->subject('Accountability for'. $C12);
$this->email->set_mailtype("html");
$msg = $this->load->view('sample_html',$data,TRUE);
$this->email->message($msg);
$this->email->send();
}
Try this one
use PHPmailer. It will help You. I also use it. You can get all information about PHPmailer from here
you are using a data array ($data) to the view, but no values is assigned to it,
just assign any value to array to avoid the error,
$data['logo_img'] = 'images/logo.png'; // add this line before loading the view
This logo image is located as $logo_img in the view sample_html...
I am using the codeigniter email class to send emails when the user register to my website or perform some activity inside my website.
The thing is that, with this simple code below I can only send very simple HTML emails. And also I will have to pass the entire HTML message to the function. Is there any way so that I could load the entire template in the function itself and just pass a message which is to be placed inside the template as we do in codeigniter with views.
$message = 'Thank you for your order \n';
$message .= 'We will contact you soon';
$this->email($message);
public function email($message = NULL){
$this->load->library('email');
$this->email->mailtype('html');
$this->email->from('abbbba#gmail.com', 'Sohan Kc');
$this->email->to('abbbbaa#gmail.com');
$this->email->subject('Email Test');
$this->email->message($message);
$this->email->send();
}
Why don't you just do as you already stated? :)
The email classes message method requires a string to be passed. Just load a View and pass the returned string to the method.
public function email($message = NULL){
$this->load->library('email');
$mydata = array('message' => 'I am a message that will be passed to a view');
$message = $this->load->view('my_email_template', $mydata, true);
$this->email->mailtype('html');
$this->email->from('sohanmax2#gmail.com', 'Sohan Kc');
$this->email->to('sohanmax02#gmail.com');
$this->email->subject('Email Test');
$this->email->message($message);
$this->email->send();
}
I send emails but got the html code as body, I had to add the headers to get correct content like below and it will works out.
$this->email->set_header('MIME-Version', '1.0; charset=utf-8');
$this->email->set_header('Content-type', 'text/html');
my model:
<?php
class email_model extends CI_Model {
function __construct()
{
// Call the Model constructor
parent::__construct();
}
public function sendEmail($from = null, $to = null, $subject = null, $message = null){
$this->load->library('email');
$config['protocol'] = 'sendmail';
$config['mailpath'] = '/usr/sbin/sendmail';
$config['charset'] = 'utf-8';
$config['mailtype'] = 'html';
$config['wordwrap'] = TRUE;
$this->email->initialize($config);
$this->email->from('email#mydomain.com', 'Website name');
$this->email->to($to);
$this->email->subject($subject);
$this->email->message($message);
$this->email->send();
}
}
my domain is hosted on GoDaddy also i have created an email there and redirect it to my gmail email...
why does my email is sent to spam folder and not on Inbox?
EDIT: my email content is basically an invitation welcome
Hello $email
$website has invited you to join the website
to join visit the following link
$link_goes_here
Thanks, the team
If you are using HTML to display your mail, you have to ensure that your mail is sent with valid html.
You can validate your mail here: http://validator.w3.org/
if it, a contact us form..
maybe you can try to send an email through Amazon SES, so google / yahoo didn't think it as a spam
This is email sendind code
function send_letter()
{
$description = $this->input->post('description',true);
$this->load->model('newsletter_model');
$this->data['mail_list'] = $this->newsletter_model->getmaillist();
$this->email->from('ashitha10#gmail.com', 'Imageinit');
$this->email->subject('Email Test');
$this->email->message($description);
foreach($this->data['mail_list'] as $val)
{
$this->email->set_newline("\r\n");
$this->email->to($val['emailid']);
$this->email->send();
}
$this->index();
}
The email sends successfully to all emailids..But subject like "nosubject"
Also an error shows
Security:Notice
Message:Undefined Index:subject
FileName:libraries/email.php
line Number:941
content in line 941 in email.php is
if ($this->protocol == 'mail')
{
$this->_subject = $this->_headers['Subject'];
unset($this->_headers['Subject']);
}
Apparently the code which unsets the Subject header is in the method _write_headers, which is called from _build_message which is called from send. So every time you call send with mail as backend your subject gets erased (but the first mail is okay, since the subject gets stored in $this->_subject but that gets overwritten with an empty string in a subsequent call too).
This seems like a bug in the email.php, but as a workaround you could just set the subject in every iteration of the loop (so just move $this->email->subject('Email Test'); inside of the foreach-loop, maybe after the to-call).
Use this code
function send_letter()
{
$description = $this->input->post('description',true);
$this->load->model('newsletter_model');
$this->data['mail_list'] = $this->newsletter_model->getmaillist();
foreach($this->data['mail_list'] as $val)
{
$this->email->from('ashitha10#gmail.com', 'Imageinit');
$this->email->subject('Email Test');
$this->email->message($description);
$this->email->set_newline("\r\n");
$this->email->to($val['emailid']);
$this->email->send();
}
$this->index();
}