Codeigniter: unable to send email - php

I have a "Contact Us" page, where if users submit the form. Then email should go to the specified email.
After submiting the form i am getting the Success message ,but i am not seeing any email in my inbox/spam.
I am testing on my live server.
Pelase help me to solve my problem.
My code:
<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class contact extends CI_Controller {
public function __construct()
{
parent:: __construct();
$this->load->helper('url');
$this->load->helper('form');
$this->load->library('session');
$this->load->library('email');
$this->load->model('shopmodel');
$this->load->model('contactusmodel');
$this->load->library('form_validation');
}
function index()
{
$this->form_validation->set_error_delimiters(' <li class="errorlist">', '</li>')->set_rules('fullname', 'Name','trim|required|min_length[5]|max_length[50]|xss_clean');
$this->form_validation->set_error_delimiters('<li class="errorlist">', '</li>')->set_rules('countryname', 'Country','trim|required|min_length[2]|max_length[50]|xss_clean');
$this->form_validation->set_error_delimiters('<li class="errorlist">', '</li>')->set_rules('email', 'Email', 'trim|required|valid_email');
$this->form_validation->set_error_delimiters('<li class="errorlist">', '</li>')->set_rules('contactdetails', 'Contact Details','trim|required|min_length[40]|max_length[2000]|xss_clean');
$data=$this->contactusmodel->contactusmodel();
$data["query"] = $this->shopmodel->getshopdetailsById(199);//taking data as shop id 199
if($this->form_validation->run() === FALSE)
{
$data['ffullname']['value'] = $this->input->post('fullname');
$data['fcountryname']['value'] =$this->input->post('email');
$data['femail']['value'] = $this->input->post('countryname');
$data['fcontactdetails']['value'] =$this->input->post('contactdetails');
$this->load->view('contact/contact',$data);
}
else if ($this->form_validation->run() === TRUE)
{
$name=$this->input->post('fullname');
$sendersemail=$this->input->post('email');
$fromcountry=$this->input->post('countryname');
$message=$this->input->post('contactdetails');
$config = Array(
'protocol' => 'smtp',
'smtp_host' => 'ssl://smtp.googlemail.com',
'smtp_port' => 465,
'smtp_user' => 'ashutosh10g#gmail.com',
'smtp_pass' => 'xxxxxxxx',
'mailtype' => 'html',
'charset' => 'utf-8',
'wordwrap' => TRUE
);
$this->load->library('email', $config);
$this->email->set_mailtype("html");
$this->email->set_newline("\r\n");
$email_body ="<div>hello world</div>";
$this->email->from('ashutosh10g#gmail.com', 'ddd');
$list = array('ashutosh10g#gmail.com');
$this->email->to($list);
$this->email->subject('Testing Email');
$this->email->message($email_body);
$this->email->send();
echo $this->email->print_debugger();
}
else{
$this->load->view('contact/contact',$data);
}
}
}
?>
What output i am getting is:
[code]
Your message has been successfully sent using the following protocol: mail
From: "ddd"
Return-Path:
Reply-To: "ashutosh10g#gmail.com"
X-Sender: ashutosh10g#gmail.com
X-Mailer: CodeIgniter
X-Priority: 3 (Normal)
Message-ID: <513e1456185d4#gmail.com>
Mime-Version: 1.0
Content-Type: multipart/alternative; boundary="B_ALT_513e1456185e3"
=?utf-8?Q?Testing_Email?=
This is a multi-part message in MIME format.
Your email application may not support this format.
--B_ALT_513e1456185e3
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 8bit
hello world
--B_ALT_513e1456185e3
Content-Type: text/html; charset=utf-8
Content-Transfer-Encoding: quoted-printable
<div>hello world</div>
--B_ALT_513e1456185e3--

Maybe you are experimenting a relaying error somtimes the Google Server don't accept relay mail form one MTA to another inbox. Try to test to establish an SMTP dialog to your server an try to send the mail. Here is a simple SMTP dialog example:
http://www.soi.wide.ad.jp/class/20000009/slides/11/6.html
Try to send a mail if the server sends a relay error you should try with another SMTP server. If not maybe is a missconfiguration of your php.
But in my experience gmail always deny the relaying mail for spam and security reasons if the MTA is not a trusted agent.
Another reason may be that you don't have correctly configure DNS Servers and your MTA cannot find the MX Records. Example y your MTA is sendmail it notifies php tha the mail was send succesfully but if you look into sendmail logs you can find that the host is unreachable.

Related

Can't send server emails in CodeIgniter 4

I just hanged up to send server emails.
I'm preparing email sending code like this:
$email_config = Array(
'charset' => 'utf-8',
'mailType' => 'html'
);
$email = \Config\Services::email();
$email->initialize($email_config);
$email->setNewline("\r\n");
$email->setCRLF("\r\n");
$email->setFrom("test#mysite.com", "Sender name");
$email->setTo("receiver#gmail.com");
$email->setSubject("Test message");
$email->setMessage("Hello");
if ($email->send()) {
echo "Email sent!";
} else {
echo $email->printDebugger();
return false;
}
It's showing this error message:
Unable to send email using PHP mail(). Your server might not be configured to send mail using this method.
Date: Thu, 4 Jun 2020 05:21:47 -0500
From: "Sender name" <test#mysite.com>
Return-Path: <test#mysite.com>
Reply-To: <test#mysite.com>
User-Agent: CodeIgniter
X-Sender: test#mysite.com
X-Mailer: CodeIgniter
X-Priority: 3 (Normal)
Message-ID: <5ed8cb3ba9e500.94682473#mysite.com>
Mime-Version: 1.0
Content-Type: multipart/alternative; boundary="B_ALT_5ed8cb3ba9e702.65790334"
=?UTF-8?Q?Test=20message?=
This is a multi-part message in MIME format.
Your email application may not support this format.
--B_ALT_5ed8cb3ba9e702.65790334
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Hello
--B_ALT_5ed8cb3ba9e702.65790334
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable
Hello
--B_ALT_5ed8cb3ba9e702.65790334--
And giving an error in error log:
Email: sendWithMail throwed Use of undefined constant INTL_IDNA_VARIANT_UTS46 - assumed 'INTL_IDNA_VARIANT_UTS46' (this will throw an Error in a future version of PHP)
I want to mention that,
intl extension is enabled in server.
I'm using cPanel.
It was working fine on Codeigniter 3.
The mail function is working with the row method in my server like this:
$to = 'receiver#gmail.com';
$subject = 'Test message';
$message = 'Hello';
$headers = 'From: test#mysite.com' . "\r\n" .
'Reply-To: test#mysite.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
Please help.
Thanks in advance.
You need to setup an MTA on your server (Mail Transfer Agent).
For instance: Postfix or exim, and in some cases nullmailer will do the trick
Possibly you can connect to an SMTP relay of your provider
codeigniter provide alternative types ( Mail, Sendmail and SMTP ) check your cpanel outgoing email configuration or ask your provider to check for php's mail() configuration
I ran into this today, and was surprised to see the library failed out of the box. Seems to be an issue in CI's email lib here:
/**
* Validate email for shell
*
* Applies stricter, shell-safe validation to email addresses.
* Introduced to prevent RCE via sendmail's -f option.
*
* #see https://github.com/codeigniter4/CodeIgniter/issues/4963
* #see https://gist.github.com/Zenexer/40d02da5e07f151adeaeeaa11af9ab36
* #license https://creativecommons.org/publicdomain/zero/1.0/ CC0 1.0, Public Domain
*
* Credits for the base concept go to Paul Buonopane <paul#namepros.com>
*
* #param string $email
*
* #return boolean
*/
protected function validateEmailForShell(&$email)
{
if (function_exists('idn_to_ascii') && $atpos = strpos($email, '#'))
{
$email = static::substr($email, 0, ++$atpos)
. idn_to_ascii(static::substr($email, $atpos), 0, INTL_IDNA_VARIANT_UTS46);
}
return (filter_var($email, FILTER_VALIDATE_EMAIL) === $email && preg_match('#\A[a-z0-9._+-]+#[a-z0-9.-]{1,253}\z#i', $email));
}
I don't have time to investigate what this method is all about (wasted a few hours on this already!), but was able to bypass it and send emails successfully.
Create App/Libraries/Email.php to override the problematic method:
<?php namespace App\Libraries;
class Email extends \CodeIgniter\Email\Email{
protected function validateEmailForShell(&$email){
return TRUE;
}
}
Then make the service return your subclass in App/Config/Services.php:
public static function email(bool $getShared=TRUE){
return $getShared ? static::getSharedInstance('email') : new \App\Libraries\Email();
}
Got a solution from CI forum.
From phpinfo() > intl > ICU version was 4.6 in my case. Which is too old.
Updating that to the latest version worked for me.

Codeigniter 3 SMTP with Office 365

I am having difficulties sending email via SMTP using CI3 and Office 365.
My website is hosted with BT.
I found this sample code which I am using, it's the minimum test code for successful email sending over SMTP with Office 365.
In my contact controller I have the following code;
class Contact extends CI_Controller {
public function test(){
$config = [
'protocol' => 'mail',
'smtp_host' => 'smtp.office365.com',
'smtp_user' => 'MY_EMAIL',
'smtp_pass' => 'MY_PASSWORD',
'smtp_crypto' => 'tls',
'newline' => "\r\n",
'smtp_port' => 587,
'mailtype' => 'html'
];
$this->load->library('email', $config);
$this->email->from('MY_FROM_EMAIL');
$this->email->to('MY_TO_EMAIL');
$this->email->subject('Test');
$this->email->message('SMTP sending test');
$sent = $this->email->send();
if ($sent) {
echo 'OK';
} else {
echo $this->email->print_debugger();
}
}
}
When I visit my URL example.com/contact/test I see the following error;
A PHP Error was encountered
Severity: Warning
Message: fsockopen(): unable to connect to smtp.office365.com:587
(Connection timed out)
Filename: libraries/Email.php
Line Number: 1990
Backtrace:
File: .../controllers/Contact.php Line: 118 Function: send
File: ../index.php Line: 341 Function: require_once
Then below that;
The following SMTP error was encountered: 110 Connection timed out
Unable to send email using PHP SMTP. Your server might not be
configured to send mail using this method.
User-Agent: CodeIgniter
Date: Tue, 14 Nov 2017 15:08:27 +0000
etc ...
etc ...
etc ...
I know the login details are correct for the email account as I am able to login and send/receive emails using various email clients.
Any ideas why this is happening, and how I can fix it? Are there any other tests I can carry put in order to help troubleshoot the issue.
I've been stuck at this for days. I don't want to use gmail or any other email provider.

My Mails Is Always Going To Spam Folder Even Delivering Very Late In Codeigniter Php

I'm Trying To Send Mails From My Web Server Using SMTP PROTOCOL in codeigniter frame work.my mails was always delivering late and going to spam.i had follow many past conservations,still i didn't solve this problem.help me folks to solve this problem.here is my code:
$config = Array('protocol' => 'smtp',
'smtp_host' => 'http://smtp.tfas.net/',
'smtp_port' => 465,
'smtp_user' => 'info#tfas.net',
'smtp_pass' => 'xxxxxxx',
'mailtype' => 'html',
'mailpath' => '/usr/sbin/sendmail',
'charset' => 'utf-8'
);
$this->load->library('email', $config);
$this->email->set_newline("\r\n");
if (file_exists($attachment_path))
{
$this->email->attach("$attachment_path");
$this->email->from('info#tfas.net','TFAS MEMBER PORTAL');
$this->email->reply_to('info#tfas.net','Help Center');
$this->email->message($message);
$this->email->to($to);
$this->email->subject('Greetings From TFAS Member Portal');
}
else
{
$this->email->from('info#tfas.net','TFAS MEMBER PORTAL');
$this->email->reply_to('info#tfas.net','Help Center');
$this->email->message($message);
$this->email->to($to);
$this->email->subject('Greetings From TFAS Member Portal');
}
if($this->email->send())
{
return true;
}
else
{
return false;
}
// Even I had gone through this link https://github.com/ivantcholakov/codeigniter-phpmailer which is a tutorial to send mails with phpmailer library in codeigniter but same problem araises..
I don't think this is an issue with codeigniter's mailer. Usually, emails are marked as spam by email service providers based on the content or an IP filter. In your case it's possible that your content appear spam-like due to not having relevant text and images. Google and most providers has a list of spam words that they look out for when evaluating the spam-ness of your emails. Check this out as it may be help you to find a useful lead: http://blog.hubspot.com/blog/tabid/6307/bid/30684/The-Ultimate-List-of-Email-SPAM-Trigger-Words.aspx

Accept email address from user and send email to that address

public function send_mail() {
$this->email->from('your#email.com', 'My Name');
$this->email->to('to#email.com');
$this->email->cc('cc#email.com');
$this->email->subject('Email Test');
$this->email->message('Testing the email class.');
$this->email->send();
echo $this->email->print_debugger();
}
i'm trying out the codeigniter email func but i cant seem to receive the test mail, but it saids that it has successfully send the mail but there's none,
new to ci so i'm confused about this problem, $this->load->library('email'); is also loaded,
anyhelp thanks!
EDIT
ok guys thanks for the answer i have added the ff code and it now works,
$config = Array(
'protocol' => 'smtp',
'smtp_host' => 'ssl://smtp.googlemail.com',
'smtp_port' => 465,
'smtp_user' => 'your email',
'smtp_pass' => 'your password'
);
Now my question is how will I edit the codes above to accept the "to" email from the user?
okay i did the "to" part already, and its like this
$this->email->to($point->email);
need help again with the "message" part, i need to put data from the user in the "message" part, i tried what i did in the "to" part but it only accepts one item, i need multiple items, thanks
In the section where you're calling the function send_mail(), pass the email address and other data as well, as arguments to the function; something like this:
$to = $point->email;
$msg = $point->message; // the message part
$phone = $point->phone; // phone number
$address = $point->address; // postal address
$this->send_email($to, $msg, $phone, $address);
And then slightly modify the send_mail() function as:
// $to_address will contain the value stored in $to, i.e. "abc#def.com" in this case
public function send_mail($to_address, $body_message, $phone_no, $addr) {
// can make use of extra data like phone number, address in here
$this->email->from('your#email.com', 'My Name');
$this->email->to($to_address);
$this->email->message($body_message);
.
.
.
}
It works fine in my linux server without any $config.
Please read this to set another protocol

CodeIgniter/TankAuth forgot_password function sending emails with no content

Recently moved an existing CodeIgniter product to its own VPS, everything is working away. However, for some reason a password has been invalidated and when the forgot_password function is run the email arrives with an intact subject line, however, the emails have no content. At all. This is the offending code:
function _send_email($type, $email, &$data) {
$this->load->library('email');
$this->email->from($this->config->item('webmaster_email', 'tank_auth'), $this->config->item('website_name', 'tank_auth'));
$this->email->reply_to($this->config->item('webmaster_email', 'tank_auth'), $this->config->item('website_name', 'tank_auth'));
$this->email->to($email);
$this->email->subject(sprintf($this->lang->line('auth_subject_' . $type), $this->config->item('website_name', 'tank_auth')));
$this->email->message($this->load->view('email/' . $type . '-html', $data, TRUE));
echo $this->load->view('email/' . $type . '-html', $data, TRUE);
$this->email->set_alt_message($this->load->view('email/' . $type . '-txt', $data, TRUE));
$this->email->send();
}
I wrote a short PHP script to send an email using the same CodeIgniter library:
<?php
if(!defined('BASEPATH'))
exit('No direct script access allowed');
class EmailTest extends CI_Controller{
function __construct(){
parent::__construct();
$this->load->helper(array('form', 'url'));
$this->load->library('form_validation');
$this->load->library('tank_auth');
$this->lang->load('tank_auth');
}
function sendTest(){
$this->load->library('email');
$this->email->from($this->config->item('webmaster_email', 'tank_auth'), $this->config->item('website_name', 'tank_auth'));
//$this->email->reply_to($this->config->item('webmaster_email', 'tank_auth'), $this->config->item('website_name', 'tank_auth'));
$this->email->to('cameron.j.leafe#gmail.com');
$this->email->subject("Here and There");
$this->email->message("I hear you are an example");
$this->email->send();
echo $this->email->print_debugger();
}
}
?>
That email lands completely fine, and produces this output:
User-Agent: CodeIgniter
Date: Mon, 14 Apr 2014 18:55:55 +1000
From: "EPI Dashboard" <epi#epidashboard.com>
Return-Path: <epi#epidashboard.com>
Reply-To: "epi#epidashboard.com" <epi#epidashboard.com>
X-Sender: epi#epidashboard.com
X-Mailer: CodeIgniter
X-Priority: 3 (Normal)
Message-ID: <534ba29b87010#epidashboard.com>
Mime-Version: 1.0
Content-Type: multipart/alternative; boundary="B_ALT_534ba29b8708b"
=?utf-8?Q?Here_and_There?=
This is a multi-part message in MIME format.
Your email application may not support this format.
--B_ALT_534ba29b8708b
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 8bit
I hear you are an example
--B_ALT_534ba29b8708b
Content-Type: text/html; charset=utf-8
Content-Transfer-Encoding: quoted-printable
I hear you are an example
--B_ALT_534ba29b8708b--
The server is a CentOS 6 VPS running postfix and obviously PHP/MySql/Apache.
Any thoughts on what to check or any resources that you can think of would be greatly appreciated.
The PHP.INI file for this server need to have the line:
sendmail_path = /usr/sbin/sendmail -t -i
Which fixed the problem instantly. Not entirely sure as to why parts of the email were showing up and not the body, but it works.

Categories