2015-12-07 38 views
0

我试图通过使用PHP的Gmail API发送电子邮件。该文档说我需要创建一个MIME消息,将整个事件编码为base64url字符串,然后将此字符串设置为Google_Service_Gmail_Message的'raw'属性。PHP为Gmail API创建MIME邮件

PHP创建此MIME邮件的最佳方式是什么?我还使用Laravel 5.1以防万一有人知道使用Laravel的方便方法。这里我指的是对Gmail的API文档:

https://developers.google.com/gmail/api/guides/sending

回答

1

我觉得我得到这个工作使用随Laravel底层斯威夫特梅勒:

$message = \Swift_Message::newInstance(); 
$message->setTo(['[email protected]'=>'Test Name']); 
$message->setBody('Here is my body'); 
$message->setSubject('Here is my subject'); 
echo $message->toString(); 

此输出:

Message-ID: 
Date: Mon, 07 Dec 2015 02:38:30 +0000 
Subject: Here is my subject 
From: 
To: Test Name 
MIME-Version: 1.0 
Content-Type: text/plain; charset=utf-8 
Content-Transfer-Encoding: quoted-printable 

Here is my body 
1

我得到它的工作..,使用imap_mail_compose

string imap_mail_compose (array $envelope , array $body) 

例如-1 @:http://php.net/manual/en/function.imap-mail-compose.php

$envelope["from"]= "[email protected]"; 
$envelope["to"] = "[email protected]"; 
$envelope["cc"] = "[email protected]"; 
$envelope["subject"] = "Testing.."; 

$part1["type"] = TYPETEXT; 
$part1["subtype"] = "plain"; 
$part1["description"] = "description3"; 
$part1["contents.data"] = "contents.data3\n\n\n\t"; 

$body[1] = $part1; 

$mime = imap_mail_compose ($envelope , $body); 
$mime = rtrim(strtr(base64_encode($mime), '+/', '-_'), '='); 
$message = new Google_Service_Gmail_Message(); 
$message->setRaw($mime); 
$service->users_messages->send($userId, $message); 
+0

确保你的base64urlencode不仅仅是base64encode,否则你会遇到错误。 – flyingL123

0

我创建的字符串MIME消息你可以看到下面。如果你愿意,可以自己设定功能。

我编码的主题和消息与base64_encode并把=?utf-8?B?到起点和?=到每一个的结尾。

$mimeMessage = "MIME-Version: 1.0\r\n"; 
$mimeMessage = "From: Fullname <[email protected]>\r\n"; 
$mimeMessage .= "To: Fullname <[email protected]>\r\n"; 
$mimeMessage .= "Subject: =?utf-8?B?".base64_encode('Sample Subject Which Contains Non-Latin Characters')."?=\r\n"; 
$mimeMessage .= "Date: ".date(DATE_RFC2822)."\r\n"; 
$mimeMessage .= "Content-Type: multipart/alternative; boundary=test\r\n\r\n"; 
$mimeMessage .= "--test\r\n"; 
$mimeMessage .= "Content-Type: text/plain; charset=UTF-8\r\n"; 
$mimeMessage .= "Content-Transfer-Encoding: base64\r\n\r\n"; 
$mimeMessage .= base64_encode('Sample email message which contains non-latin chcracters')."\r\n"; 

使用换行符时要小心。一些线路需要\r\n\r\n

然后,编码整个$mimeMessage作为Gmail的API请求:

$mimeMessageEncoded = base64url_encode($mimeMessage); 
0

您可以尝试PhpMimeClient,并创建附件和内嵌图片的MIME消息: https://github.com/breakermind/PhpMimeParser/blob/master/PhpMimeClient_class.php

简单的例子:

$m = new PhpMimeClient(); 

// Add files inline 
$m->addFile('photo.jpg',"zenek123"); 

// Add file 
$m->addFile('sun.png'); 

// create mime 
$m->createMime("Witaj księżniczko Alabambo",'<h1>Witaj księżniczko Alabambo <img src="cid:zenek123"> </h1>',"Wesołych świąt życzę!","Heniek Wielki", "[email protected]","[email protected]"); 

// get mime 
// $m->getMime() 

// Show mime in browser 
echo nl2br(htmlentities($m->getMime())); 
0

我知道这是一个相当古老的帖子,但我在Google上发现它,并且我刚刚使用Laravel 5.5遇到了同样的问题,这是我做到这一点的方式 - 以防万一它有助于:)

下面的函数将使用刀片模板返回一个HTML字符串,该模板允许您将它传递到Google API。

/** 
* Create a Message from an email formatted string. 
* 
* @return Google_Service_Gmail_Message Message containing email. 
*/ 
function createMessage() 
{ 
    $message = new \Google_Service_Gmail_Message(); 

    // Initialise the Swift Message instance. 
    $swift = (new Swift_Message()) 
     ->setSubject('Test Email') 
     ->setFrom(['[email protected]' => 'Joe Bloggs']) 
     ->setTo(['[email protected]' => 'An Other']) 
     ->setBody(view('mail.invoice'), 'text/html') 

     // Optionally add any attachments 
     ->attach(Swift_Attachment::fromPath('my-document.pdf')); 

    $message->setRaw(strtr(base64_encode($swift->toString()), array('+' => '-', '/' => '_'))); 

    return $message; 
} 

如果您想了解更多有关使用SwiftMailer的更多信息,你可以前往该Symfony的网站了解更多信息 - 这是我用这个页面。 https://swiftmailer.symfony.com/docs/messages.html

希望它有帮助!