Is it possible to send mail in core php via gmail smtp without using any external class?
-
1The better question is why would you want to? :) – MetalFrog Apr 05 '12 at 12:27
-
2You will need to use an external class *and* you will need the OpenSSL extension. This is because Gmail *require* SSL/TLS and you cannot do this with PHP core. – DaveRandom Apr 05 '12 at 12:28
-
1Both comments are incorrect. I agree that it'd be ideal to just say `mail()`, instead of including a 600 line library. – gavanon Jan 25 '15 at 23:14
-
3@MetalFrog An even better question is why would you want to do **anything in the world** with an attitude like that. I can't tell you *how often* I visit forums googling the answers to obscure questions, and the **Very First** answer is *ALWAYS* someone like you, asking "Why". *Why NOT?* Maybe it's our **job**. Maybe it's something we're doing **for a friend**. Maybe we just want to know **if it's possible**. Why don't you *stop asking why*, and if you *actually know the answer*, then **HELP OUT FOR A CHANGE**. – Ayelis Jul 09 '15 at 20:24
-
@Ayelis As often the case, it's difficult to convey sarcasm through text alone, though I had hoped a smiley face would have sufficed -- which it did for three years. It's a concise way of saying, "there are better options out there, you have a better solution already waiting for you, just dig a little deeper." If you take offense to that, _you're_ not digging far enough. – MetalFrog Jul 10 '15 at 01:38
-
@Ayelis Also, through asking someone why they want to do something, it helps them explain and determine exactly what they're trying to do. Many times, explaining what I'm trying to achieve to another person opens my own quest up to clarity. It helps center what I'm attempting to complete, and refines my task greatly. If you're unwilling to answer such simple questions about what you're doing, your attitude is clearly the one that needs correction. Critique, modification, and more questions is an important feedback loop. Take part instead of getting angry. – MetalFrog Jul 10 '15 at 01:43
-
@MetalFrog Sick and tired of reading "Why" as the first (and often ONLY) response to every damn question on the internet. "Answer simple questions"? I don't need to provide you my papers. I just want an answer. If the answer was actually "It is not possible," then SAY it is not possible. However, if the answer is "Install SSMTP, edit the settings and php.ini, and use this 1-line mail function", then Problem Solved. You don't need to get all up in my business. I'm not giving you login details to my server or confidential information about my job. What actually needs correction is your manners. – Ayelis Jul 10 '15 at 20:33
-
@MetalFrog Merely asking "Why" is open-ended and unhelpful. Instead, if there are such options, ask leading questions. "Are you trying to send core php mail() via GMail without external classes because you are not allowed to save more than a single PHP file to a website? Or because you don't know how to use include files? Or are you trying to exploit a server with PHP injection?" If you want clarification, try to be clear yourself. I swear, if I can educate JUST ONE PERSON who has ever asked "Why?" as the first response to a technical question, it will all be worth it. Let this be that once. – Ayelis Jul 10 '15 at 20:33
-
@Ayelis I had intended to only reply with "Why?" but there is a minimum character restriction. – MetalFrog Jul 14 '15 at 15:38
6 Answers
There's a lot of miscommunication about this. It is 100% possible to send emails using gmail via PHP's simple "mail()" command. And it is 100% easy.
Install SSMTP:
sudo apt-get install ssmtp
Edit its settings file:
sudo nano /etc/ssmtp/ssmtp.conf
Inside, make it similar to this, but with your own credentials:
mailhub=smtp.gmail.com:587
AuthUser=youremail@gmail.com
AuthPass=password
UseSTARTTLS=YES
# You can only do this if you've verified your domain with Gmail.
# If you haven't, delete, or add a # before this
hostname=yourwebsite.com
FromLineOverride=YES
Lastly, open your php.ini, and search for sendmail_path and use this value:
sendmail_path = /usr/sbin/ssmtp -t
That's it! Test it out in your PHP, with the simple 1-line mail function:
mail('to@address.com', 'Subject', 'Message', 'From: Your name <youremail@gmail.com>');
Update on Gmail Security
Gmail now blocks this by default. You can still do this by visiting: http://www.google.com/settings/security/lesssecureapps
Turn this feature ON.

- 1,293
- 14
- 15
-
what does it mean: `You can only do this if you've verified your domain with Gmail.`? Is it related to gmail or we can do something to verify our domain on gmail? – VSB Aug 30 '14 at 12:48
-
1It was so wonderful and straight solution :) I spent long time to overcome this situation and hopefully this method was working completely fine without no problems. – VSB Aug 30 '14 at 12:59
-
2Thanks for your feedback! It's unfortunate the chosen answer is wrong. As for using a verified domain, this only applies if you want the From address to be different than your Gmail address, like if you own a website and want a professional From address. But you can't use any From address like bill@microsoft.com, unless you can prove you have access to this email already. **How to verify this email with Gmail:** Sign into Gmail, and in the Settings > Accounts section, click "Add another email address you own". Enter your email, and Google will send a verification email with a link to confirm – gavanon Aug 30 '14 at 19:55
-
I'm not sure this works anymore. Google blocks my attempts: > Someone just tried to sign in to your Google Account from an app that doesn't meet modern security standards. > We strongly recommend that you use a secure app, like Gmail, to access your account. All apps made by Google meet these security standards. Using a less secure app, on the other hand, could leave your account vulnerable. > Google stopped this sign-in attempt, but you should review your recently used devices – rooter May 23 '16 at 13:39
-
1This is still possible. Gmail recently disabled it by default, and now requires you enable less secure apps. You can do so here: http://www.google.com/settings/security/lesssecureapps – gavanon May 23 '16 at 18:59
-
1can confirm that this is still working. I know this is very old. but this is just amazing :) trying to configure php to do mail has been a nightmare and this has been truly easy/wonderful – Frank H Jun 28 '20 at 01:43
-
I don't think it is possible because you need to perform an authentification. Also, you need to connect via an SSL socket, I don't know if the stock mail()
function support this.
If you are willing to use the Pear Mail package, you might want to take a look at this:

- 1
- 1

- 3,104
- 19
- 37
It is possible, although you have to modify php.ini settings, see the PHP manual. You can modify php.ini settings at runtime with ini_set

- 9,580
- 2
- 34
- 43
If you have access to edit the php.ini
then you can do something like this:
[mail function]
SMTP = ssl://smtp.gmail.com
smtp_port = 465
username = info@Mmydomainname.com
password = myemailpassword
sendmail_from = info@mydomainname.com
Alternatively you can do:
<?php
ini_set( 'smtp_port', 465 );
//etc

- 27
- 2
-
1This won't work. Gmail only accepts connection on port 465, which is SSL. Also, I don't see how you are going to perform the authentification process. – Pierre-Olivier Apr 05 '12 at 12:29
-
Add yes, I just tested my code, you can't authenticate using the native mail() function...you will need to either use a thirdparty options like PEAR or PHPMailer to get this to work...or implement your own using fsockopen (a little teadious though). – Dave Goodchild Apr 05 '12 at 13:00
Not Possible in Core PHP mail() function. You need PHPMailer for this solution.

- 80
- 2
- 12
you can do this by PHPmailer Library it already having gmail.php file .
just open and place your detail in that file , you can also use the similar code in your file .
You must make setting in your gmail account setting to allow smtp mailing

- 94
- 10
-
1The OP wants to simply use the core PHP mail() function and not any external libraries. – FreeKrishna Feb 09 '17 at 05:37