PHP mail()函数,gmail作为接收者(noname.txt作为附件而不是文本消息)


PHP mail() function with gmail as receiver (noname.txt as attachment instead of text message)

我有一个通过Php mail()函数发送电子邮件到gmail的问题。我用我的雅虎邮件测试了这个脚本,没有问题(电子邮件正文没有附件)。下面是我的代码:

$mailto = "info@email.com";
$charset = "utf-8";
$subject = "subject text";
$content = "text/html";
    $headers  = "MIME-Version: 1.0'n";
    $headers .= "Content-Type: $content  charset=$charset'n";
    $headers .= "Date: ".date("Y-m-d (H:i:s)",time())."'n";
    $headers .= "From: '"".$mailto."'" <".$mailto.">'n";
    $headers .= "X-Mailer: My Send E-mail'n";
    $message = "message text";
    mail($_POST['posEmail'],$subject,$message,$headers);

使用phpmailer类,它更安全,更容易使用。它支持附件和类似的

$headers .= "Content-Type: $content; charset=$charset'n";

内容和字符集之间的分号?

并尝试以'r'n结束,而不是'n。

$headers =  'To: ' . $epost . "'r'n";
$headers .= 'From: ' . $mailto . "'r'n";
$headers .= 'Message-ID: <' . $ordernummer . ">'r'n";
$headers .= 'MIME-Version: 1.0' . "'r'n";
$headers .= 'Content-Type: text/plain; charset=UTF-8' . "'r'n";
$headers .= 'Content-Transfer-Encoding: quoted-printable';

这些头文件有效。我还收到了名为noname.txt的附件邮件。

我添加了Message-ID, Content-Transfer-Encoding,从ToFrom中去掉了实名,跳过了电子邮件周围的<>,去掉了text/plainUTF-8周围的引号,我大写了

你只需要编辑你的php.ini然后把这个

SMTP = smtp.gmail.com

smtp_port = 25

sendmail_from = youremailid@gmail.com

从PHP页面使用GMail SMTP服务器发送电子邮件