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

发布于 2024-08-21 19:20:51 字数 894 浏览 8 评论 0原文

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

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

这是我的控制器的示例:

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');
}

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');
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

无语# 2024-08-28 19:20:51

我建议您使用 Zend_Mail 而不是 mail()。它会自动处理很多事情,而且效果很好。

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

这是我使用 Zend_Mail 和 Gmail 发送邮件的方法:

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
    }
}

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

//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.
}

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

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

This is what I use for sending mails using Zend_Mail and Gmail:

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

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.
}
安人多梦 2024-08-28 19:20:51

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

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.

羞稚 2024-08-28 19:20:51

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

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

韬韬不绝 2024-08-28 19:20:51

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文