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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2278095/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 05:50:22  来源:igfitidea点击:

Sending email using Zend Framework and PHP

phpemailzend-frameworksend

提问by Ryan

I working on a form whereby when the user enter in their email account and click on send, an email will be sent to their email account.

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

I have everything worked out. Just that it doesnt send the email to my account. Anyone have any ideas? Is there a configuration that I left out or something?

我已经一切都解决了。只是它不会将电子邮件发送到我的帐户。谁有想法?是否有我遗漏的配置或其他什么?

This is the sample from my 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');
}

回答by Matt Way

I recommend you use Zend_Mailinstead of mail(). It handles a lot of stuff automatically and just works great.

我建议您使用Zend_Mail而不是mail(). 它可以自动处理很多东西,而且效果很好。

Do you have a SMTP server? Trying to send mail without your own SMTP server could be causing the mail to not be sent.

你有 SMTP 服务器吗?尝试在没有您自己的 SMTP 服务器的情况下发送邮件可能会导致邮件无法发送。

This is what I use for sending mails using Zend_Mailand Gmail:

这是我用于使用Zend_MailGmail发送邮件的内容:

In Bootstrap.php, I configure a default mail transport:

在 中Bootstrap.php,我配置了默认邮件传输:

protected function _initMail()
{
    try {
        $config = array(
            'auth' => 'login',
            'username' => '[email protected]',
            '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
    }
}

Then to send an email I use the following code:

然后发送电子邮件我使用以下代码:

//Prepare email
$mail = new Zend_Mail();
$mail->addTo($email);
$mail->setSubject($subject);
$mail->setBody($message);
$mail->setFrom('[email protected]', '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.
}

回答by prodigitalson

First of all i would switch to using Zend_Mail. Second i would use a real mail account on an smtp server somewhere and send from that. A lot of times there are restrictions on sending from the server itself, but using an actual mail server usually fixes this.

首先,我会改用 Zend_Mail。其次,我会在某处的 smtp 服务器上使用真实的邮件帐户并从中发送。很多时候从服务器本身发送有限制,但使用实际的邮件服务器通常可以解决这个问题。

回答by Gray Fox

There's a very useful screencast covering Zend_Mail available on ZendCasts http://www.zendcasts.com/introduction-to-zend_mail/2010/02/

ZendCasts http://www.zendcasts.com/introduction-to-zend_mail/2010/02/上有一个非常有用的截屏视频,涵盖 Zend_Mail

回答by Hayk Manasyan

In line $mail->setBody($message);, change it to $mail->setBodyText($message);

在行中$mail->setBody($message);,将其更改为$mail->setBodyText($message);