繁体   English   中英

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

[英]HTML and PHP Code Not Sending Email

我一直在尝试使用PHP发送电子邮件,并且发现此代码很适合我的需要,因为它很容易适合我当前网站的代码。 但是,我将整个代码放入文件中并进行了测试,它不会发送电子邮件。

码:

<?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> 

我无法获得将其发送至“ heroicostrich@gmail.com”的电子邮件。 我是在做错什么,还是忘了填写?

因为,您在本地使用它,并且您的邮件服务器未配置:

您可以配置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>');
}

有时, 发件人电子邮件收件人电子邮件应该是在网站的邮件服务器或cPanel上配置的电子邮件 出于安全原因,大多数托管服务提供商均不允许使用此设备。 将其更改为在您网站的邮件服务器上配置的电子邮件地址,然后该地址即可正常工作。

问题不在于代码,但如果没有“ SMTP”服务器,您将无法发送电子邮件! 链接

暂无
暂无

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

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