我正在处理一个表单,当用户输入他们的电子邮件帐户并点击发送时,一封电子邮件将发送到他们的电子邮件帐户。
我已经解决了所有问题。只是它不会将电子邮件发送到我的帐户。谁有想法?是否有我遗漏的配置?
这是来 self 的 Controller 的示例:
public function retrieveemailAction(){
$users = new Users();
$email = $_POST['email'];
$view = Zend_Registry::get('view');
if($users->checkEmail($_POST['email'])) {
// The Subject
$subject = "Email Test";
// The message
$message = "this is a test";
// Send email
// Returns TRUE if the mail was successfully accepted for delivery, FALSE otherwise.
// Use if command to display email message status
if(mail($email, $subject, $message, $headers)) {
$view->operation = 'true';
}
} else {
$view->operation = 'false';
}
$view->render('retrieve.tpl');
}
最佳答案
我建议您使用 Zend_Mail
而不是 mail()
。它会自动处理很多事情,而且效果很好。
您有 SMTP 服务器吗?尝试在没有您自己的 SMTP 服务器的情况下发送邮件可能会导致邮件无法发送。
这就是我使用 Zend_Mail
和 Gmail 发送邮件的方式:
在 Bootstrap.php
中,我配置了默认邮件传输:
protected function _initMail()
{
try {
$config = array(
'auth' => 'login',
'username' => 'username@gmail.com',
'password' => 'password',
'ssl' => 'tls',
'port' => 587
);
$mailTransport = new Zend_Mail_Transport_Smtp('smtp.gmail.com', $config);
Zend_Mail::setDefaultTransport($mailTransport);
} catch (Zend_Exception $e){
//Do something with exception
}
}
然后我使用以下代码发送电子邮件:
//Prepare email
$mail = new Zend_Mail();
$mail->addTo($email);
$mail->setSubject($subject);
$mail->setBody($message);
$mail->setFrom('username@gmail.com', 'User Name');
//Send it!
$sent = true;
try {
$mail->send();
} catch (Exception $e){
$sent = false;
}
//Do stuff (display error message, log it, redirect user, etc)
if($sent){
//Mail was sent successfully.
} else {
//Mail failed to send.
}
关于php - 使用 Zend Framework 和 PHP 发送电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2278095/