Php 表单提交不适用于Gsuite电子邮件,但适用于任何其他电子邮件
我是一个相当新的开发人员,终于遇到了我的第一个无法解决的问题 我使用了一个可重用的表单,并在我自己的两个Gmail帐户上成功地测试了它(example@gmail.com)但是,只要我使用客户的专业G套件电子邮件(info@example.co.uk)它不起作用,电子邮件永远不会到达。我已经检查并重新检查了明显的东西,如拼写、缓存文件、垃圾邮件和垃圾文件夹,并多次重新上传代码。我已经通过了所有的Gsuite电子邮件设置,甚至将IP添加到电子邮件白名单和入站网关。什么都不管用 我想知道MX记录中是否有我必须做的事情?我没有尝试这个,因为它与我的其他电子邮件一起工作,并且在信息中没有关于这个的内容 我还考虑联系Gsuite,看看专业电子邮件中是否存在某种阻碍 以下是我正在使用的代码:Php 表单提交不适用于Gsuite电子邮件,但适用于任何其他电子邮件,php,html,forms,email,google-workspace,Php,Html,Forms,Email,Google Workspace,我是一个相当新的开发人员,终于遇到了我的第一个无法解决的问题 我使用了一个可重用的表单,并在我自己的两个Gmail帐户上成功地测试了它(example@gmail.com)但是,只要我使用客户的专业G套件电子邮件(info@example.co.uk)它不起作用,电子邮件永远不会到达。我已经检查并重新检查了明显的东西,如拼写、缓存文件、垃圾邮件和垃圾文件夹,并多次重新上传代码。我已经通过了所有的Gsuite电子邮件设置,甚至将IP添加到电子邮件白名单和入站网关。什么都不管用 我想知道MX记录中是
<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
/*
Tested working with PHP5.4 and above (including PHP 7 )
*/
require_once './vendor/autoload.php';
use FormGuide\Handlx\FormHandler;
$pp = new FormHandler();
$validator = $pp->getValidator();
$validator->fields(['name','email'])->areRequired()->maxLength(50);
$validator->field('email')->isEmail();
$validator->field('comments')->maxLength(6000);
$pp->sendEmailTo('info@example.co.uk'); // ← Your email here
echo $pp->process($_POST);
如果你说向一个普通的Gmail免费帐户发送电子邮件是可行的,但你使用的是一个G套件帐户,那么很可能不是一些G套件设置或DNS域设置,而是G套件域
如果是您所说的MX记录,请参考以下内容:
- 你有没有尝试过从gmail发送电子邮件到G套件(或者从任何其他客户发送,这都没关系),它能工作吗
- 要检查您是否在Goolge的MX记录点可以使用此工具箱,您只需放置域并选择MX:
如果没有(记录以粗体显示),则需要为Google服务器添加MX记录,如下所示:
- ASPMX.L.GOOGLE.COM:prioritá1
- ALT1.ASPMX.L.GOOGLE.COM:prioritá5
- ALT2.ASPMX.L.GOOGLE.COM:prioritá5
- ALT3.ASPMX.L.GOOGLE.COM:prioritá10
- ALT4.ASPMX.L.GOOGLE.COM:prioritá10
为G套件Gmail设置MX记录:
如果这不是问题,那么可能是其他问题,如果我理解正确,您的代码正在发送到电子邮件,而不是实际使用帐户通过SMPT发送
对吧?
还有其他错误吗?
你是这个G套件帐户的超级管理员吗
是否尝试启用不太安全的应用程序?控制对不太安全的应用程序的访问:最后只是一个学童MX错误。没有仔细检查细节是否匹配,并且有一个旧记录在那里导致混乱。无论如何谢谢你的帮助 您在那里使用的FormHandler类是什么,它到底是如何发送这些邮件的?通过SMTP进行正确的身份验证?如果是,您是否在任何位置为发件人指定了From
字段,并且该字段是否与您使用的SMTP凭据匹配?有很多原因可以解释为什么你在发送电子邮件时会遇到问题,请检查这些原因是否适用于你的情况。
<?php
namespace FormGuide\Handlx;
use FormGuide\PHPFormValidator\FormValidator;
use PHPMailer;
use FormGuide\Handlx\Microtemplate;
use Gregwar\Captcha\CaptchaBuilder;
/**
* FormHandler
* A wrapper class that handles common form handling tasks
* - handles Form validations using PHPFormValidator class
* - sends email using PHPMailer
* - can handle captcha validation
* - can handle file uploads and attaching the upload to email
*
* ==== Sample usage ====
* $fh = FormHandler::create()->validate(function($validator)
* {
* $validator->fields(['name','email'])
* ->areRequired()->maxLength(50);
* $validator->field('email')->isEmail();
*
* })->useMailTemplate(__DIR__.'/templ/email.php')
* ->sendEmailTo('you@website.com');
*
* $fh->process($_POST);
*/
class FormHandler
{
private $emails;
public $validator;
private $mailer;
private $mail_template;
private $captcha;
private $attachments;
private $recaptcha;
public function __construct()
{
$this->emails = array();
$this->validator = FormValidator::create();
$this->mailer = new PHPMailer;
$this->mail_template='';
$this->mailer->Subject = "Contact Form Submission ";
$host = isset($_SERVER['SERVER_NAME'])?$_SERVER['SERVER_NAME']:'localhost';
$from_email ='forms@'.$host;
$this->mailer->setFrom($from_email,'Contact Form',false);
$this->captcha = false;
$this->attachments = [];
$this->recaptcha =null;
}
/**
* sendEmailTo: add a recipient email address
* @param string/array $email_s one or more emails. If more than one emails, pass the emails as array
* @return The form handler object itself so that the methods can be chained
*/
public function sendEmailTo($email_s)
{
if(is_array($email_s))
{
$this->emails =array_merge($this->emails, $email_s);
}
else
{
$this->emails[] = $email_s;
}
return $this;
}
public function useMailTemplate($templ_path)
{
$this->mail_template = $templ_path;
return $this;
}
/**
* [attachFiles find the file uplods and attach to the email]
* @param array $fields The array of field names
*/
public function attachFiles($fields)
{
$this->attachments = array_merge($this->attachments, $fields);
return $this;
}
public function getRecipients()
{
return $this->emails;
}
/**
* [validate add Validations. This function takes a call back function which receives the PHPFormValidator object]
* @param function $validator_fn The funtion gets a validator parameter using which, you can add validations
*/
public function validate($validator_fn)
{
$validator_fn($this->validator);
return $this;
}
public function requireReCaptcha($config_fn=null)
{
$this->recaptcha = new ReCaptchaValidator();
$this->recaptcha->enable(true);
if($config_fn)
{
$config_fn($this->recaptcha);
}
return $this;
}
public function getReCaptcha()
{
return $this->recaptcha;
}
public function requireCaptcha($enable=true)
{
$this->captcha = $enable;
return $this;
}
public function getValidator()
{
return $this->validator;
}
public function configMailer($mailconfig_fn)
{
$mailconfig_fn($this->mailer);
return $this;
}
public function getMailer()
{
return $this->mailer;
}
public static function create()
{
return new FormHandler();
}
public function process($post_data)
{
if($this->captcha === true)
{
$res = $this->validate_captcha($post_data);
if($res !== true)
{
return $res;
}
}
if($this->recaptcha !== null &&
$this->recaptcha->isEnabled())
{
if($this->recaptcha->validate() !== true)
{
return json_encode([
'result'=>'recaptcha_validation_failed',
'errors'=>['captcha'=>'ReCaptcha Validation Failed.']
]);
}
}
$this->validator->test($post_data);
//if(false == $this->validator->test($post_data))
if($this->validator->hasErrors())
{
return json_encode([
'result'=>'validation_failed',
'errors'=>$this->validator->getErrors(/*associative*/ true)
]);
}
if(!empty($this->emails))
{
foreach($this->emails as $email)
{
$this->mailer->addAddress($email);
}
$this->compose_mail($post_data);
if(!empty($this->attachments))
{
$this->attach_files();
}
if(!$this->mailer->send())
{
return json_encode([
'result'=>'error_sending_email',
'errors'=> ['mail'=> $this->mailer->ErrorInfo]
]);
}
}
return json_encode(['result'=>'success']);
}
private function validate_captcha($post)
{
@session_start();
if(empty($post['captcha']))
{
return json_encode([
'result'=>'captcha_error',
'errors'=>['captcha'=>'Captcha code not entered']
]);
}
else
{
$usercaptcha = trim($post['captcha']);
if($_SESSION['user_phrase'] !== $usercaptcha)
{
return json_encode([
'result'=>'captcha_error',
'errors'=>['captcha'=>'Captcha code does not match']
]);
}
}
return true;
}
private function attach_files()
{
foreach($this->attachments as $file_field)
{
if (!array_key_exists($file_field, $_FILES))
{
continue;
}
$filename = $_FILES[$file_field]['name'];
$uploadfile = tempnam(sys_get_temp_dir(), sha1($filename));
if (!move_uploaded_file($_FILES[$file_field]['tmp_name'],
$uploadfile))
{
continue;
}
$this->mailer->addAttachment($uploadfile, $filename);
}
}
private function compose_mail($post)
{
$content = "Form submission: \n\n";
foreach($post as $name=>$value)
{
$content .= ucwords($name).":\n";
$content .= "$value\n\n";
}
$this->mailer->Body = $content;
}
}