'From' field in Laravel 4 Mail::send() - php

I am creating some booking system and have HTML form in which user enter his email (and other stuff). I would like to get email as if user has send it from his email address, but my Gmail always shows that I have sent it (i.e. sender email address is same as 'username' from app/config/mail.php = XYZ#gmail.com).
This is Send::mail() method I am using
Mail::send('reservation-email', ['data' => $data], function($message) use ($data) {
$message->from($data['email'], $data['name']);
$message->to('ABC#gmail.com', 'ABC')->subject($data['subject']);
});
This is my app/config/mail.php
return array(
'driver' => 'smtp',
'host' => 'smtp.gmail.com',
'port' => 587,
'from' => array('address' => null, 'name' => null),
'encryption' => 'tls',
'username' => 'XYZ#gmail.com',
'password' => 'XYZ_password',
'sendmail' => '/usr/sbin/sendmail -bs',
'pretend' => false,
);
I found out that I could use replyTo
Mail::send('reservation-email', ['data' => $data], function($message) use ($data) {
$message->from($data['email'], $data['name']);
$message->to('ABC#gmail.com', 'ABC')->subject($data['subject']);
$message->replyTo($data['email'], $data['name']); // <--------------------
});
but then I do not know what is the purpose of 'from' field?
Thank you in advance!

Maybe you are confuse of the purpose of 'from' and 'replyTo'.
'from' will show to your users where they received email from (It can be set in your Mail::send method in each email or in your app/config/mail.php to apply to all emails).
'replyTo' will define which address that users would send their email to when they click 'reply' button.

Related

Unable to use the dynamic mail configs in Laravel

I want to override the default mail settings in config/mail.php or .env which Laravel uses by default. I'm setting up the dynamic configs (coming from DB) but email when sent is still going from the "from" address set in the config/mail.php or .env.
Any suggestion for the above words??
Here's the part of the code I'm using:
$mailConfigs = [
'driver' => 'smtp',
'port' => $dbSettings->smtp_port,
'host' => $dbSettings->smtp_host,
'encryption' => $dbSettings->smtp_encryption,
'username' => $dbSettings->smtp_email,
'password' => $dbSettings->smtp_password,
'sendmail' => '/usr/sbin/sendmail -bs',
'from' => [
'address' => $dbSettings->smtp_email,
'name' => 'XXXXX'
],
];
Config::set('mail', $mailConfigs);
// print_r(Config::get('mail'));
// Config here are reflected correctly if printed but when email is sent, it again uses the "from" address which is setup in .env and totally ignores the one setup above.
$res = Mail::send([], $data, function ($message) use($mailConfigs)
{
// email body, etc...
});

Web Server not sending email

I face a serious issue that I can't send emails to my clients when I update something on my website. When someone contacts me using the website contact form it doesn't come to my email but it does store in the database. It worked fine before 10th March 2015, after this time sending email with Laravel mostly doesn't work. I have tried to configure it in different ways but haven't found a solution.
My Mail Config: /public_html/app/config/mail.php
'driver' => 'mail',
'host' =>'smtp.gmail.com',
'port' => 597,
'from' => array(
'address' => 'my email address',
'name' => 'Awesome site'
),
'encryption' => 'tls',
'username' => 'my gmail username',
'password' => '',
'sendmail' => '/usr/sbin/sendmail -bs',
'pretend' => false,
and my " /public_html/app/controllers" route main codes are -
$validation = Validator::make($contact_info, $rules);
if ($validation->passes())
{
$insert = Contact::create($contact_info);
$data = array('name' => $name, 'email' => $email_id, 'message' => $contactmessage);
Mail::send('emails.welcome', $data, function($message) use ($email_id, $name)
{
$message->from($email_id, $name);
$message->to('My Email', 'My Subject')->subject('New Contact from user');
});
}
If anyone knows about this problem please help me. Thank You.

Laravel - Gmail SMTP, not sending mail through - from address

I configured my website to send mails through gmail's SMTP. My websiite is running in laravel - 4 framework. Below is the code in config>>mail.php
'driver' => 'smtp',
'host' => 'smtp.gmail.com',
'port' => 587,
'from' => array('address' => null, 'name' => null),
'encryption' => 'tls',
'username' => 'xxx#somedomain.com',
'password' => 'xxxxxx',
'sendmail' => '/usr/sbin/sendmail -bs',
'pretend' => false
And this is a sample of code, on how I send mail,
$from = 'sender#somedomain.com';
$mail = Mail::send('mailer_template', ['any_data' => $data], function ($msg) use ($from, $from_name, $to_email, $subject, $attach) {
$msg->from($from,$from_name);
$msg->to($to_email);
$msg->subject($subject);
$msg->attach($attach);
});
When I send a mail, the mail is being sent. But it is sent from - 'xxx#somedomain.com'. I want it to be sent through the email in $from variabble. And the from address will change at different places. I am not sure how to configure this. Any help would be appreciated.
Have you visited this URL yet? If not, go there and follow the instructions then try to sign in again with your application on the production server.
https://accounts.google.com/displayunlockcaptcha
Try to solve it this way. Keep it mind.
Here password is not mandatory.
Keep blank for encryption field.
use smtp-relay.gmail.com.
return array(
'driver' => 'smtp',
'host' => 'smtp-relay.gmail.com',
'port' => 25, //25, 465 or 587
'from' => array('address' => 'xxx#gmail.com', 'name' => 'myname'),
'encryption' => '',
'username' => 'xxx#gmail.com',
'password' => '',
'sendmail' => '/usr/sbin/sendmail -bs',
'pretend' => false,
);
This doesn't seems feasible as SMTP(gmail) only use one from email (whose detials are added to config/env).In your case whenever you send a mail, the mail is being sent from - 'xxx#somedomain.com'.

How to pass emails info stored in array to Mail::send using Gmail in Laravel 4.2

I want to use Laravel 4 to send emails. The list of emails and user names are retrieved from mysql like below:
[{"user_id":1,"first_name":"foo","last_name":"bar","email":"foobar#email.com"}, ..
I am able to retrieve the first name, last name and email from mysql but how do I pass it to Mail:send function and use Gmail to send out the emails? The mail config has been set to a default settings and some emails are sent out using a different sender name and email.
app/config/mail.php
return array(
'driver' => 'smtp',
'host' => 'smtp.gmail.com',
'port' => 465,
'from' => array('address' => 'my_gmail_username.gmail.com', 'name' => 'Test Email'),
'encryption' => 'ssl',
'username' => 'my_gmail_username',
'password' => 'my_gmail_password',
'sendmail' => '/usr/sbin/sendmail -bs',
'pretend' => false,
app/routes.php
Mail::send('mail_template1', array('name' => 'Laravel'), function($message) use ($arr_index)
{
$message->to('name#email.com', 'name')
->from('different_email#gmail.com', 'Another name')
->subject('Laravel Email Test 1');
});
You need to just pass the values in and loop through your list of users to send to
$users = //Get your array of users from your database
foreach($users as $user)
{
Mail::send('mail_template1', array('name' => 'Laravel'), function($message) use ($user)
{
$message->to($user->email, $user->first_name.' '.$user->last_name)
->from('different_email#gmail.com', 'Another name')
->subject('Laravel Email Test 1');
});
}
You can pass array of addresses to your ->to:
Mail::send('mail_template1', array('name' => 'Laravel'), function($message) use ($arr_index)
{
$message->to(array(
'name#email.com',
'anothername#email.com' => 'name'
))
->from('different_email#gmail.com', 'Another name')
->subject('Laravel Email Test 1');
});

Laravel SMTP Email

Start working with Laravel 4.2 I tried to send email using Gmail STMP server. Below is my app/config/mail.php.
return array(
'driver' => 'smtp',
'host' => 'smtp.gmail.com',
'port' => 465,
'from' => array('address' => 'sample_address#gmail.com', 'name' => 'Sample'),
'encryption' => 'tls',
'username' => 'sample_address#gmail.com',
'password' => 'sample password',
'sendmail' => '/usr/sbin/sendmail -bs',
'pretend' => false,
);
Below is my php code.
<!-- app/views/emails/welcome.php -->
Mail::send('emails.welcome', 'Laravel Admin', function($msg) {
$msg->from('sample_address#gmail.com', 'Laravel Admin');
$msg->to('sample_receiver#gmail.com');
});
But it does not work. I have already configured my XAMPP php.ini on my MAC OSX. It only works when sending a normal PHP mail, not SMTP. The error message that I've got from Laravel on the view page is 'Error in exception handler'. I would like to see more error information but I don't know how to get more info. What is wrong with my code? What else do I need to do or configure? Thank you!
you can put your email and name in Input
Input::merge(array('email'=>'sample_receiver#gmail.com','name'=>'sample_name'));
Mail::send('emails.welcome', 'Laravel Admin', function($msg) {
$msg->from('sample_address#gmail.com', 'Laravel Admin');
$msg->to(Input::get('email'), Input::get('name'))->subject('You have');
});
also change 'encryption'
return array(
'driver' => 'smtp',
'host' => 'smtp.gmail.com',
'port' => 465,
'from' => array('address' => 'sample_address#gmail.com', 'name' => 'Sample'),
'encryption' => 'ssl',
'username' => 'sample_address#gmail.com',
'password' => 'sample password',
'sendmail' => '/usr/sbin/sendmail -bs',
'pretend' => false,
);
You should change 'encryption' for ssl and in your gmail must be enabled IMAP access in config
When you are having problems with sending mails via gmail, try this. It worked for me.
Login with your gmail account and then go to:
https://accounts.google.com/b/0/DisplayUnlockCaptcha
and click continue. Then, you have few minutes to send your mail with your code.
After this, Google will allow sign in to that account from the new source.

Categories