当前位置: 首页 > 知识库问答 >
问题:

如何在AMP中创建电子邮件发送程序

周麒
2023-03-14

我是一名新开发人员,致力于用AMP代码构建“mywebsite.com”网站。我想创建一个页面,其中包含用户名、电子邮件地址和消息的输入字段。我希望用户能够输入这三个字段,并将用户名、电子邮件地址和邮件发送到“myemail@gmail.com”

以下是我页面的超文本标记语言:

<!doctype html>
<html AMP lang="en">
<head>
    <meta charset="utf-8">
    <title>mywebsite</title>
    <link rel="canonical" href="https://www.mywebsite.com/index.html" />
    <meta name="viewport" content="width=device-width,minimum-scale=1,maximum-scale=1,initial-scale=1">

	<script async custom-element="amp-form" src="https://cdn.ampproject.org/v0/amp-form-0.1.js"></script>
	<script async custom-template="amp-mustache" src="https://cdn.ampproject.org/v0/amp-mustache-0.1.js"></script>
    <script async  src="https://cdn.ampproject.org/v0.js"></script>
    
	<style amp-boilerplate>body{-webkit-animation:-amp-start 8s steps(1,end) 0s 1 normal both;-moz-animation:-amp-start 8s steps(1,end) 0s 1 normal both;-ms-animation:-amp-start 8s steps(1,end) 0s 1 normal both;animation:-amp-start 8s steps(1,end) 		0s 1 normal both}@-webkit-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-moz-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-ms-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-o-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}</style><noscript><style amp-boilerplate>body{-webkit-animation:none;-moz-animation:none;-ms-animation:none;animation:none}</style></noscript>
    
<style amp-custom>
form.amp-form-submit-success [submit-success],
form.amp-form-submit-error [submit-error]{
  margin-top: 16px;
}
form.amp-form-submit-success [submit-success] {
  color: green;
}
form.amp-form-submit-error [submit-error] {
  color: red;
}
form.amp-form-submit-success.hide-inputs > input {
  display: none;
}
</style>

</head>

<body>

<form method="post"
  class="p2"
  action-xhr="MAILER.php"
  target="_top">
  
  <div class="ampstart-input inline-block relative m0 p0 mb3">
    <input type="text"
      class="block border-none p0 m0"
      name="name"
      placeholder="Name..."
      required>
    <input type="email"
      class="block border-none p0 m0"
      name="email"
      placeholder="Email..."
      required>
      <input type="text"
      class="block border-none p0 m0"
      name="message"
      placeholder="Message..."
      required>
  </div>
  <input type="submit"
    value="Send"
    class="ampstart-btn caps">
  <div submit-success>
    <template type="amp-mustache">
      Success! Thanks {{name}} for your message.
    </template>
  </div>
  <div submit-error>
    <template type="amp-mustache">
      Error!
    </template>
  </div>
</form>

</body>
</html>

这是我的php文件:

<?php

	$source_origin = trim($_REQUEST['__amp_source_origin']);//Security
	if($source_origin != "https://mywebsite.com"){
	echo "Not allowed origin";
	return;
	}
	header('AMP-Access-Control-Allow-Source-Origin: https://mywebsite.com');
	header('Content-Type: application/json; charset=UTF-8;'); 

    // variables start
	$name = "";
	$email = "";
	$message = "";
	
	$name =  trim($_REQUEST['name']);
	$email =  trim($_REQUEST['email']);
	$message =  trim($_REQUEST['message']);
	// variables end
	
	// email address starts
	$emailAddress = 'admin@mywebsite.com';
	// email address ends
	
	$subject = "Message From: $name";	
	$message = "<strong>From:</strong> $name <br/><br/> <strong>Message:</strong> $message";
	
	$headers = '';
	$headers .= 'From: '. $name . '<' . $email . '>' . "\r\n";
	$headers .= 'Reply-To: ' . $email . "\r\n";
	
	$headers .= 'MIME-Version: 1.0' . "\r\n";
	$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
	
	//send email function starts
	$result = mail($emailAddress, $subject, $message, $headers);

	if($result === true && !empty($emailAddress)){
		echo json_encode(array("name"=>$name,"email"=>$email));
	}else{
		header('Status: 400', TRUE, 400);
		echo json_encode(array('message'=>'This is error message'));
	}
	//send email function ends
?>

在真正的代码中,我用我的网站URL和电子邮件代替了我的网站。com和admin@mywebsite.com.

我已经在这个论坛上反映了一些人的意见,不幸的是,这根本不起作用。没有指示邮件已成功发送的消息,也没有错误消息。最后,没有电子邮件发送到admin@mywebsite.com.

我做错了什么?如何以AMP Valid格式创建这个简单的网页?

提前谢谢大家!

共有1个答案

岑驰
2023-03-14

梅勒。php:

<?php
if(!empty($_POST)){
$name=$_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$formcontent=" From: $name \n Message: $message";
$recipient = "admin@mywebsite.com";
$subject = "Message From: $name";
$mailheader = "From: $email \r\n";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");

       $domain_url = (isset($_SERVER['HTTPS']) ? "https" : "http") . "://$_SERVER[HTTP_HOST]";
        header("Content-type: application/json");
        header("Access-Control-Allow-Credentials: true");
        header("Access-Control-Allow-Origin: ". str_replace('.', '-','https://mywebsite.com') .".cdn.ampproject.org");
        header("AMP-Access-Control-Allow-Source-Origin: " . $domain_url);
        header("Access-Control-Expose-Headers: AMP-Access-Control-Allow-Source-Origin");
        header("AMP-Redirect-To: https://mywebsite.com/index.html");
        header("Access-Control-Expose-Headers: AMP-Redirect-To, AMP-Access-Control-Allow-Source-Origin"); 
        echo json_encode(array('name' => $name));
        exit;
}
?>

代替https://mywebsite.com/到您的域url

 类似资料:
  • 问题内容: 我最近开始编程我的第一个node.js。但是,我发现我无法创建直接发送到我的电子邮件的“联系我”表单,因为我无法从能够发送电子邮件的节点中找到任何模块。 有人知道node.js电子邮件库或示例联系表单脚本吗? 问题答案: node-email-templates是一个更好的选择:https : //github.com/niftylettuce/node-email- template

  • 本文向大家介绍如何创建一个链接以HTML发送电子邮件?,包括了如何创建一个链接以HTML发送电子邮件?的使用技巧和注意事项,需要的朋友参考一下 要创建发送电子邮件的链接,请使用带有href属性的<a>标记。该的mailto链接<a>标签里面添加。只需记住在mailto链接中添加您想接收电子邮件的电子邮件地址。 示例 您可以尝试运行以下代码来创建一个链接,以HTML格式发送电子邮件。

  • 可能重复: 如何从我的Android应用程序发送电子邮件? 我正在创建一个main。xml 如何在android中发送电子邮件。但必须是固定id(例如:myid@example.com) 我有main。xml

  • 我知道Firebase不允许您使用第三方电子邮件服务发送电子邮件。所以唯一的方法是通过Gmail发送。 所以我在互联网上搜索了一些方法,这里有一个片段可以让我免费发送电子邮件。 我想创建一个模板,所以我在nodemailer中使用了这个名为的包。但是该函数不会在Firebase控制台中执行,也不会显示错误并显示与“计费”相关的警告。

  • 问题内容: 我已经使用JMS在Web应用程序中成功发送了电子邮件,但是结果仅以纯文本显示。我希望内容能够显示html。我该怎么做?这大致就是我所拥有的: 问题答案: 根据Javadoc,在需要时,这些设置将默认的mime类型设置为。而是使用代替。