我正在尝试使用PHP通过Gmail API发送电子邮件。文档说我需要创建一个MIME消息,将整个事物编码为base64url字符串,然后将此字符串设置为Google_Service_Gmail_Message
的'raw'属性。
PHP创建此MIME邮件的最佳方法是什么?我也在使用Laravel 5.1,以防有人知道使用Laravel进行操作的便捷方法。以下是我所指的Gmail API文档:
答案 0 :(得分:2)
我认为我使用Laravel附带的底层Swift Mailer进行了这项工作:
$message = \Swift_Message::newInstance();
$message->setTo(['test@example.com'=>'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 :(得分:2)
我使用 imap_mail_compose
让它工作.. string imap_mail_compose ( array $envelope , array $body )
example-1 @:http://php.net/manual/en/function.imap-mail-compose.php
$envelope["from"]= "foo@gmail.com";
$envelope["to"] = "foo3@example.com";
$envelope["cc"] = "bar@example.com";
$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);
答案 2 :(得分:1)
我知道这是一个很老的帖子,但是我发现它在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(['example@example.com' => 'Joe Bloggs'])
->setTo(['other@example.com' => '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
希望它有所帮助!
答案 3 :(得分:1)
Swiftmailer可以为您完成所有繁重的工作,包括Base64解析。
$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);
答案 4 :(得分:0)
我从字符串创建mime消息,如下所示。如果你愿意,可以自己动手。
我使用base64_encode
对主题和邮件进行编码,并将=?utf-8?B?
放在起点上,?=
放在每个结尾处。
$mimeMessage = "MIME-Version: 1.0\r\n";
$mimeMessage = "From: Fullname <from@from.com>\r\n";
$mimeMessage .= "To: Fullname <to@t.com>\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);
答案 5 :(得分: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", "heik@dom-ain.com","ho@gomail.coc");
// get mime
// $m->getMime()
// Show mime in browser
echo nl2br(htmlentities($m->getMime()));