johnef_sh's avatar

can't send mail from my web application, and no errors

I am trying to send a contact us mail with attachment, when I submit the form, it just comeback with no errors but mail never sent. see my code

I setup my config/mail like this

'driver' => 'mail',
'host' => 'smtp.hotmail.com',
'port' => 587,
'from' => array('[email protected]' => null, 'name' => null),
'encryption' => 'tls',
'username' => null,
'password' => null,
'sendmail' => '/usr/sbin/sendmail -bs',
'pretend' => false,

Controller

public function getJoinRequest()
    {
        $data = Input::all();
        $rules = array(
            'name' => 'required|regex:/^[\pL\s\-]+$/u',
            'email' => 'required|email',
            'job' => 'required|alpha',
            'comment' => 'required|min:25'
        );

        $validator = Validator::make($data, $rules);
        if ($validator->passes()) {
            Mail::send('emails.joinRetunMessage.hello', $data, function ($message) use ($data) {
                $message->from($data['email'], $data['name']);
                $message->to('[email protected]', 'El Arabia')->subject('Join us request');
                $message->attach($data['resume']->getRealPath(), array(
                        'as' => 'resume.' . $data['resume']->getClientOriginalExtension(),
                        'mime' => $data['resume']->getMimeType())
                );
            });
            return Redirect::to('/join-us')
                ->with('message', 'Your message has been sent. Thank You!');
        } else {
            return Redirect::to('/join-us')->withErrors($validator);
        }
    }

here is how I got it to work with my hotmail

'driver' => 'mail', 
'host' => 'hotmail.com',
'port' => 587,
'from' => array('address' => '[email protected]', 'name' => 'ElArabia'),
'encryption' => 'tls',
'username' => "[email protected]",
'password' => "XXXX",
'sendmail' => '/usr/sbin/sendmail -bs',
'pretend' => false,

and same with my domain information. but the the thing I have to say about this is you can write your 'username' => "[email protected]", and then say send it to the same email this will never work.

0 likes
23 replies
johnef_sh's avatar

@tomi greeting, What if I need to use something like the native PHP emails? Laravel don't support that!!

amitshrestha221's avatar
Level 3

in config/mail.php replace the code with this.. i hope it helps..


<?php
return array(
    'driver' => 'smtp',
    'host' => 'smtp.gmail.com',
    'port' => 587,
   'from' => array('address' => 'your_email', 'name' => 'amit'),
    'encryption' => 'tls',
    'username' => 'your_email',
    'password' => 'your_password',
    'sendmail' => '/usr/sbin/sendmail -bs',
    'pretend' => false,

);

1 like
tomopongrac's avatar

Laravel support nativa php mail but i think that than you need to write this in your config

'host' => 'smtp.your-domain',

johnef_sh's avatar

@tomi thanks, now about the code I wrote above will do, or should I change it. thanks again.

johnef_sh's avatar

Same thing still the page coming back without any errors but email not sent!! any idea please

leolam2005's avatar

@johnef_sh

Ran into the same situation few days ago,

solution:

MAIL_DRIVER=mail
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME=XXXXXX
MAIL_PASSWORD=XXXXXX
MAIL_ENCRYPTION=tls

You need to change MAIL_ENCRYPTION from null to tls

it fixes, but don't know why

If you use mail driver,

You will also need the mail server properly set up.

johnef_sh's avatar

@leolam2005 are you using gmail to send this emails?

If you use mail driver,
You will also need the mail server properly set up.

can you please clear this out

Thanks

leolam2005's avatar

@johnef_sh

if you intend to send email with Mail Driver,

you need to set the DNS record correct on production server

example:

    mail.mysite.com.    A   111.xx.xxx.xx
    mysite.com. MX (10) mail.mysite.com.
    mysite.com. TXT v=spf1 +a +mx -all +a:vps-xxxxxxxxx.c
p.readyspace.com.hk

You can use other smtp driver like mailgun, but you also need to add mailgun DNS record!

For testing only, you can use their sandbox api

leolam2005's avatar

send with mail server

MAIL_DRIVER=mail
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME=XXXXXX
MAIL_PASSWORD=XXXXXX
MAIL_ENCRYPTION=tls

send with gmail account

MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME=XXXXXX
MAIL_PASSWORD=XXXXXX
MAIL_ENCRYPTION=tls
1 like
johnef_sh's avatar

@leolam2005 thanks, maybe this the lost part, but please just one second You said that if I intend to send email with Mail Driver, I need to set the DNS record correct on production server.

You mean like using Simple Zone Editor? and then? I saw this DNS, CNAME, MX but on what base should I edit them? I thought that this settings is only should be done for mailgun! can you please clear this more

thanks

leolam2005's avatar

normally you don't need to edit the DNS setting for Mail driver to work on production. Most hosting did add the record for you

But are you sure

  1. you are using Mail Driver on production server to send Email without effect?

  2. in .env MAIL_ENCRYPTION=tls , not MAIL_ENCRYPTION=null ?

A few days ago I my server suddenly cannot send mail yet doesnot throw errors just because of the MAIL_ENCRYPTION. It needs to set to tls

1 like
johnef_sh's avatar

@leolam2005 yes I use to use mail driver on the same domain but native PHP and it works will sending emails no problems at all and in my .env MAIL_ENCRYPTION is tls and not NULL I will update my question now please review I will put all .env/mail

thanks

johnef_sh's avatar

thanks for all of y'all @leolam2005 @tomi @amitshrestha221 I got it to work but with hotmail account and then got it to work via my domain too.

here is how I got it to work with my hotmail

'driver' => 'mail', 
'host' => 'hotmail.com',
'port' => 587,
'from' => array('address' => '[email protected]', 'name' => 'ElArabia'),
'encryption' => 'tls',
'username' => "[email protected]",
'password' => "XXXX",
'sendmail' => '/usr/sbin/sendmail -bs',
'pretend' => false,

and same with my domain information. but the the thing I have to say about this is you can write your 'username' => "[email protected]", and then say send it to the same email this will never work.

thank you all for your help.

eduardocruz's avatar

I just had a similar issue. After spending many hours looking into that, I realized the problem is that my server on Digital Ocean had the default SMTYP ports blocked by default. My final tests, /i tried using first LOG instead of SMTP just to make sure that an email was being logged locally. Then I tried using SMTP with Mailtrap, which uses port 2525 locally and on my server. But it didn't work when I tried with Mailgun, which was using port 485 or 587 I think. My final test, I tried Mailgun with port 2525 and it worked just fine.

The worst part is that as described, using a blocked port will not raise any exceptions.

Please or to participate in this conversation.