代码之家  ›  专栏  ›  技术社区  ›  Drew Rich

Php mail()电子邮件发送到达gmail帐户,但不在常规电子邮件帐户中

  •  0
  • Drew Rich  · 技术社区  · 16 年前

    我有一个php脚本,一旦人们提交了一些信息,它就会发送带有附件的电子邮件。我在我的gmail收件箱中收到这些电子邮件,没有问题。但是,当我使用我的个人电子邮件地址或我的工作电子邮件地址时,电子邮件将永远不会发送。这是我的脚本(以下)或服务器上的某些设置的问题吗?我认为标题可能有问题,但每次我更改标题时,它们都会破坏电子邮件,所有内容都会显示在邮件正文中。有人知道如何解决这个问题吗?

    //define the receiver of the email
    $to = 'test@test.com';
    //define the subject of the email
    $subject = 'Email with Attachment';
    //create a boundary string. It must be unique
    //so we use the MD5 algorithm to generate a random hash
    $random_hash = md5(date('r', time()));
    //define the headers we want passed. Note that they are separated with \r\n
    $mime_boundary = "<<<--==+X[".md5(time())."]";
    
    $path = $_SERVER['DOCUMENT_ROOT'].'/two/php/';
    $fileContent =  chunk_split(base64_encode(file_get_contents($path.'CTF_brochure.pdf')));
    
    
    $headers .= "From: info@example.org.uk <info@poundsandpennies.org.uk>\r\n";  
    
    $headers .= "MIME-Version: 1.0\r\n";
    $headers .= "Content-Type: multipart/mixed;\r\n";
    $headers .= " boundary=\"".$mime_boundary."\"";
    
    
    
    
    $message = "This is a multi-part message in MIME format.\r\n";
    
    $message .= "\r\n";
    $message .= "--".$mime_boundary."\r\n";
    
    $message .= "Content-Type: text/plain; charset=\"iso-8859-1\"\r\n";
    $message .= "Content-Transfer-Encoding: 7bit\r\n";
    $message .= "\r\n";
    $message .= "Email content and what not: \r\n";
    $message .= "This is the file you asked for! \r\n";
    $message .= "--".$mime_boundary."" . "\r\n";
    
    $message .= "Content-Type: application/octet-stream;\r\n";
    $message .= " name=\"CTF-brochure.pdf\"" . "\r\n";
    $message .= "Content-Transfer-Encoding: base64 \r\n";
    $message .= "Content-Disposition: attachment;\r\n";
    $message .= " filename=\"CTF_brochure.pdf\"\r\n";
    $message .= "\r\n";
    $message .= $fileContent;
    $message .= "\r\n";
    $message .= "--".$mime_boundary."\r\n";
    
    //send the email
    $mail_sent = mail($to, $subject, $message, $headers);
    //if the message is sent successfully print "Mail sent". Otherwise print "Mail failed"
    echo $mail_sent ? "Mail sent" : "Mail failed";
    
    3 回复  |  直到 16 年前
        1
  •  2
  •   Frank    16 年前

    尝试此多部分替代版本。选择允许html和客户端同时显示的纯文本消息。

    
    //define the receiver of the email
    $to = 'test@test.com';
    //define the subject of the email
    $subject = 'Email with Attachment';
    //create 2 boundary strings. They must be unique
    $boundary1 = rand(0,9)."-"
                                .rand(10000000000,9999999999)."-"
                                .rand(10000000000,9999999999)."=:"
                                .rand(10000,99999);
    $boundary2 = rand(0,9)."-".rand(10000000000,9999999999)."-"
                                .rand(10000000000,9999999999)."=:"
                                .rand(10000,99999);
    
    $path = $_SERVER['DOCUMENT_ROOT'].'/two/php/';
    $fileContent =  chunk_split(base64_encode(file_get_contents($path.'CTF_brochure.pdf')));
    
    $txt_message = "Email content and what not: \r\n";
    $txt_message .= "This is the file you asked for! \r\n";
    
    $html_message = "Email content and what not: <br />";
    $html_message .= "This is the file you asked for! ";
    
    $headers     =<<<AKAM
    From: info@example.org.uk <info@poundsandpennies.org.uk>
    MIME-Version: 1.0
    Content-Type: multipart/mixed;
        boundary="$boundary1"
    AKAM;
    
    $attachment = <<<ATTA
    --$boundary1
    Content-Type: application/octet-stream;
        name="CTF_brochure.pdf"
    Content-Transfer-Encoding: base64
    Content-Disposition: attachment;
        filename="CTF_brochure.pdf"
    
    $fileContent
    
    ATTA;
    
    $body = <<<AKAM
    This is a multi-part message in MIME format.
    
    --$boundary1
    Content-Type: multipart/alternative;
        boundary="$boundary2"
    
    --$boundary2
    Content-Type: text/plain;
        charset=ISO-8859-1;
    Content-Transfer-Encoding: 7bit
    
    $txt_message
    --$boundary2
    Content-Type: text/html;
        charset=ISO-8859-1;
    Content-Transfer-Encoding: 7bit
    
    $html_message
    
    --$boundary2--
    
    $attachment
    --$boundary1--
    AKAM;
    
    //send the email
    $mail_sent = @mail($to, $subject, $body, $headers);
    //if the message is sent successfully print "Mail sent". Otherwise print "Mail failed"
    echo $mail_sent ? "Mail sent" : "Mail failed";
    
        2
  •  0
  •   John Nicely    16 年前

    检查其他帐户上的垃圾邮件文件夹。有时,如果电子邮件帐户提供商正在使用的任何垃圾邮件过滤软件触发过多警告,邮件最终可能会变成垃圾邮件。

        3
  •  0
  •   Siniseus    11 年前

    mail()函数非常有用,除了需要处理标题的时候。大多数电子邮件都会被没有正确标题的垃圾邮件机器人拒绝。

    我建议使用PHPMailer类,并让它完成所有工作。自己做一个是一种痛苦,不推荐。

    https://github.com/PHPMailer/PHPMailer

    它具有许多出色的功能,可以避免所有这些问题:)

    推荐文章