首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >PHP在发送邮件到id时遇到问题,请联系我们

PHP在发送邮件到id时遇到问题,请联系我们
EN

Stack Overflow用户
提问于 2019-06-11 02:59:10
回答 2查看 45关注 0票数 0

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

我尝试过更改窗体名称或属性名称,但都不起作用。

下面是我的表单的代码:

代码语言:javascript
复制
<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代码:

代码语言:javascript
复制
<?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的数据。

EN

回答 2

Stack Overflow用户

发布于 2019-06-11 03:09:36

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

简单的例子:https://github.com/PHPMailer/PHPMailer

适合您的情况的示例:

代码语言:javascript
复制
<?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}";
}
票数 0
EN

Stack Overflow用户

发布于 2019-06-11 03:16:59

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

我建议你使用FILTER_VALIDATE_EMAIL来确保你有一个有效的电子邮件。

你也可以检查你期望的post关键字是否确实存在:$message=(isset($_POST'message'))?$_POST'message':'default message';

代码语言:javascript
复制
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);
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56532073

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档