【问题标题】:How do I send email using gmail api php如何使用 gmail api php 发送电子邮件
【发布时间】:2014-08-27 17:20:47
【问题描述】:

如何使用 gmail api php 发送电子邮件

$msg['raw'] = "This is sample test message";            
$msg['To'] = "test.api@gmail.com";
$msg['subject'] = "Sample test subject";

目前我正在使用 $message = $service->users_messages->send('me', $msg);

在哪里区分,主题?

【问题讨论】:

    标签: gmail-api


    【解决方案1】:

    根据http://www.pipiscrew.com/2015/04/php-send-mail-through-gmail-api/,答案如下:

    $user = 'me';
    $strSubject = 'Test mail using GMail API' . date('M d, Y h:i:s A');
    $strRawMessage = "From: myAddress<myemail@gmail.com>\r\n";
    $strRawMessage .= "To: toAddress <recptAddress@gmail.com>\r\n";
    $strRawMessage .= 'Subject: =?utf-8?B?' . base64_encode($strSubject) . "?=\r\n";
    $strRawMessage .= "MIME-Version: 1.0\r\n";
    $strRawMessage .= "Content-Type: text/html; charset=utf-8\r\n";
    $strRawMessage .= 'Content-Transfer-Encoding: quoted-printable' . "\r\n\r\n";
    $strRawMessage .= "this <b>is a test message!\r\n";
    // The message needs to be encoded in Base64URL
    $mime = rtrim(strtr(base64_encode($strRawMessage), '+/', '-_'), '=');
    $msg = new Google_Service_Gmail_Message();
    $msg->setRaw($mime);
    //The special value **me** can be used to indicate the authenticated user.
    $service->users_messages->send("me", $msg);
    

    【讨论】:

    • 使用上述技术,您每天可以发送多少封电子邮件,请指导
    【解决方案2】:

    Swiftmailer 拥有构建 base64 消息的所有工具。

    这是发送电子邮件的完整示例:

    // Visit https://developers.google.com/gmail/api/quickstart/php
    // for an example of how to build the getClient() function.
    $client = getClient();
    
    $service = new \Google_Service_Gmail($client);
    $mailer = $service->users_messages;
    
    $message = (new \Swift_Message('Here is my subject'))
        ->setFrom('myemailaddress@myserver.com')
        ->setTo(['receiver@someserver.com' => 'Test Name'])
        ->setContentType('text/html')
        ->setCharset('utf-8')
        ->setBody('<h4>Here is my body</h4>');
    
    $msg_base64 = (new \Swift_Mime_ContentEncoder_Base64ContentEncoder())
        ->encodeString($message->toString());
    
    $message = new \Google_Service_Gmail_Message();
    $message->setRaw($msg_base64);
    $message = $mailer->send('me', $message);
    print_r($message);
    

    2021 年 11 月修改

    Swiftmailer 已经到了生命的尽头。 See swiftmailer's author blog post.

    从现在开始,您应该使用Symfony Mailer 组件。

    【讨论】:

      【解决方案3】:

      我使用的是相同的代码,它确实可以正常工作,只是它总是发送纯文本电子邮件。

      指定Content-Type: text/html; 还不够吗?

      【讨论】:

        猜你喜欢
        • 2014-10-30
        • 2017-05-25
        • 2019-04-17
        • 2018-08-20
        • 2018-01-26
        • 2018-02-23
        • 2021-01-24
        • 2021-09-11
        • 2014-09-16
        相关资源
        最近更新 更多