sending emails form different forms in codeigniter - php

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.

Related

simple mail function not working on server- CodeIgniter

i am new in codeigniter what i am trying to do is fairly simple i am sending a mail using simple email library in code ignitor i have uploaded the code to the server but some how it is not sending the email! here's my code
constructor
function __construct(){
parent::__construct();
$this->load->helper(array('form', 'url'));
$this->load->model('contactus');
$this->load->library('email');
}
i have included the email library
public function contactus()
{
$data = $this->input->post();
$this->contactus->contactus_db($data);
$this->email->to('uneeb#nextcrawl.com');
$this->email->subject('Contact US');
$msg="hey uneeb";
$this->email->message($msg);
if($this->email->send()) {
echo "send";
}
else
{
echo "not send";
}
//redirect('Home');
}
this is my email code i have done some validations that either mail is actually send or not and in this case it is showing send i don't know what is the issue!

CodeIgniter mail not working on server but works fine localhost

i am using Shared hosting plan. I tried my level best but i am not able to resolve this issue.
here is my code. i tried first with gmail but was not working then i read somewhere that may the IP of my shared hosting plan is blacklisted by google then i tired my own smtp then imap server, same results it works fine on localhost but again i am getting the same error.
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Contact extends CI_Controller {
/**
* Index Page for this controller.
*
* Maps to the following URL
* http://example.com/index.php/welcome
* - or -
* http://example.com/index.php/welcome/index
* - or -
* Since this controller is set as the default controller in
* config/routes.php, it's displayed at http://example.com/
*
* So any other public methods not prefixed with an underscore will
* map to /index.php/welcome/<method_name>
* #see http://codeigniter.com/user_guide/general/urls.html
*/
public function __construct()
{
parent::__construct();
$this->load->helper(array('form','url'));
$this->load->library(array('session', 'form_validation', 'email'));
}
public function index()
{
$this->load->helper('security');
//set validation rules
$this->form_validation->set_rules('name', 'Name', 'trim|required|xss_clean|callback_alpha_space_only');
$this->form_validation->set_rules('email', 'Emaid ID', 'trim|required|valid_email');
$this->form_validation->set_rules('subject', 'Subject', 'trim|required|xss_clean');
$this->form_validation->set_rules('message', 'Message', 'trim|required|xss_clean');
//run validation on form input
if ($this->form_validation->run() == FALSE)
{
//validation fails
$this->load->view('head');
$this->load->view('map');
$this->load->view('footer_map');
}
else
{
//get the form data
$name = $this->input->post('name');
$from_email = $this->input->post('email');
$subject = $this->input->post('subject');
$message = $this->input->post('message');
//set to_email id to which you want to receive mails
$to_email = 'example#gmail.com';
//configure email settings
$config['protocol'] = 'imap';
$config['smtp_host'] = 'imap.example.com';
$config['smtp_port'] = '587';
$config['smtp_user'] = 'info#exampl.com';
$config['smtp_pass'] = 'example';
$config['mailtype'] = 'html';
$config['charset'] = 'iso-8859-1';
$config['wordwrap'] = TRUE;
$config['newline'] = "\r\n"; //use double quotes
// $this->load->library('email', $config);
$this->email->initialize($config);
//send mail
$this->email->from($from_email, $name);
$this->email->to($to_email);
$this->email->reply_to($from_email, $name);
$this->email->subject($subject);
$this->email->message($message);
if ($this->email->send())
{
// mail sent
$this->session->set_flashdata('msg','<div class="alert alert-success text-center">Your mail has been sent successfully!</div>');
redirect('contact/index');
}
else
{
//error
echo $this->email->print_debugger();
$this->session->set_flashdata('msg','<div class="alert alert-danger text-center">There is error in sending mail! Please try again later</div>');
redirect('contact/index');
}
}
}
public function alpha_space_only($str)
{
if (!preg_match("/^[a-zA-Z ]+$/",$str))
{
$this->form_validation->set_message('alpha_space_only', 'The %s field must contain only alphabets and space');
return FALSE;
}
else
{
return TRUE;
}
}
}
i am getting this output
There is error in sending mail! Please try again later.
Help me guys, i m tired
On line 73 of the code you posted it looks like the if statement for
$this->email->send()
is failing, which then runs this:
else
{
//error
echo $this->email->print_debugger();
$this->session->set_flashdata('msg','<div class="alert alert-danger text-center">There is error in sending mail! Please try again later</div>');
redirect('contact/index');
}
I would check the configuration in Email.php - in my Codeigniter 3 project it is located here:
html/system/libraries/Email.php
At the bottom of this Codeigniter 3 API page is some info about print_debugger and about the Email library. It should help you get in the right direction.

Why Username and Password not accepted when I send mail in php?

Hi this is my problem:
I want send a mail and this is my mail code:
<?php
require "vendor/autoload.php";
class HelperMail{
private $oPhpMailer;
function __construct(){
$this->oPhpMailer = new PHPMailer();
$this->oPhpMailer->isSMTP();
$this->oPhpMailer->SMTPDebug = 2;
$this->oPhpMailer->Debugoutput = 'html';
$this->oPhpMailer->SMTPSecure = 'tls';
$this->oPhpMailer->SMTPAuth = true;
}
public function mailFrom($from,$usuario){
$this->oPhpMailer->setFrom($from,$usuario);
}
public function mailPort($puerto){
$this->oPhpMailer->Port = $puerto;
}
public function mailUsuario($usuario){
$this->oPhpMailer->Username = $usuario;
}
public function mailPassword($pass){
$this->oPhpMailer->Password = $pass;
}
public function mailHost($host){
$this->oPhpMailer->Host = $host;
}
public function mailSubject($subject){
$this->oPhpMailer->Subject = $subject;
}
public function mailAddress($address){
$this->oPhpMailer->addAddress($address);
}
public function mailAltBody(){
$this->oPhpMailer->AltBody = 'This is a plain-text message body';
}
public function mailHtml(){
}
public function setData ($usuarios){
$htmlMail = $this->oPhpMailer->msgHTML(file_get_contents('helpers/mailAvisoSinTareasReg/contenido.html'));
$htmlMailChange = str_replace("TRABAJO","LAZOS",$htmlMail);
$this->sendMail();
}
public function sendMail(){
if (!$this->oPhpMailer->send()) {
echo "Mailer Error: " . $this->oPhpMailer->ErrorInfo;
} else {
echo "Message sent!";
}
}
}
?>
I have this error :
I know that the user and pass are ok , so i don't know what is the problem.
How can I fix this? , sorry for my english
If you’re receiving the following error:
SMTP -> ERROR:Password not accepted from server. Code: 535 Reply: 535-5.7.1 Please log in with your web browser and then try again.
Even if the Gmail account credentials are correct, Google’s servers may still block the server attempting to authenticate (more than likely due to a new server location and/or recent password change).
To get around this, make sure you are logged in using the same Gmail account in your browser, and then simply open the link below and step through the process of verification:
https://accounts.google.com/b/0/DisplayUnlockCaptcha
This will allow access to your Gmail account for about 10 minutes so that a new authenticating server can be detected. Be sure to try authenticating again within this time period."
SRC: http://www.rocketideas.com/2012/05/gmail-error-password-not-accepted-from-server-solved/
TIP:
You should also take a look at the url (https:/support.google...) presented after the error.
Allow less secure apps in google.
https://myaccount.google.com/lesssecureapps
Error 1: Password Command Failed: 534-5.7.9 Application-Specific Password Required
Answer:
This happens mostly when SMTP Mail Server Credentials were correct but Application Specific Password was not provided.
Error 2: SMTP ERROR: Password Command Failed: 535-5.7.8 Username And Password Not Accepted
Answer:
If you have encountered this error, then this is mostly because the application specific password is incorrect.
GO to Below Link
https://support.google.com/accounts/answer/185833?hl=en
Create App Password by Select app (Other) and copy the generated password and paste this password in place of gmail password.
Allow less secure app access for your gmail account

codeigniter email sent to spam

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

codeigniter email weirdness

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...

Categories