Using curl to send mail from gmail - php

Is it possible to login to my gmail account and send mail using curl in php? I don't want to use pop3 or any other function.
$msg = "something";
$email = "sa#sas.com";
$pass= "something"
function send($msg, $email, $pass)
{
//something
}
I have never used curl, to be honest. Have just heard of it.

It is theoretically possible, but extremely complex and subject to breaking whenever Google decide to change their HTML interface. Not a good idea.
Your best option is to use Google Mails's SMTP servers. Mailer packages like SwiftMailer make this easy to set up.
Here is an example for how to connect to GMail with SMTP.

You may do it with libcurl without many troubles. Here is an example http://curl.haxx.se/libcurl/c/smtp-tls.html of how to send email using TLS (all you need to send via gmail) in C. I believe libcurl PHP port provides the same ability.

I believe it is possible but the codebase would be huge and the benefits very small compared with using imap or pop3 or other services.
Why do you want to do this?
p.s. gmail has a limit on the numbers of email you cand send per hour, as far as i know. So, if you're trying to use gmail to send mass emails i would advise against it.

I should use SMTP for it, there are some great mailer classes for SMTP (E.g. Zend_Mail). Zend components can be use stand alone and are very great.

Libcurl is designed to support smtp email since version 7.20 (April 2010). It also support several authentication methods (SSL, TLS, CRAM-MD5)

Related

How to send SMTP emails with PHP without using a 3rd party class

I've been using PHPMailer to send SMTP emails, and also have heard some recommend various other 3rd party classes.
I've always been a surprised and a little disappointed that this functionality is not native in PHP.
Do I still need to use a 3rd party class to do so? If not, how? If so, any idea why it isn't native?
PHP does have a native function mail() to send emails, but it is not very efficient.
From the official PHP documentation -
It is worth noting that the mail() function is not suitable for larger
volumes of email in a loop. This function opens and closes an SMTP
socket for each email, which is not very efficient.
Official PHP docs suggest using PEAR::Mail library for efficient email support. I would rather use PHPmailer or Swift mailer(http://swiftmailer.org/) though as they both have excellent support and also well documented.
If you are using windows the php mail() function will not work unless you have an smtp server installed on your PC. Windows passes the email to the email server which then sends the email if it has access through your firewall. Also note that mail() as documented is not the correct way to send an email. In order to send an email the email message must be composed in a manner resembling the RFC documentation.

google apps is setup on my server, how do i get php mail() to use it?

I've got google apps setup on my server with all the correct MX records etc, now i want to start testing it.
I want to use it for all automated
emails (registration, lost password
etc).
I'm testing on a localhost.
You'll probably want to use PEAR Mail, and set it up using the SMTP settings provided to you by Google. I don't believe the built-in mail() command supports TLS, which is required for sending using Google.
Ok, the solution i've found is to use the PHPMailer class found here:
http://phpmailer.worxware.com/

Access gmail or yahoo mail using script

I was wondering how one would go about writing a php script to access mail from yahoo or gmail ?
The "simplest" way would be to use IMAP to access the mails (i.e. don't try any kind of HTML scraping !). I have no idea for Yahoo, but I know you can enable IMAP in gmail.
There are several libraries in PHP that allow one to use the IMAP protocol to access a mailbox ; for instance :
There are functions provided in PHP (You might have to install the extension, though) : IMAP, POP3 and NNTP
Or you could use something like Zend_Mail.
You could use the Zend Framework Zend_Mail functionality, this would allow you to connect to GMail/Yahoo/etc with POP3 or IMAP.
One more to add to Pascal's suggestions: libgmailer

is there something wrong with using php's native mail function?

i tried googling but sadly i get only documentations (or am i using bad keywords)
anyway
i can see that alot of programmers (even those im working with right now) does not seem to approve to using the php native mail function and resorts to using some other framework like sendmail? swift mailer etc...
i'd like to know why? are there really disadvantages to using the native mail function?
if so how does the mailing frameworkds solve that or how are they better??
There's nothing wrong with it for sending simple plain text emails.
However, once you get into multipart mime emails (say, you want an HTML version or to add an attachment) then you have to build the email yourself, and it can be quite tricky to get all the headers and encoding correct. In this case you're better off using a library.
The PHP manual for function mail mentions that there are some restrictions with the mail function and one of these are that the function opens and closes an SMTP socket for each email. The mail function works good when you just want to send a mail or two.
As far as I'm concerned, all of these problems pale in comparison to the major security problem:
Mail header injection: ( http://en.wikipedia.org/wiki/E-mail_injection , and php specific info: http://www.damonkohler.com/2008/12/email-injection.html )
Whereby a spammer bot spiders your site and, finding a vulnerability in your script that is easy to still have when using the very insecure mail() function, IS ABLE TO SEND EMAIL FROM YOUR SERVER TO AN ARBITRARY LIST OF CONTACTS, essentially turning your script & server into a cog in their spam email machine.
I recommend never using mail() with user input, and in general, just making use of PEAR::mail instead. http://pear.php.net/package/Mail/
Using PHP's mail() function requires a properly configured sendmail or equivalent on the host the program is running. However, the Windows implementation is a bit different. If you don't have your MTA configured properly, you won't be able to successfully send emails from your PHP scripts. Like another commenter said on this thread, PHP manual explicitly states that each call to the mail() function opens and closes a socket. This can cause unnecessary delay in script execution.
Additionally, your development and testing environment may not have a public static IP address. Your IP address might be blacklisted by DNSBL, Gmail, Yahoo! and other popular email service providers.
Your best bet in this situation is to use a properly configured external SMTP server. Chances are your employer has already provided an email account with SMTP access. If you don't have one you can use a Gmail account. Gmail provides SMTP access to all email accounts.
You can write scripts to open a socket connection to the external SMTP server. When there are tried and tested open source libraries for this purpose, why write your own?
Incidentally, I wrote a blog post on the very same subject yesterday: Using SMTP With Zend Framework - Solve Email Delivery Problem
Best regards,

Specify smtp server for php mail() in freebsd?

I have two dedicated servers, one of which is configured for sending email out (SPF, DKIM, other domain whitelisting methods, etc). I need to send email from both servers, but I want to send mail from both servers through the server that's been set up for it.
It doesn't look like I can explicitly set an SMTP server directly in the mail function. Is there a way I can override the value set in php.ini, through .htaccess or something?
I would recommend not using the mail command and using a pre-built PHP mailing solution. There are 2 great recommendations at the following: Is this the correct way to send email with PHP?
In using a pre-built solution, you can have all of your mail go to the same server if you choose.
I would recommend using ezcMail for sending emails. It has a clean object oriented prebuilt mailing package which is highly configurable.

Categories