使用gmail-api和google-api-php-client发送电子邮件 [英] send email using gmail-api and google-api-php-client

查看:175
本文介绍了使用gmail-api和google-api-php-client发送电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 https://github.com/google/google-api-php -client ,我想使用用户的授权gmail帐户发送测试电子邮件.

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:

$msg = new Google_Service_Gmail_Message();  
$msg->setRaw('gp1');  
$service->users_messages->send('me', $msg);  

这导致电子邮件被退回,因为我不知道如何设置原始邮件.我在经过身份验证的用户的收件箱中看到弹跳.我想学习如何设置电子邮件的收件人",抄送",密件抄送",主题"和正文"的值.我相信我也需要对该原始数据进行64位编码.我可能想在电子邮件正文中使用一些html.

This results in a bounce email because I have no clue how to set the raw message. I see the bounce in the inbox of my authenticated user. I want to learn how to set values for 'To', 'Cc', 'Bcc', 'Subject', and 'Body' of the email. I believe I will need to do a 64 encoding on that raw data as well. And I might want to use some html in the body of my email.

请帮助提供一个使用gmail-api和google-api-php-client发送电子邮件的有效示例.

Please help to provide a working example of sending an email using the gmail-api and the google-api-php-client.

这是收件箱中退回的电子邮件:

Here is the bounced email in the inbox:

Bounce -nobody@gmail.com- 12:58 PM(7分钟前)
对我
发生错误.您的消息未发送.

Bounce -nobody@gmail.com- 12:58 PM (7 minutes ago)
to me
An error occurred. Your message was not sent.

,日期:2014年7月24日,星期四10:58:30 -0700邮件ID:CABbXiyXhRBzzuaY82i9iODEiwxEJWO1 = jCcDM_TH-

‚ Date: Thu, 24 Jul 2014 10:58:30 -0700 Message-Id: CABbXiyXhRBzzuaY82i9iODEiwxEJWO1=jCcDM_TH-

推荐答案

我问了

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);

这篇关于使用gmail-api和google-api-php-client发送电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆