[英]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.