php - 使用 Zend Framework 和 PHP 发送电子邮件

标签 php email zend-framework send

我正在处理一个表单,当用户输入他们的电子邮件帐户并点击发送时,一封电子邮件将发送到他们的电子邮件帐户。

我已经解决了所有问题。只是它不会将电子邮件发送到我的帐户。谁有想法?是否有我遗漏的配置?

这是来 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/

相关文章:

php - 来自 Symfony 命令的 Swift 邮件

java - 如何在 Outlook 中创建电子邮件并使其对用户可见

javascript - 从 JS 函数调用 NodeJS 函数

iphone - 我如何从 iphone 设备获取电子邮件历史记录..?

php - 如果电子邮件已发送,则从邮件服务器获取响应

php - 使用Trie在mysql+PHP中搜索大数据

php - TYPO3 v10LTS - 错误 - 核心 : Exception handler (WEB): Uncaught TYPO3 Exception: #1573385431: RSS URL could not be fetched | RuntimeException

php - libCURL::SSL:证书主题名称 'Common Name (eg, YOUR name)' 与目标主机名不匹配...但 CN 和目标名称匹配

php - Zend db PDO 不返回结果

zend-framework - 如何使用 Zend_Form->createElement()