【问题标题】:Send a Copy of email to Sender PHP contact script向发件人发送电子邮件副本 PHP 联系脚本
【发布时间】:2013-10-03 19:40:25
【问题描述】:

我正在使用以下 PHP 脚本从我的联系表中获取电子邮件。我也想将同一电子邮件的副本发送到发件人电子邮件。我该怎么做...有人可以在我的脚本中添加一些行来做到这一点..

我可以在 $emailTo 中分隔多个电子邮件,例如$emailTo = 'owner@website.com, $email';

if(!isset($hasError)) {

    $emailFrom = 'smptcontactemail@website.com';
    $emailTo = 'owner@website.com';
    $subject = 'Submitted message from '.$name;
    $sendCopy = trim($_POST['sendCopy']);
    $body = "Name: $name \n\nEmail: $email \n\nComments: $comments";
    $headers = 'From: ' .' <'.$emailFrom.'>' . "\r\n" . 'Reply-To: ' . $email;

    mail($emailTo, $subject, $body, $headers);

    // set our boolean completion value to TRUE
    $emailSent = true;
}

添加另一个 FUNCTION 可能会起作用。但我正在寻找其他一些简单的方法来在同一功能中做到这一点:)

PS:我想向发件人添加一个附加行,例如“您发送给 xxx 所有者的邮件的电子邮件副本”

【问题讨论】:

  • 内置的 PHP mail() 函数严重缺乏功能,而且一旦您开始编写自己的邮件标头,安全性通常是一个问题(您的代码很可能被黑客攻击以发送垃圾邮件)。因此,我强烈建议在编写 PHP 程序来发送电子邮件时使用像 PHPMailer 或 Swiftmailer 这样不错的邮件程序类。
  • @Spudley 感谢您的建议。我会调查的!

标签: php email


【解决方案1】:

请参阅php.net/mail 示例#4,了解如何扩展$additional_headers 参数以添加抄送收件人:

$headers = 'From: ' .' &lt;'.$emailFrom.'&gt;' . "\r\n" . 'Reply-To: ' . $email . "\r\n" . 'Cc: ' .' &lt;some@mail.com&gt;';

【讨论】:

    【解决方案2】:

    那就是

    $headers .= 'Cc: anotheremail@domain.net' . "\r\n";
    

    【讨论】:

      【解决方案3】:

      你可以通过写作来做到这一点

      $emailTo = 'owner@website.com, customer@website.com';
      

      【讨论】:

      • 我想向发件人发送一些额外的行。比如你的电子邮件副本之类的.....
      • 在这种情况下,您需要发送两封单独的邮件,因此调用 mail() 两次
      【解决方案4】:

      您可以尝试像这样将抄送副本发送到发件人电子邮件

      $headers["From"] = "$from";
      $headers["To"] = "$email";
      $headers["Subject"] = $subject;
      $headers['Cc'] = '$from';
      

      【讨论】:

      • mail() 需要一个字符串,而不是一个数组,用于附加参数
      • 这个概念是对的,但代码不是你需要附加的,看看 Alma Do Mundo 的回答
      【解决方案5】:

      希望对你有帮助

      <?php
                  $name = $_POST['name'];
                  $email_address = $_POST['email'];
                  $phone = $_POST['phone'];
                  $course = $_POST['course'];
      
      // Create the email and send the message
      
                  $to = $email_address; //This is the email address of the person who filled the form, this email will be supplied by the sender.
      
                  $email_subject = "Website Contact Form:  $name";
      
                  $email_body = "You have received a new message from your Website contact form.\n\n"."Here are the details:\n\n
                  Name: $name\n\n
                  Email: $email_address\n\n
                  Phone: $phone\n\n
                  Course:\n$course";
      
                  $headers = "From: noreply@yourdomain\n"; // This is the email address the generated message will be from. We recommend using something like noreply@yourdomain.com.
      
                  $headers .= 'Cc: youremail@yourdomain.com' . "\r\n"; // Add your email address  replacing yourname@yourdomain.com - This is where the form will send the message to.
      
                  $headers .= "Reply-To: $email_address"; 
      
                  mail($to, $email_subject,$email_body,$headers);
      
                  return true;            
      ?>
      

      【讨论】:

        猜你喜欢
        • 2015-08-25
        • 1970-01-01
        • 2011-09-16
        • 1970-01-01
        • 2016-01-27
        • 1970-01-01
        • 1970-01-01
        • 2012-09-12
        • 2012-06-24
        相关资源
        最近更新 更多