5

I want to send Email to my Email when a user views a page.
I done it successfully.

My code looks like this:

Route::any('contact_us/send', function() {
    return Mail::send('contactus.send', ['name' => 'Advaith'], function($message) {
        $name = $_POST['name']; 
        $from = $_POST['email'];
        $message->to('itsme@gmail.com')->from($from, $name)->subject('Feed Back'); 
        //itsme@gmail.com is used in this question only
    });
});

This code send Email to my account.

But it is not sending the senders name and email ($name and $from)
I Even tried to change the variables and give a example email to it.

My .env file looks like this:

MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME=itsme@gmail.com
MAIL_PASSWORD=**********
MAIL_ENCRYPTION=tls

My config/mail.php file looks like this:

'driver' => env('MAIL_DRIVER', 'smtp'),
'host' => env('MAIL_HOST', 'smtp.gmail.com'),
'port' => env('MAIL_PORT', 587),
'from' => [
    'address' => 'hello@example.com',
    'name' => 'Example',
],
'encryption' => env('MAIL_ENCRYPTION', 'tls'),
'username' => env('MAIL_USERNAME'),
'password' => env('MAIL_PASSWORD'),
'sendmail' => '/usr/sbin/sendmail -bs',
'pretend' => false,

My form which sends the data is like this:

<form class="ad-form col-md-12" method="POST" action="/contact_us/send">
    {{ csrf_field() }}
    <input placeholder="Name...*" type="text" name="name" required="required">
    <br>
    <input placeholder="Email...*" type="email" name="email" required="required">
    <br>
    <textarea required="required" placeholder="Your valuable feedback...*" name="comment" rows="5" cols="40"></textarea>
    <br>
    <input type="submit" name="submit" value="Send">
</form>

And my contactus/send.blade.php file looks like this:

<div style="border: 2px solid orange; padding: 30px;">
    <div>From: <b>{{ $_POST['name'] }}</b></div>
    <div>From-Email: <b>{{ $_POST['email'] }}</b><hr></div><br>
    <div style="text-align: justify;"><strong>{{ $_POST['comment'] }}</strong></div>
</div>

Please tell me why it is not sending the from address.

And please also tell me how to go to another page after the email is send.

Saurabh Srivastava
  • 1,093
  • 14
  • 27
Advaith
  • 2,490
  • 3
  • 21
  • 36

1 Answers1

5

This problem is not a Laravel issue. You said in a comment, that it is written that you sent the email to yourself. This is due to the fact that gmail does not allow to use random from addresses, to prevent spamming.

You have to register alternate addresses before you can use them as your "from address" , or gmail will change it back to default.

For references see here or here

There are also some SO ressources that deal with the problem:

How to change from-address when using gmail smtp server,

When using Gmail for SMTP, can you set a different “from” address?

change sender address when sending mail through gmail in c#

Community
  • 1
  • 1
shock_gone_wild
  • 6,700
  • 4
  • 28
  • 52