Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel not sending from address

Tags:

php

email

laravel

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('[email protected]')->from($from, $name)->subject('Feed Back'); 
        //[email protected] 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
[email protected]
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' => '[email protected]',
    '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.

like image 962
Advaith Avatar asked Oct 11 '16 06:10

Advaith


1 Answers

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#

like image 187
shock_gone_wild Avatar answered Sep 29 '22 05:09

shock_gone_wild