【问题标题】:reply to sender mail php script回复发件人邮件php脚本
【发布时间】:2023-01-05 19:38:54
【问题描述】:

我一直在寻找一个邮件表单脚本并想出了他的。工作正常,但仅此而已:当我回复收到的电子邮件时,我是在回复自己,而我想自动回复发件人的邮件地址。

<?php
$to = "MY@MAILADRES.COM";
$subject = "mail via website";
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$email = $_POST['email'];
$onderwerp = $_POST['onderwerp'];
$comments = $_POST['message'];
$message = "
name: $firstname $lastname
email: $email

subject: $onderwerp

message: $comments
";
  $mime_boundary="==Multipart_Boundary_x".md5(mt_rand())."x";
     $headers = "From: MY@MAILADRES.COM\r\n".
     "MIME-Version: 1.0\r\n" .
        "Content-Type: multipart/mixed;\r\n" .
        " boundary=\"{$mime_boundary}\"";
     $message = "This is a multi-part message in MIME format.\n\n" .
        "--{$mime_boundary}\n" .
        "Content-Type: text/plain; charset=\"iso-8859-1\"\n" .
        "Content-Transfer-Encoding: 7bit\n\n" .
     $message . "\n\n";
     foreach($_FILES as $userfile)
     {
        $tmp_name = $userfile['tmp_name'];
        $type = $userfile['type'];
        $name = $userfile['name'];
        $size = $userfile['size'];
        if (file_exists($tmp_name))
        {
           if(is_uploaded_file($tmp_name))
           {
              $file = fopen($tmp_name,'rb');
              $data = fread($file,filesize($tmp_name));
              fclose($file);
              $data = chunk_split(base64_encode($data));
           }
           $message .= "--{$mime_boundary}\n" .
              "Content-Type: {$type};\n" .
              " name=\"{$name}\"\n" .
              "Content-Disposition: attachment;\n" .
              " filename=\"{$fileatt_name}\"\n" .
              "Content-Transfer-Encoding: base64\n\n" .
           $data . "\n\n";
        }
     }
     $message.="--{$mime_boundary}--\n";
if (mail($to, $subject, $message, $headers))
   echo "Thanks for getting in touch.<br>Your message wil get my full attention.<br>I     will get back to you soon.";
else
   echo "Error in mail.<br>Please try again.";
?>

请问我应该在此代码中更改什么?

【问题讨论】:

  • 你尝试过什么来解决这个问题?你卡在哪儿了?整个代码看起来你应该更好地重构它以使用任何现有的邮件库,如 Symfony Mailer、SwiftMailer、phpMailer

标签: email-headers reply mail-form


【解决方案1】:

您可以将电子邮件中的“回复”标题设置为发件人的电子邮件地址,以便将对电子邮件的回复发送给发件人。

为此,您可以向 $headers 变量添加一个名为“Reply-To”的新标头,如下所示:

$headers = "From: MY@MAILADRES.COM
" .
           "Reply-To: $email
" .
           "MIME-Version: 1.0
" .
           "Content-Type: ipart/mixed;
" .
           " boundary="{$mime_boundary}"";

这会将“Reply-To”标头设置为 $email 变量的值,即发件人的电子邮件地址。

【讨论】:

    猜你喜欢
    • 2013-06-29
    • 1970-01-01
    • 2013-10-03
    • 2013-07-30
    • 1970-01-01
    • 1970-01-01
    • 2017-07-16
    • 2012-09-12
    • 1970-01-01
    相关资源
    最近更新 更多