Search code examples
phpgmailphpmailer

Send mail using Gmail from php in localhost


I'm already know how to use phpmailer but I can't send mail which are using Gmail (I have in phpmailer all the configuration done like port, server name, etc) because it are using OAuth 2.0 and I already get my client id an all that but I'm getting this error:

 400. That’s an error.

Error: invalid_scope

Some requested scopes were invalid. {invalid=[[email protected]]}

Learn more

Request Details

when I try to connect to the server from the URL so what I must to do about that?

this is the file which I have the codes, I'm going to change some character to keep this private, I hope that you sand me with this some example how to use it.

copy and paste in a noteblock to have this more clear.

{
  "private_key_id": "c6ffd46f1@@@@@@@@@@@50@@@@@@62b620526a",
  "private_key": "-----BEGIN PRIVATE KEY-----\nMIICdwIBADA@@@@@@@@@@@@@@@@@@wggJdAgEAAoGBAJYjqzxsw9z9kdp5\ngjuqZz0vVuad574M6fTlFcLp3HdPg8y2hxl2hzW+lZPCWFDUgRgmJEWAR1Qf40ZB\nTm4FwD4mN7/YmExzjVTJ9sPHkf@@@@@@@@o7VincKg5RddvYML95Vzzz0\n1ft@@@@@@@@@@@e0CKuWeIXDKbJVAgMBAAECgYBBc4dArkGsYzZkQbhOfnjHKY3+\ntzPB@@@@@@@@@@@IDcztkeyti2M3EAu8MTXf1N9yAhQk134RxpxkQJqPbk\na5lxv+euyq@@@@@@@@@@iTyKglke8AgR3tXFWcFT1u0p6VeC3uQOskFEO4jNS\nNBbuOcVn/LbyNoB4+QJB@@@@@@@@@@@wU81o36dLz/Ad7hl4yqdWcmotEe38j1\nWa/mBBHGMm+XJFLnfnde/KNeY8a@@@@@@@@@@@@@@@@QQDA8Ej7Ho6whqAiP3d5\nO4@@@@@@@@@@@@3VCzPipDjYzZjvojqz3WRW1pTvAvRcmWWCSJSUOV2FI\npENvAk@@@@@@@@@@@@@jojJJ59QokIYFDlALIP4FiDUVnTKrbZ1JdsqXcRmm\nTzJuVi@@@@@@@@@@@@@@@@@@@@@@@Ihh91BmYFwTBfvsbhUJ8At2IqkpP\nemKr2hbUMs4yTd1IT@@@@@@@J+@@@@@@@@P8CQHVWlZPm\nm7oV9484A8NHT@@@@@@@@@@@@MpUlAbk37C45gU4xFhp1MSvNsNiO\nquDr@@@@@kitgIc\u003d\n-----END PRIVATE KEY-----\n",
  "client_email": "56@@@@@@@@5@@63-3tummk4ie@@@@@@@@@@@@[email protected]",
  "client_id": "56@@@@@@5@@63-3tu@@@@@@@@nlejapn5fajni0sun19n.apps.googleusercontent.com",
  "type": "service_account"
}

Solution

  • Using the pear mail library

    // Pear Mail Library
    require_once "Mail.php";
    
    $from = '<from.gmail.com>';
    $to = '<to.yahoo.com>';
    $subject = 'Hi!';
    $body = "Hi,\n\nHow are you?";
    
    $headers = array(
        'From' => $from,
        'To' => $to,
        'Subject' => $subject
    );
    
    $smtp = Mail::factory('smtp', array(
            'host' => 'ssl://smtp.gmail.com',
            'port' => '465',
            'auth' => true,
            'username' => '[email protected]',
            'password' => 'passwordxxx'
        ));
    
    $mail = $smtp->send($to, $headers, $body);
    
    if (PEAR::isError($mail)) {
        echo('<p>' . $mail->getMessage() . '</p>');
    } else {
        echo('<p>Message successfully sent!</p>');
    }
    

    I got it from here.