Laravel-why admin receives every email message? - email

I am working on a web application with multiple users, and I have some issue with sending emails.
Sending is working fine, no problems, but the issue is that every mail interaction between visitors and users is sent to admin email address stated in env file. I am using gmail for sending, Laravel 8.
For example, visitor A has sent a message to user B. User B received mail correctly, but also admin received message.
Thanks for help.
Here is my EmailsController
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Mail;
class EmailsController extends Controller
{
public function send(Request $request)
{
function sentence_case($string) {
$sentences = explode(" ",$string);
$sentences = array_reverse($sentences);
$new_string = '';
foreach ($sentences as $key => $sentence) {
$new_string = ucfirst(mb_strtolower(trim($sentence)))." ".$new_string;
}
return $new_string;
}
$this->validate($request, [
'ime' => 'required', //visitor name
'email' => 'required', // visitor email address
'poruka' => 'required' //visitor message
]);
$ime = $request->get('ime');
$ime = sentence_case($ime);
$email = $request->get('email');
$poruka = $request->get('poruka');
Mail::send('emails.message', [
'name' => $ime,
'email' => $email,
'comment' => $poruka ],
function ($m) use ($email) {
$m->from($email);
$m->to('usersemail#yahoo.com', 'MyApp')
->subject('Website Contact Form');
});
/* usersemail#yahoo.com is registered user email address different from aplication admin/owner email addres stated in env file, but admin receives this message */
if (Mail::failures()) {
return response()->Fail('Sorry! Please try again latter');
}else{
return back()->with('success', 'Thanks for contacting me, I will get back to you soon!');
}
}
}

Related

Message: Call to undefined method CI_Email::get_emails() codeigniter 3

I try build function in controller Codeigniter 3 to read emails from inbox and insert to database.
`public function read_emails() {
// Load the email and database libraries
$this->load->library('email');
// Connect to the POP3 server
$config = array(
'protocol' => 'pop3',
'pop3_host' => 'pop.gmail.com',
'pop3_user' => 'xxxxxxxxxxxx',
'pop3_pass' => 'xxxxxxxxxxxxxxxx',
'pop3_port' => 995,
'pop3_encryption' => 'ssl'
);
$this->email->initialize($config);
// Retrieve emails from the inbox
$emails = $this->email->get_emails();
// Loop through the emails
foreach ($emails as $email) {
// Check if the email was received within the last 24 hours
$received_time = strtotime($email['received_time']);
$current_time = time();
if ($current_time - $received_time <= 86400) {
// Check if the email already exists in the database
$this->db->where('email', $email['email']);
$query = $this->db->get('contacts');
if ($query->num_rows() == 0) {
// Insert the email into the database
$data = array(
'email' => $email['email'],
'subject' => $email['subject'],
'message' => $email['message']
);
$this->db->insert('contacts', $data);
}
}
}
}`
When I run this controller I get output:
Message: Call to undefined method CI_Email::get_emails()
Can anyone please help me debug this issue? Very important.
line 43:
// Retrieve emails from the inbox
$emails = $this->email->get_emails();
I don't know if codeigniter3's email class has such a get_emails() function, or if you can retrieve emails from the inbox.
Please check the official documentation: https://codeigniter.com/userguide3/libraries/email.html
If it is a custom class/plugin/addon, you should either publish its code or submit a link to the repo

How to send email with dynamic sender in laravel?

I am creating a simple contact us from in Laravel.
I set up .env with my Gmail account. I can send email from Laravel with my email address, no problem with this.
But, I want to send email from sender address who is sending the message form contact us form.
Here is my code in controller:
public function sendMessage(Request $request)
{
$this->validate($request,[
'email'=>'required|email',
'subject'=>'required',
'body'=>'required|max:150'
]);
$body = $request['body'];
Mail::send('emails.support',['body' => $body], function($message){
$email = Input::get('email');
$subject = Input::get('subject');
$message->sender($email);
$message->to('smartrahat#gmail.com','Mohammed');
$message->subject($subject);
});
return redirect('contactUs');
}
Though, I am getting email address form contact us form, the email always sent form my email account which I configured in .env
I would have done some things different. I guess $body is the email-text? You should put this in a array and add it as a parameter in Mail::send (or directly put $request->all() as a parameter.
Also, inside of the closure of Mail, i don`t thinks its very nice to put logic there (like $email=Input::get). It does not look right if you ask me.
Didn`t test this code, but this should work:
public function sendMessage(Request $request)
{
$this->validate($request,[
'email' => 'required|email',
'subject' => 'required',
'body' => 'required|max:150'
]);
$data = [
'email' => $request->input('email'),
'subject' => $request->input('subject'),
'body' => $request->input('body')
];
Mail::send('emails.support', $data, function($message) use ($data)
{
$message->from($data['email']);
$message->to('smartrahat#gmail.com','Mohammed');
$message->subject($data['subject']);
});
return redirect('contactUs');
}
Because you add $data as as 'use', you can access this data inside the closure. The $data send as a parameter, can be used in the email blade. In this example, you can access the data like this: $email , $subject , $data, in blade these will output the values.
But you can also do it like this:
Mail::send('emails.support', $request->all(), function($message) use ($data)
public function reset_password(Request $request)
{
$user=User::whereEmail($request->email)->first();
if(count($user) == 1)
{
$data = array('name'=>"Virat Gandhi");
$subject=$request->email;
mail::send(['emails'=>'reset_set'],$data,
function($message)use($subject)
{
$message->from('your#gmail.com','kumar');
$message->to($subject);
$message->subject($subject);
});
echo "Basic Email Sent. Check your inbox.";
}
}
foreach ($emails as $value1)
{
$to[]=implode(',',$value1['0']);
}
$subject=$request->input('sub');
$mailsend=Mail::send('email.subscribe',$mail_content,
function($message)use($to,$subject)
{
$message->from('ganarone#ganar.io','Ganar');
$message->to($to);
$message->subject($subject);
});

Laravel4: check if the inserted email is in a proper format

In my app there is a little form with subscription to newsletter.
The form has just one field: email.
I want that when the entry is not in a proper email format, instead of throwing a laravel error: Address in mailbox given [fghfghfhf] does not comply with RFC 2822, 3.6.2.
I want to, more elegantly, give a validation error.
How can i define this rule?
Thanks!
EDIT:
This is my NewsletterUser model:
class NewsletterUser extends Eloquent {
protected $table = 'newsletterusers';
protected $guarded = array();
public static $rules = array(
'email' => 'required | email | unique:newsletterusers'
);
public static $messages = array(
'email' => 'You already subscribed',
'empty' => 'Insert the mail, please'
);
}
And this is the subscription method in the Controller:
public function subscription()
{
$input = Input::all();
$validation = Validator::make($input, NewsletterUser::$rules, NewsletterUser::$messages);
if($validation->passes())
{
// subscription stuff
}
return Redirect::back()->withInput()->withErrors($validation)->with('message','Insert the mail, please');
}
Just define a custom error message:
$messages = array(
'email' => 'Address in mailbox given :attribute does not comply with RFC 2822, 3.6.2.',
);
$validator = Validator::make($input, $rules, $messages);

email sending from localhost in cakephp using emailcomponent

<?php
class EmailsController extends AppController
{
var $uses=null;
var $components=array(
'Email'=>array(
'delivery'=>'smtp',
'smtpOptions'=>array(
'host'=>'ssl://smtp.google.com',
'username'=>'username#gmail.com',
'password'=>'password',
'port'=>465
)
));
function sendEmail() {
$this->Email->to = 'Neil <neil6502#gmail.com>';
$this->Email->subject = 'Cake test simple email';
$this->Email->replyTo = 'neil6502#gmail.com';
$this->Email->from = 'Cake Test Account <neil6502#gmail.com>';
//Set the body of the mail as we send it.
//Note: the text can be an array, each element will appear as a
//seperate line in the message body.
if ( $this->Email->send('Here is the body of the email') ) {
$this->Session->setFlash('Simple email sent');
} else {
$this->Session->setFlash('Simple email not sent');
}
$this->redirect('/');
}
}
?>
above code is my controller responsible for sending emails...
but when i run this function sendEmail() using url http://localhost/authentication/emails/sendemail it shows nothing not even single error or any response... complete blank page. I don't know the reason.
I think I had the same issue a while ago. It might be that you need to change the to address into a value that holds just the address, so instead of Name <email#example.com> you should use email#example.com.
You can check for errors by logging (or debugging) the smtp errors with:
$this->log($this->Email->smtpError, 'debug');
or
debug($this->Email->smtpError);
Good luck. Hope this helps.
/* Auf SMTP-Fehler prüfen */
$this->set('smtp_errors', $this->Email->smtpError);
I would add the Email Config to your email.php file located in /app/Config/email.php , if it doesn't exist copy email.php.default to email.php, Change the smtp settings there
public $smtp = array(
'host' => 'ssl://smtp.gmail.com',
'port' => 465,
'username' => 'my#gmail.com',
'password' => 'secret'
);
At the top of your Controller above class EmailsController extends AppController add,
App::uses('CakeEmail', 'Network/Email');
Then in your function sendEmail() try,
$Email = new CakeEmail();
$Email->from(array('me#example.com' => 'My Site'))
->to('you#example.com')
->subject('About')
->send('My message');
To test emails what I usually do is send them to the Cake Logs,
**In /app/Config/email.php, include: ( The log output should be /app/tmp/logs/debug.log )
public $test = array(
'log' => true
);
Also doing this add 'test' to your $Email variable like,**
$Email = new CakeEmail('test');
Actually in my case : I got a error message "SMTP server did not accept the password."
After that i follow the below link and issue has been resolved :
Step1 : https://blog.shaharia.com/send-email-from-localhost-in-cakephp-using-cakeemail/
Step2 : Sending Activation Email , SMTP server did not accept the password

Sending email to gmail with CodeIgniter displays "message sent" but there nothing in the inbox?

I'm following this amazing nettusts+ tutorial about sending a email to gmail.
It aparently worked. When I load the page it says: 'Your email was sent, fool.' but then I check my gmail and there's nothing there.
Does CodeIgniter take care of everything? Or I have to install smtp or something in my PC because I'm using localhost (LAMP in Ubuntu)?
Code:
/* SEND EMAIL WITH GMAIL */
class Email extends Controller {
function __construct()
{
parent::Controller();
}
function index()
{
$config = Array(
'protocol' => 'smtp',
'smtp_host' => 'ssl://smtp.googlemail.com',
'smtp_port' => 465,
'smtp_user' => 'janoochen#gmail.com',
'smtp_pass' => '***',
);
$this->load->library('email', $config);
$this->email->set_newline("\r\n");
$this->email->from('janoochen#gmal.com', 'Alex Chen');
$this->email->to('janoochen#gmal.com');
$this->email->subject('This is an email');
$this->email->message('It is working. Great!');
if($this->email->send())
{
echo 'Your email was sent, fool.';
}
else
{
show_error($this->email->print_debugger());
}
}
}
I don't know if this is it, but in your code you've got #gmal.com instead of #gmail.com