通过 Google SMTP 在 PHP 中发送附件时出现问题
嘿,我无法在我正在使用的服务器上重新编译 PHP,所以我相信处理此功能的最佳选择(我们公司最近转移到 GoogleApps 来处理电子邮件)是通过一些基于套接字的电子邮件。问题似乎是,当我实际发送邮件时,标题和消息显示为内联,而不是作为附件。我认为这可能是一个简单的修复,我只是缺少一些东西
,我下载了一个很棒的 smtp 邮件类,但它没有任何附件功能,所以我必须手动更改该类。该类最初由@author wooptoo http://wooptoo.com 编写。为了不将他们的工作广泛发布在互联网上,我只发布相关的代码部分:
function attach($attachments){
$semi_rand = md5(time());
$this->mimeBoundary = "==Multipart_Boundary_x{$semi_rand}x";
$fileatt = $attachments[0]["file"]; // Path to the file
$fileatt_type = $attachments[0]["content_type"]; // File Type
$fileatt_name = basename($attachments[0]["file"]); // Filename that will be used for the file as the attachment
$file = fopen($fileatt,'rb');
$data = fread($file,filesize($fileatt));
fclose($file);
$data = chunk_split(base64_encode($data));
$email_message = "--{$this->mimeBoundary}\n" .
"Content-Type: {$fileatt_type};\n" .
" name=\"{$fileatt_name}\"\n" .
//"Content-Disposition: attachment;\n" .
//" filename=\"{$fileatt_name}\"\n" .
"Content-Transfer-Encoding: base64\n\n" .
$data . "\n\n" .
"--$this->mimeBoundary\n";
$this->hasAttachment = '1';
$this->attachmentData = $email_message;
}
上面将附件设置为邮件对象,下面的代码将其发送
function send($from, $to, $subject, $message, $headers=null) {
if($this->hasAttachment == '1')
{
$headers .= "\nMIME-Version: 1.0\n" .
"Content-Type: multipart/mixed;\n" .
" boundary=\"{$this->mimeBoundary}\"";
$message .= "This is a multi-part message in MIME format.\n\n" .
"--{$this->mimeBoundary}\n" .
"Content-Type:text/plain; charset=\"iso-8859-1\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" . $message ."\n\n" .
$this->attachmentData;
}
fputs($this->conn, 'MAIL FROM: <'. $from .'>'. $this->nl);
fgets($this->conn);
fputs($this->conn, 'RCPT TO: <'. $to .'>'. $this->nl);
fgets($this->conn);
fputs($this->conn, 'DATA'. $this->nl);
fgets($this->conn);
fputs($this->conn,
'From: '. $from .$this->nl.
'To: '. $to .$this->nl.
'Subject: '. $subject .$this->nl.
$headers .$this->nl.
$this->nl.
$message . $this->nl.
'.' .$this->nl
);
fgets($this->conn);
return;
}
Hey there, I'm unable to recompile PHP on the server I'm working on, so I believe my best option for handling this functionality (our company recently moved to GoogleApps for email) is via some socket based email. The issue seems to be that when I actually send the mail, the headers and message appear inline, and not as an attachment. I think this is probably an easy fix and I'm just missing something
I downloaded a great smtp mail class, but it didn't have any functionality for attachments so I had to manually alter the class. This class was originally written by @author wooptoo, http://wooptoo.com. In the interest of not putting their work widly out on the internet I'm only going to post the relevant code portions:
function attach($attachments){
$semi_rand = md5(time());
$this->mimeBoundary = "==Multipart_Boundary_x{$semi_rand}x";
$fileatt = $attachments[0]["file"]; // Path to the file
$fileatt_type = $attachments[0]["content_type"]; // File Type
$fileatt_name = basename($attachments[0]["file"]); // Filename that will be used for the file as the attachment
$file = fopen($fileatt,'rb');
$data = fread($file,filesize($fileatt));
fclose($file);
$data = chunk_split(base64_encode($data));
$email_message = "--{$this->mimeBoundary}\n" .
"Content-Type: {$fileatt_type};\n" .
" name=\"{$fileatt_name}\"\n" .
//"Content-Disposition: attachment;\n" .
//" filename=\"{$fileatt_name}\"\n" .
"Content-Transfer-Encoding: base64\n\n" .
$data . "\n\n" .
"--$this->mimeBoundary\n";
$this->hasAttachment = '1';
$this->attachmentData = $email_message;
}
The above sets the attachment to the mail object, and the below code sends it
function send($from, $to, $subject, $message, $headers=null) {
if($this->hasAttachment == '1')
{
$headers .= "\nMIME-Version: 1.0\n" .
"Content-Type: multipart/mixed;\n" .
" boundary=\"{$this->mimeBoundary}\"";
$message .= "This is a multi-part message in MIME format.\n\n" .
"--{$this->mimeBoundary}\n" .
"Content-Type:text/plain; charset=\"iso-8859-1\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" . $message ."\n\n" .
$this->attachmentData;
}
fputs($this->conn, 'MAIL FROM: <'. $from .'>'. $this->nl);
fgets($this->conn);
fputs($this->conn, 'RCPT TO: <'. $to .'>'. $this->nl);
fgets($this->conn);
fputs($this->conn, 'DATA'. $this->nl);
fgets($this->conn);
fputs($this->conn,
'From: '. $from .$this->nl.
'To: '. $to .$this->nl.
'Subject: '. $subject .$this->nl.
$headers .$this->nl.
$this->nl.
$message . $this->nl.
'.' .$this->nl
);
fgets($this->conn);
return;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您有兴趣,我为 Swiftmailer 编写了a Facade。下面是一个例子:
If you're interested I wrote a Facade for Swiftmailer. Below an example: