Laravel dynamic email not sending by Mailable Class - laravel

In my case, In the Mailable Class I store the email in the variable hardcode form LIKE
$email = 'abc#example.com';
then, its work fine. but when we pass the email dynamically from the Controller LIKE
$this->email;
this shows the error are given below :
Expected response code 250 but got code "550", with message "550 The from address does not match a verified Sender Identity. Mail cannot be sent until this error is resolved. Visit https://sendgrid.com/docs/for-developers/sending-email/sender-identity/ to see the Sender Identity requirements "
$subject = 'Account Created Successfully';
$name = 'Saakin Qatar';
return $this->view('emails.AgencyRegisterMail')
->from($address, $name)
->cc($address, $name)
->bcc($address, $name)
->replyTo($address, $name)
->subject($subject)
->with('inputs', $this->inputs);
It works but when I pass the address from the controller and store it like below:
$subject = 'Account Created Successfully';
$name = 'Saakin Qatar';
$name = 'Saakin Qatar';
return $this->view('emails.AgencyRegisterMail')
->from($address, $name)
->cc($address, $name)
->bcc($address, $name)
->replyTo($address, $name)
->subject($subject)
->with('inputs', $this->inputs);
then it returns the error

Twilio SendGrid developer evangelist here.
The error message here said:
Visit https://sendgrid.com/docs/for-developers/sending-email/sender-identity/ to see the Sender Identity requirements
On the page it recommends you visit it describes that to send emails you need to send them from either an email address that has been verified or an email address from an authenticated domain.
My guess is that you are building a contact form of sorts and trying to send the mail from an email address that a user enters into the form. If that is the case, you cannot use the user provided email address as the from parameter for sending the email, you have to use an email address that is verified or authenticated. But you can set the user's email address as the replyTo so that when you reply from your email client you write back to the user.

Related

Laravel Email Greeting show Name instead of ID

Hello I wanted to ask regarding the Email Notification on Laravel, I have made a Status Notification wher I added this code
public function toMail($notifiable)
{
$leaveStatus=$this->leave->status;
if($leaveStatus==1){
$leaveNotify=" approved ";
}
else{
$leaveNotify=" declined ";
}
return (new MailMessage)
->subject('Leave Status')
->greeting('Hello '.$this->leave->user_id)
->line('I hope you are doing well')
->line('Your requested leave of' . $this->leave->type. ' type has been ' .$leaveNotify)
->line('Leave type: ' .$this->leave->type)
->line('From dt :' .$this->leave->from)
->line('To dt :' .$this->leave->to);
}
This works very well the email is being sent in each change on a Leave Application so what I am looking for is the part of the greeting , the
greeting('Hello '.$this->leave->user_id)
It shows the ID of the user instead of the first_name(which is a filed for the name) I have tried adding a ->first_name after the user_id but then it returns an error, the user_id doesn't have a foreign key that connects it with the users table its just a field which stores the id of each authenticated users but it works all the way here so im not sure that is the problem
You can use the notifiable variable passed into the function, which is the user the email is being sent to.
->greeting('Hello '.$notifiable->first_name)
You need to add relation for user in that model. See https://laravel.com/docs/7.x/eloquent-relationships . After that call it via $this->leave->user->first_name

Send automatical email after save to database

I am new here.
I have a project in Laravel. I have one textarea and data from it is save in datavase. It works good. Now I would like to send automatical email to one specific email address with this data. It must be sent only one time with save to database.
I have no problem with sending email to customer with data but now I need to send email with data from this textarea to one specific email. It is a textarea what we have to buy for customer. It must be sent to our cooperation company.
Is it possible?
Ofcourse this is possible!
You should take a look at the following resources :
Observers
https://laravel.com/docs/6.0/eloquent#observers
Notifications
https://laravel.com/docs/6.0/notifications
-> specifically : https://laravel.com/docs/6.0/notifications#mail-notifications
yes, you can just trigger your function after saving: for example, after saving in controller.
public function store(Request $request){
$var = new Property; //your model
$var->title=$request->title; // the input that being save to database.
$var ->save();
// Send email to that input
Mail::send('email',['email'=>$request->title],function ($mail) use($request){
$mail->from('info#sth.com');
$mail->to($request->title);
});
return redirect()->back()->with('message','Email Successfully Sent!');
}

Sent Mail checking if it is delivered and open to recipient in Laravel

I want help in scaffolding code with Laravel default Mail package to send an email to the recipient with an enhancement that checks the status either mail is delivered to recipient and then check that either recipient opened the mail or not and then change the status of that email in my db_email_list. I googled it that to add headers just like follow the example but could not get it how to get the status
$message->getHeaders()->addTextHeader('X-Confirm-Reading-To','recipient_mail');
$sendEmail->getHeaders()->addTextHeader('Disposition-Notification-To','recipient_mail');
$sendEmail->getHeaders()->addTextHeader('Return-Receipt-To','recipient_mail');
When the user has gotten the email: Simply use this piece of code:
if (count(Mail::failures())) {
return false;
} else {
return true;
}
true=delivered, false=not delivered
When user reads the email: Hm sounds like you need to include a trick in your email in order to know if user has opened/read the email by simply adding for instance including an image on your email with a route defined in your end and passing user id as query param.
<img src="http://www.example.com/user-read-email?user_id=20" />
So whenever the user opens the email img src will fire a call to your url and simmply get the user id from the url, and set the flag for that user in db.

Laravel Mail the from and replyTo are ignored in the received email

I have problem with the Laravel's Mail. I have nothing wrong with the source code to send an email. The email was sent but the email address of sender is mine instead of sender. It is the one in the config file. The replyTo was ignored also.
My code is as following:
Mail::send('appointments.emails.new_appointment', ['data' => $data], function($message) use ($sender)
{
$message->from($sender['email'], $sender['name'])
->to('myemail#gmail.com', 'My Name')
->replyTo($sender['email'], $sender['name'])
->subject('Contact from ' . $sender['name']);
});
I received an email with sender name and my email address. And, when I click on rely, it replys to myself. That means the replyTo didn't work. How can I fix that?
Sorry if my English is not so good.

sending email to all newsletter members with codeigniter

I want to send an email using codeigniter library to my newsletter members and I want to mention each members' Email address in email's content. For doing that, I used foreach loop to send the email one by one. The problem is that the code sends email to just one member ( the first member ) of my newsletter. I've checked my code which gets members from the database and it printed out all the members.
This is my model:
function send_news()
{
$subscribers = $this->get_subscriber_data();
foreach($subscribers as $subscriber)
{
$this->load->helper('typography');
//Format email content using an HTML file
$data['news_Title'] = $this->input->post('news_Title');
$HTML_Message = $this->load->view('admin/includes/newsletter_html_format', $data, true);
$config['mailtype'] = 'html';
$this->email->initialize($config);
$this->email->from('newsletter#site.com', 'newsletter of site.com');
$this->email->to($subscriber->subscriber_Email);
$this->email->subject('newsletter of site.com');
$this->email->message($HTML_Message);
return $this->email->send();
}
}
and this is how I'm getting subscribers list:
function get_subscriber_data($options = array())
{
$query = $this->db->get('mg_newsletter');
if(isset($options['subscriber_Id']))
return $query->row(0);
return $query->result();
}
when I try to echo $subscriber->subscriber_Email it prints all the emails in the database one after another. but it does not send email to all of them. What am I doing wrong?!
use
$this->email->clear()
As codeignitor says:
Initializes all the email variables to an empty state. This function is intended for use if you run the email sending function in a loop, permitting the data to be reset between cycles.
foreach ($list as $name => $address)
{
$this->email->clear();
$this->email->to($address);
$this->email->from('your#example.com');
$this->email->subject('Here is your info '.$name);
$this->email->message('Hi '.$name.' Here is the info you requested.');
$this->email->send();
}
If you set the parameter to TRUE any attachments will be cleared as well. Then let us know
You are "return" -ing inside your loop, which exits the function.
Just send
You can keep the loading helper file and email configuration outside the loop and then remove the return from for loop, it will send to all subscribers one by one and then after loop finishes you can do the return statement.

Resources