I'm working on some software that will, ideally, send an HTML email on behalf of the user from their personal email address. This wouldn't be a bulk mailing, it would go to one specific email address at a time. The most active users would be doing this a few times per day, with many users not using it on a daily basis at all. This is not an app with wide appeal, so I suspect the user base would be in the low thousands at most. And, lastly, each time the email is sent, it would be as a result of the user pressing a send button. I plan to have an iOS and PHP-based web version of the app.

Most of my users will be sending from one of the major email providers (Gmail, Yahoo, etc.), although a few will probably be using an address from their own domain.

I know that for iOS, there is an API to send a message using the built-in mail program, but this is not ideal for my situation since not everyone uses that program (I use the Gmail app, for example) and since there's also going to be a web app, it would be nice to be able to route everything through an API there so that I'm not duplicating too much code.

I understand that I can use PHP to send an email from any address, but it will likely be flagged as spam and the proper way to do it is with authenticated SMTP.

I also know that, for example, Gmail has an API for programmatically sending emails from an account as long as the account holder authorizes an app to do so.

I'm wondering if there's any library or service that exists that would essentially allow a user to pick from a list of major email providers and authorize my app using their APIs, or else enter in their own custom SMTP information if they're using their own server. I guess I can roll my own if need be, but I'm hoping not to reinvent the wheel. I'm also not sure how I'd be securely storing all of this email authentication information in my app database.

Any advice would be appreciated. Thanks!

  • 1
    are you providing the body of the message, and what address should it go to if the recipient hits reply? You don't need a library, you just need to generate the mail properly to comply with best practices when trying to fight spam
    – ivanivan
    Commented Sep 18, 2017 at 2:31

2 Answers 2


To send email as a user using gmail, hotmail, and most mail servers you would need to authenticate with that mail server as the user. So this means you would need to not only request the user's email address, but also their password.

If that is okay, then it can be done with any PHP mailer that allows you to specify a remote SMTP mail server, but I don't know of any library that would help you with automating looking up the mail servers by the user's email address. It is possible to do this though.

An easier alternative may be to send email from your own domain, and if it is setup correctly (SPF, DKIM, etc) you will get good deliverability (less likely to be treated as spam).

You can then set a reply-to email address of the user's email address and this will not affect deliverability, but if anyone replies to the message it will come to the reply-to address.

If you need to you can show the user's name as the sender name (but not address).

Note that any email sent this way would not obviously not show in the user's sent items mailbox in the gmail, hotmail, etc.


Here's some PHP code that I have been using for years. It probably needs tidying up, but it works. Of course, there are some functions missing, like DebugTrace() and HanldeException(), but you can provide your own ;-)

Be sure to configure your PHP.ini correctly for email sending.

// +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=
function SendEmail($from, $to, $subject, $body, $debug)
        DebugTrace('Send email from ' . $from . ' to ' . $to, 'info', $debug);

        if ($_SERVER["SERVER_NAME"] == 'localhost')
            DebugTrace('Email not sent - developing on localhost', 'info', $debug);
        $headers  = "From: " . $from . "\r\n";  
        $headers .= "Reply-To: " . $from . "\r\n";

        $headers .= "MIME-Version: 1.0" . "\r\n";
        $headers .= "Content-type:text/html;charset=UTF-8\r\n";

        if (mail($to, $subject, $body, $headers) === false)
            DebugTrace('Failed to send email', 'error', $debug); // ToDo: maybe write this to a log file? Send an SMS? Write a file to the server & have a cron check which emails me if it exists
            return;     // only here to allow breapointing
            DebugTrace('Email sent from ' . $from . ' to ' . $to, 'info', $debug);
            return;     // only here to allow breapointing

    catch (Exception $e)


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.