PHP发送邮件到id的问题

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (2)
  • 关注 (0)
  • 查看 (19)

我已经尝试了很长时间但直到现在都失败了。

我已经尝试更改表单名称或属性名称但不起作用。

这是我的表单的代码:

<form action="contact_process.php" class="office_contact_form" id="contactForm" method="post" name="contactForm" novalidate="">
  <div class="form-group col-md-12">
    <input class="form-control" id="name" name="name" placeholder="Name" type="text">
  </div>
  <div class="form-group col-md-12">
    <input class="form-control" id="email" name="email" placeholder="Email Address *" type="text">
  </div>
  <div class="form-group col-md-12">
    <input class="form-control" id="subject" name="subject" placeholder="Subject" type="text">
  </div>
  <div class="form-group col-md-12">
    <textarea class="form-control" id="message" name="message" placeholder="Your Message" rows="1"></textarea>
  </div>
  <div class="form-group col-md-12">
    <button class="btn p_btn" type="submit" value="submit">Send Message</button>
  </div>
</form>

这是我的PHP代码:

<?php
  $to = "hello1224@gmail.com";
  $from = $_REQUEST['yourname'];
  $name = $_REQUEST['youremail'];
  $headers = "From: $from";
  $subject = "You have a message from your attornyeproducts.com";

  $fields = array();
  $fields{"yourname"} = "name";
  $fields{"youremail"} = "email";
  $fields{"subject"} = "subject";
  $fields{"phone"} = "phone";
  $fields{"message"} = "message";
  $body = "Here is what was sent:\n\n"; foreach($fields as $a => $b){   $body .= sprintf("%20s: %s\n",$b,$_REQUEST[$a]); }

  $send = mail($to, $subject, $body, $headers);
?>

我正在尝试从此联系表单中接收数据到电子邮件的ID。

提问于
用户回答回答于

我没有测试你的字段。您可以测试您的字段以确保它们具有正确的值,然后将这些值传递给此函数。

我建议您使用FILTER_VALIDATE_EMAIL确保您拥有有效的电子邮件。

您还可以检查您期望实际存在的帖子键:$ message =(isset($ _ POST ['message']))?$ _ POST ['message']:'default message';

function send($subject,$msg,$email,$from,$replyto=null){
    $replyto=(isset($replyto) && filter_var($replyto, FILTER_VALIDATE_EMAIL) )?$replyto:'contact@mydomain.com';
    $params="-fcontact@mydomain.com";
    $subject = $subject;
    $message = "<div style='font-family: Arial, Helvetica, sans-serif;'>"; 
    $message .= $msg;
    $message .="</div>";

    $headers = "From: =?utf-8?b?".base64_encode($from)."?= <contact@mydomain.com>\r\n";
    $headers .= "Content-type: text/html; charset=UTF-8\r\n"; 
    $headers .= 'Bcc: info@mydomain.com' . "\r\n";
    $headers .= 'Reply-To: '.$replyto . "\r\n";
    $headers .= 'X-Mailer: PHP/' . phpversion(); 
    $to = $email;

    if(isset($_SERVER['REMOTE_ADDR']) && in_array( $_SERVER['REMOTE_ADDR'], array( '127.0.0.1', '::1' ))) return true;

    return mail($email, $subject, $message, $headers,$params);
}
用户回答回答于

你试过了吗https://github.com/PHPMailer/PHPMailer ?

simple example: https://github.com/PHPMailer/PHPMailer

示例以适合您的情况:

<?php

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

// Load Composer's autoloader
require 'vendor/autoload.php';

// Instantiation and passing `true` enables exceptions
$mail = new PHPMailer(true);

try {
    //Server settings
    $mail->SMTPDebug = 2;                                       // Enable verbose debug output
    $mail->isSMTP();                                            // Set mailer to use SMTP
    $mail->Host       = 'smtp1.example.com;smtp2.example.com';  // Specify main and backup SMTP servers
    $mail->SMTPAuth   = true;                                   // Enable SMTP authentication
    $mail->Username   = 'user@example.com';                     // SMTP username
    $mail->Password   = 'secret';                               // SMTP password
    $mail->SMTPSecure = 'tls';                                  // Enable TLS encryption, `ssl` also accepted
    $mail->Port       = 587;                                    // TCP port to connect to

    //Recipients
    $mail->setFrom("YOUREMAIL@gmail.com");
    $mail->addAddress("hello1224@gmail.com");     // Add a recipient

  $fields = array(
      "yourname" => $_REQUEST['yourname'],
      "youremail" => $_REQUEST['youremail'],
      "subject" => $subject ,
      "phone" => "phone",
      "message" => "message",
  );
  $body = "Here is what was sent:\n\n"; foreach($fields as $a => $b){   
  $body .= sprintf("%20s: %s\n",$b,$_REQUEST[$a]); }


    // Content
    $mail->isHTML(true);                                  // Set email format to HTML
    $mail->Subject = $subject
    $mail->Body    = $body;
    $mail->AltBody = $body;

    $mail->send();
    echo 'Message has been sent';
} catch (Exception $e) {
    echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}

扫码关注云+社区

领取腾讯云代金券