17
votes

Is it possible to send mail in core php via gmail smtp without using any external class?

5
The better question is why would you want to? :)MetalFrog
You 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
Both comments are incorrect. I agree that it'd be ideal to just say mail(), instead of including a 600 line library.gavanon
@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
@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

5 Answers

5
votes

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:

Send email using the GMail SMTP server from a PHP page

24
votes

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
[email protected]
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('[email protected]', 'Subject', 'Message', 'From: Your name <[email protected]>');

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.

4
votes

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

0
votes

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 = [email protected]
password = myemailpassword
sendmail_from = [email protected]

Alternatively you can do:

<?php
ini_set( 'smtp_port', 465 );
//etc
-2
votes

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