Sender address is not valid when send email Codeigniter - php

I will make mail delivery system using PHP CodeIgniter. Email successfully submitted, but when I check my email, the sender mail is my smtp_user, not the real email sender.
for example: My smtp_user is a#gmail.com. When user send mail to me, sender mail is a#gmail.com, not user#gmail.com.
Here is my code in email.php :
$config['wordwrap'] = TRUE;
$config['protocol'] = "smtp";
$config['smtp_host'] = "ssl://smtp.gmail.com";
$config['smtp_port'] = "465";
$config['smtp_user'] = "a#gmail.com";
$config['smtp_pass'] = "blablabla";
$config['charset'] = "utf-8";
$config['mailtype'] = "html";
$config['priority'] = "1";
$config['newline'] = "\r\n";
And here is my controller :
$this->load->library('email');
$link=base_url()."verification/".$verification_code."";
$body="Thanks for joining <br> Your code verification is
<a href=".$link." target='_blank'>".$link."</a>";
$this->email->from($this->input->post('mails'), strtoupper($name));
$this->email->to('b#gmail.com');
$this->email->subject('New account registration');
$this->email->message($body);
$this->email->send());
What's wrong with my config email?
but if I using smtp_host with mandrillapp, I did'nt see the problem like above.

Related

Codeigniter Email Issue : Your server might not be configured to send mail using this method

When sending emails using codeigniter, the emails are received but the page results in an error.
https://privatebin.net/?d89c7965b55f5712#+Y3hf+PE36m8bROxB/k4llwmcvOykt2JF+m72HhTLGU=
This is the error displayed when trying to send any email from script.
The code used for sending email is
$config['protocol'] = 'smtp';
$config['smtp_host'] = 'mail.domain.com';
$config['smtp_port'] = '587';
$config['smtp_timeout'] = '7';
$config['smtp_user'] = 'mail#domain.com';
$config['smtp_pass'] = '******';
$config['charset'] = 'utf-8';
$config['newline'] = "\r\n";
$config['mailtype'] = 'html'; // or html
$config['validation'] = TRUE; // bool whether to validate email or not
$this->email->initialize($config);
//$this->email->initialize($config);
$this->email->from('mail#domain.com', $system_name);
$this->email->to($email);
$this->email->subject('Title');
$this->email->message($message);
$send=$this->email->send();
This was the response from hosting provider:
We have implemented anti-spoofing policies on our server to curb spoofing, hence you won't be able to use the mail function if the FROM/TO email address is not from the same domain.
I suggest you the following solution to resolve the issue.
Change the FROM address as something#domain.com.
Use SMTP authentication to send emails. [Recommended solution]

Unable to send email using PHP SMTP. Your server might not be configured to send mail using this method

Iam using codeigniter
I exicuted the code on live server.
got the following error using print_debugger()
Unable to send email using PHP SMTP. Your server might not be
configured to send mail using this method.
public function sendEnquiry() {
$this->load->library('email');
$name = $this->input->post("fname");
$cemail = $this->input->post("email");
$pno = $this->input->post("phone");
$message = $this->input->post("message");
$config['protocol'] = 'smtp';
$config['smtp_host'] = 'ssl://mail.gatewaykhobar.com';
$config['smtp_port'] = '465';
$config['smtp_timeout'] = '7';
$config['smtp_user'] = '***********';
$config['smtp_pass'] = '***********';
$config['charset'] = 'utf-8';
$config['newline'] = "\r\n";
$config['mailtype'] = 'text'; // or html
$config['validation'] = FALSE;
$this->email->initialize($config);
$this->email->from('info#gatewaykhobar.com','Gateway Restaurent Contact');
$this->email->to($cemail);
$this->email->subject('Gateway Restaurent Contact Enquiry');
$this->email->message($message);
$send = $this->email->send();
if($send) {
echo json_encode("send");
} else {
$error = $this->email->print_debugger(array('headers'));
echo json_encode($error);
}
}
Change smtp_port from 465 to 587.
Make sure $config['newline'] = "\r\n"; is in double quotes not single quotes.
$mail_config['smtp_host'] = 'smtp.gmail.com';
$mail_config['smtp_port'] = '587';
$mail_config['smtp_user'] = 'user#example.com';
$mail_config['_smtp_auth'] = TRUE;
$mail_config['smtp_pass'] = 'password';
$mail_config['smtp_crypto'] = 'tls';
$mail_config['protocol'] = 'smtp';
$mail_config['mailtype'] = 'html';
$mail_config['send_multipart'] = FALSE;
$mail_config['charset'] = 'utf-8';
$mail_config['wordwrap'] = TRUE;
$this->email->initialize($mail_config);
$this->email->set_newline("\r\n");
I just added the last line
A common cause of this is the way that CodeIgniter interacts with the SMTP server with regards to line breaks. Your SMTP server might require \r\n and CodeIgniter is using \n.
There is an easy fix: after your $this->email->initialize(), add the following:
$this->email->set_newline("\r\n");
That should get it working for you.
Just use "mail" for the 'protocol' array item, and that's all...
$config = array();
$config['useragent'] = $system_name;
$config['mailpath'] = "/usr/bin/sendmail"; // or "/usr/sbin/sendmail"
$config['protocol'] = "mail"; //use 'mail' instead of 'sendmail or smtp'
$config['smtp_host'] = "your domain name";
$config['smtp_user'] = $from;
$config['smtp_pass'] = "*************";
$config['smtp_port'] = 465;
$config['smtp_crypto'] = 'ssl';
$config['smtp_timeout'] = "";
$config['mailtype'] = "html";
$config['charset'] = "utf-8";
$config['newline'] = "\r\n";
$config['wordwrap'] = TRUE;
$config['validate'] = FALSE;
It looks like the mail server is hosted by yourself as well, try sending email from any e-mail client. If it fails - there's a problem with your mailserver config, not the code you pasted - check the server logs.
I am using much time Run my configure code in localhost but it always gives me an error (Unable to send email using PHP SMTP. Your server might not be configured to send mail using this method.)
But when run this Below code in my server it works for me.
application>controller>Sendingemail_Controller.php
public function send_mail() {
$this->load->library('email');
$config = array();
$config['protocol'] = "smtp"; // you can use 'mail' instead of 'sendmail or smtp'
$config['smtp_host'] = "ssl://smtp.googlemail.com";// you can use 'smtp.googlemail.com' or 'smtp.gmail.com' instead of 'ssl://smtp.googlemail.com'
$config['smtp_user'] = "my#gmail.com"; // client email gmail id
$config['smtp_pass'] = "******"; // client password
$config['smtp_port'] = 465;
$config['smtp_crypto'] = 'ssl';
$config['smtp_timeout'] = "";
$config['mailtype'] = "html";
$config['charset'] = "iso-8859-1";
$config['newline'] = "\r\n";
$config['wordwrap'] = TRUE;
$config['validate'] = FALSE;
$this->load->library('email', $config); // intializing email library, whitch is defiend in system
$this->email->set_newline("\r\n"); // comuplsory line attechment because codeIgniter interacts with the SMTP server with regards to line break
$from_email = $this->input->post('f_email'); // sender email, coming from my view page
$to_email = $this->input->post('email'); // reciever email, coming from my view page
//Load email library
$this->email->from($from_email);
$this->email->to($to_email);
$this->email->subject('Send Email Codeigniter');
$this->email->message('The email send using codeigniter library'); // we can use html tag also beacause use $config['mailtype'] = 'HTML'
//Send mail
if($this->email->send()){
$this->session->set_flashdata("email_sent","Congragulation Email Send Successfully.");
echo "email_sent";
}
else{
echo "email_not_sent";
echo $this->email->print_debugger(); // If any error come, its run
}
}
and my view page where I defined f_email and email comes through post method.
application>view>emailtesting.php
<html>
<head>
<title> Send Email Codeigniter </title>
</head>
<body>
<?php
echo $this->session->flashdata('email_sent');
echo form_open('/Sendingemail_Controller/send_mail');
?>
<input type = "email" name = "f_email" placeholder="sender email id (from)" required />
<input type = "email" name = "email" placeholder="reciever email id (to)" required />
<input type = "submit" value = "SEND MAIL">
<?php
echo form_close();
?>
</body>
if some error comes again please visit official documentation below:
https://codeigniter.com/user_guide/libraries/email.html
For anyone else who finds this error, has set the settings mentioned elsewhere (even Codeigniter 4) but still getting tht error, one way to test what is going on is from the server console and using telnet. For example:
telnet smtp.yourprovider.com 587
Then test against a different provider and see if that works. If your provider doesn't but another does then the problem is with your provider and you should contact them. If both can't connect then you should speak with your webhost.
I've found another solution. I had the same problem: (Codeigniter 4 and the issue with php smtp), and the reason was 2-step verification of my gmail account, from which I wanted to send emails in my app. I fixed it by getting from my gmail account, password for my app. It works, at last.

SMTP Host Issue in China

I have an issue regarding smtp using codeigniter email library. It works fine with smtp.gmail.com but I have to use vpn in this case. I found other solutions like smtp.exmail.qq.com, smtp.mail.yahoo.com or smtp.sina.com but when you send the form typing your email in email field its being sent only with the email you provided in smtp user. Further please look at my code and screen shotenter image description here:
//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 =
'info#sample.com';
//configure email settings
$config['protocol'] = 'smtp';
$config['smtp_host'] = 'ssl://smtp.exmail.qq.com';
//'ssl://smtp.googlemail.com';
$config['smtp_port'] = '465';
$config['smtp_user'] = 'myemail#exmail.qq.com';
$config['smtp_pass'] = 'my password';
$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->subject($subject);
$this->email->message($message);

Gsuite account won't send emails codeigniter

I just set up google mx records for my domain. When i use my normal gmail account, emails are sent but when I use my gsuite account, the mails are not sent. I am hosting my site on siteground. However I can receive emails from other email accounts on my gsuite account.
$this->load->library('email');
$subject = 'Subject';
$message = 'My message';
//configure email settings
$config['protocol'] = 'smtp';
$config['smtp_host'] = 'ssl://smtp.gmail.com';
$config['smtp_port'] = 465
$config['smtp_user'] = 'info#mydomain.com';
$config['smtp_pass'] = 'my_password';
$config['mailtype'] = 'html';
$config['charset'] = 'utf-8';
$config['wordwrap'] = TRUE;
$config['newline'] = "\r\n"; //use double quotes
$config['validation'] = TRUE;
$this->email->initialize($config);
//send mail
$this->email->from('info#mydomain.com', 'MyDomainName');
$this->email->to($to_email);
$this->email->subject($subject);
$this->email->message($message);
//echo json_encode($this->email->print_debugger());
if($this->email->send())
return true;
else
return false;
You didn't attach any error message, but since it's working for your other Gmail accounts, try checking if turning on access to "less secure apps" for this account solves this issue and report back.
https://myaccount.google.com/lesssecureapps
(Don't forget to choose the right account, if you're logged in to several ones).
I had similar problem, I had to create a App Password to work (details here: https://support.google.com/accounts/answer/185833?hl=en)
Once the App Password was created I used:
$subject = 'Your subject';
$message = 'Your body';
//configure email settings
$config['protocol'] = 'smtp';
$config['smtp_host'] = 'ssl://smtp.gmail.com';
$config['smtp_port'] = 465;
$config['smtp_user'] = 'your#email.com';
$config['smtp_pass'] = 'your app password';
$config['mailtype'] = 'html';
$config['charset'] = 'utf-8';
$config['wordwrap'] = TRUE;
$config['newline'] = "\r\n"; //use double quotes
$config['validation'] = TRUE;
$this->email->initialize($config);
//send mail
$this->email->from('from#email', 'Title');
$this->email->to('to#email.com');
$this->email->subject($subject);
$this->email->message($message);

How to get send main in my mail account using codeigniter mailer?

I am using codeigniter for sending mail directly..
How to get send main in my mail account using codeigniter mailer?
my Code:
$ci = get_instance();
$ci->load->library('email');
$config['useragent'] = "Codeigniter";
$config['protocol'] = "smtp";
$config['smtp_host'] = "mail.example.com";
$config['smtp_port'] = "25";
$config['smtp_user'] = "account#example.com";
$config['smtp_pass'] = "Password";
$config['charset'] = "utf-8";
$config['mailtype'] = "html";
$config['newline'] = "\r\n";
$ci->email->initialize($config);
$ci->email->from('account#example.com', 'Account');
$list = array($email);
$ci->email->to($list);
$this->email->reply_to('account#example.com', 'Account');
$ci->email->subject('Verify your Email');
$ci->email->message('Massge body contain');
$ci->email->send();
I want to get all send email in account#example.com send mail box. it is possible?
add cc or bcc your email account better bcc
$this->email->cc('account#example');
or
$this->email->bcc('account#example');

Categories