send email using gmail-api and google-api-php-client

后端 未结 5 1370
感动是毒
感动是毒 2021-01-16 03:24

I am using https://github.com/google/google-api-php-client and I want to send a test email with a user\'s authorized gmail account.

This is what I have so far:

5条回答
  •  情话喂你
    2021-01-16 04:07

    I asked a more specific question which has led me to an answer. I am now using PHPMailer to build the message. I then extract the raw message from the PHPMailer object. Example:

    require_once 'class.phpmailer.php';
    $mail = new PHPMailer();
    $mail->CharSet = "UTF-8";
    $subject = "my subject";
    $msg = "hey there!";
    $from = "myemail@gmail.com";
    $fname = "my name";
    $mail->From = $from;
    $mail->FromName = $fname;
    $mail->AddAddress("tosomeone@somedomain.com");
    $mail->AddReplyTo($from,$fname);
    $mail->Subject = $subject;
    $mail->Body    = $msg;
    $mail->preSend();
    $mime = $mail->getSentMIMEMessage();
    $m = new Google_Service_Gmail_Message();
    $data = base64_encode($mime);
    $data = str_replace(array('+','/','='),array('-','_',''),$data); // url safe
    $m->setRaw($data);
    $service->users_messages->send('me', $m);
    

提交回复
热议问题