简体   繁体   English

HTML和PHP代码未发送电子邮件

[英]HTML and PHP Code Not Sending Email

I've been trying to send an email using PHP, and I've found this code that suits my needs as it will easily fit into my current website's code. 我一直在尝试使用PHP发送电子邮件,并且发现此代码很适合我的需要,因为它很容易适合我当前网站的代码。 However, I put this entire code into a file and tested it out, and it won't send the email. 但是,我将整个代码放入文件中并进行了测试,它不会发送电子邮件。

Code: 码:

<?php 
if(isset($_POST['submit'])){
    $to = "heroicostrich@gmail.com"; // this is your Email address
    $from = $_POST['email']; // this is the sender's Email address
    $first_name = $_POST['first_name'];
    $last_name = $_POST['last_name'];
    $subject = "Form submission";
    $subject2 = "Copy of your form submission";
    $message = $first_name . " " . $last_name . " wrote the following:" . "\n\n" . $_POST['message'];
    $message2 = "Here is a copy of your message " . $first_name . "\n\n" . $_POST['message'];

    $headers = "From:" . $from;
    $headers2 = "From:" . $to;
    mail($to,$subject,$message,$headers);
    mail($from,$subject2,$message2,$headers2); // sends a copy of the message to the sender
    echo "Mail Sent. Thank you " . $first_name . ", we will contact you shortly.";
    // You can also use header('Location: thank_you.php'); to redirect to another page.
    }
?>

<!DOCTYPE html>
<head>
<title>Form submission</title>
</head>
<body>

<form action="" method="post">
First Name: <input type="text" name="first_name"><br>
Last Name: <input type="text" name="last_name"><br>
Email: <input type="text" name="email"><br>
Message:<br><textarea rows="5" name="message" cols="30"></textarea><br>
<input type="submit" name="submit" value="Submit">
</form>

</body>
</html> 

I am unable to get this to send an email to "heroicostrich@gmail.com". 我无法获得将其发送至“ heroicostrich@gmail.com”的电子邮件。 Am I doing something wrong, or did I forgot to fill something out? 我是在做错什么,还是忘了填写?

As, you are using it locally, and your mail server is not configured: 因为,您在本地使用它,并且您的邮件服务器未配置:

You can configure Gmail SMTP for that to work like this : 您可以配置SMTP的Gmail对于像工作这样

// Pear Mail Library
require_once "Mail.php";

$from = '<fromaddress@gmail.com>';
$to = '<toaddress@yahoo.com>';
$subject = 'Hi!';
$body = "Hi,\n\nHow are you?";

$headers = array(
    'From' => $from,
    'To' => $to,
    'Subject' => $subject
);

$smtp = Mail::factory('smtp', array(
        'host' => 'ssl://smtp.gmail.com',
        'port' => '465',
        'auth' => true,
        'username' => 'johndoe@gmail.com',
        'password' => 'passwordxxx'
    ));

$mail = $smtp->send($to, $headers, $body);

if (PEAR::isError($mail)) {
    echo('<p>' . $mail->getMessage() . '</p>');
} else {
    echo('<p>Message successfully sent!</p>');
}

It also occurs sometimes that either your sender email or recipient email should be an email that is configured on your website's mail server or cPanel . 有时, 发件人电子邮件收件人电子邮件应该是在网站的邮件服务器或cPanel上配置的电子邮件 Due to security reasons, most hosting service providers do not allow this thing. 出于安全原因,大多数托管服务提供商均不允许使用此设备。 Change it to an email address which is configured on your website's mail server, it should work then. 将其更改为在您网站的邮件服务器上配置的电子邮件地址,然后该地址即可正常工作。

the problem is not the code but you aren't able to send e-mail without a "SMTP" server! 问题不在于代码,但如果没有“ SMTP”服务器,您将无法发送电子邮件! Link 链接

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM