Zend Framework 2 - 使用 Zend\Mail 发送表单
Zend Framework 2 - Sending form using Zend\Mail
所以我正在制作联系页面。当然它会有一个表格,需要发送。
我让 class 发送电子邮件:
<?php
namespace Site\Mail;
use Zend\Mail\Message;
use Zend\Mail\Transport\Sendmail;
class SendEMail {
public function SendEMail($to, $from, $subject, $body) {
$message = new Message();
$message->addTo($to)
->addFrom($from)
->setSubject($subject)
->setBody($body);
$transport = new Sendmail();
try {
$transport->send($message);
return true;
} catch (\Exception $e) {
return false;
}
}
}
?>
在我的控制器中
public function emailAction () {
$request = $this->getRequest();
$vm = new ViewModel();
$vm->setTerminal(true);
if ($request->isPost()) {
$to = "some@mail.com";
$from = $request->getPost("email");
$subject = "Messaggio inviato da ".$request->getPost("nome")." (tel. ".$request->getPost("phone").")";
$body = $request->getPost("messaggio");
$mail = new SendEMail($to, $from, $subject, $body);
if (!$mail) {
$vm->setVariables(array(
"result" => "no"
));
return $vm;
} else {
$vm->setVariables(array(
"result" => "yes"
));
return $vm;
}
}
$vm->setVariables(array(
"result" => "This script needs POSTDATA."
));
return $vm;
}
我禁用了布局以允许 Ajax/Jquery 脚本加载它,它就像一个魅力(我会告诉你它,虽然我不认为它导致我的问题)
$(document).on('submit', 'form', function() {
$.ajax({
url:$("form").attr("action"),
method:"POST",
beforeSend: function() {
$("form").css("opacity", 0.5);
},
complete: function() {
$("form").css("opacity", 1);
},
success: function(responseText) {
$("body").append("<div id='popupmessage'>"+responseText+"</div>");
},
error: function(errorThrown) {
$("body").append("<div id='popupmessage'>"+errorThrown+"</div>");
}
}).always(function() {
$("#popupmessage").delay(1000).fadeOut(300, function () {
if($(this).length>0) {
$(this).remove();
}
});
});
return false;
});
问题是我的 SendEMail()
函数看起来总是 return 正确,即使我在 sendmail.ini 中设置了错误的设置(并且我正在使用 sendmail)。
我的错误是什么?我感谢任何帮助。谢谢。
当我实现我的电子邮件方法时,我始终无法获得异常:
catch (\Exception $e) {
return false;}
对我来说,得到它并不是那么重要。但在某些时候,我注意到当域不存在或域错误时我得到了一个异常,所以我将异常范围缩小到。
} catch (\Zend\Mail\Protocol\Exception\RuntimeException $e) {
$error = true;
}
因此,如果这不是您想要的,我会假设进行此更改会为您提供比 runtimeException 更多的信息,或者您可以保持它的方式假设此 return 您想要的结果
} catch (\Zend\Mail\Protocol\Exception $e) {
$error = true;
}
希望对您有所帮助。祝你好运。
更新:
我注意到我正在使用 SMTP 传输 (more details) 所以这就是我使用协议异常的原因。如果您查看 Zend Library,您将看到另一个文件夹 Zend\Mail\Exception。因此,请在 try catch 中尝试一下,例如
} catch (\Zend\Mail\Exception $e) {
$error = true;
}
如果这不起作用,请尝试缩小异常范围:在文件夹中,您将看到 6 个异常 类 在 try catch (\Zend\Mail\Exception\AnExceptionClass)[=15 中测试每个异常=]
希望对您有所帮助。
所以我正在制作联系页面。当然它会有一个表格,需要发送。 我让 class 发送电子邮件:
<?php
namespace Site\Mail;
use Zend\Mail\Message;
use Zend\Mail\Transport\Sendmail;
class SendEMail {
public function SendEMail($to, $from, $subject, $body) {
$message = new Message();
$message->addTo($to)
->addFrom($from)
->setSubject($subject)
->setBody($body);
$transport = new Sendmail();
try {
$transport->send($message);
return true;
} catch (\Exception $e) {
return false;
}
}
}
?>
在我的控制器中
public function emailAction () {
$request = $this->getRequest();
$vm = new ViewModel();
$vm->setTerminal(true);
if ($request->isPost()) {
$to = "some@mail.com";
$from = $request->getPost("email");
$subject = "Messaggio inviato da ".$request->getPost("nome")." (tel. ".$request->getPost("phone").")";
$body = $request->getPost("messaggio");
$mail = new SendEMail($to, $from, $subject, $body);
if (!$mail) {
$vm->setVariables(array(
"result" => "no"
));
return $vm;
} else {
$vm->setVariables(array(
"result" => "yes"
));
return $vm;
}
}
$vm->setVariables(array(
"result" => "This script needs POSTDATA."
));
return $vm;
}
我禁用了布局以允许 Ajax/Jquery 脚本加载它,它就像一个魅力(我会告诉你它,虽然我不认为它导致我的问题)
$(document).on('submit', 'form', function() {
$.ajax({
url:$("form").attr("action"),
method:"POST",
beforeSend: function() {
$("form").css("opacity", 0.5);
},
complete: function() {
$("form").css("opacity", 1);
},
success: function(responseText) {
$("body").append("<div id='popupmessage'>"+responseText+"</div>");
},
error: function(errorThrown) {
$("body").append("<div id='popupmessage'>"+errorThrown+"</div>");
}
}).always(function() {
$("#popupmessage").delay(1000).fadeOut(300, function () {
if($(this).length>0) {
$(this).remove();
}
});
});
return false;
});
问题是我的 SendEMail()
函数看起来总是 return 正确,即使我在 sendmail.ini 中设置了错误的设置(并且我正在使用 sendmail)。
我的错误是什么?我感谢任何帮助。谢谢。
当我实现我的电子邮件方法时,我始终无法获得异常:
catch (\Exception $e) { return false;}
对我来说,得到它并不是那么重要。但在某些时候,我注意到当域不存在或域错误时我得到了一个异常,所以我将异常范围缩小到。
} catch (\Zend\Mail\Protocol\Exception\RuntimeException $e) {
$error = true;
}
因此,如果这不是您想要的,我会假设进行此更改会为您提供比 runtimeException 更多的信息,或者您可以保持它的方式假设此 return 您想要的结果
} catch (\Zend\Mail\Protocol\Exception $e) {
$error = true;
}
希望对您有所帮助。祝你好运。
更新:
我注意到我正在使用 SMTP 传输 (more details) 所以这就是我使用协议异常的原因。如果您查看 Zend Library,您将看到另一个文件夹 Zend\Mail\Exception。因此,请在 try catch 中尝试一下,例如
} catch (\Zend\Mail\Exception $e) {
$error = true;
}
如果这不起作用,请尝试缩小异常范围:在文件夹中,您将看到 6 个异常 类 在 try catch (\Zend\Mail\Exception\AnExceptionClass)[=15 中测试每个异常=]
希望对您有所帮助。