Magentoを使用してメールを送信する方法


16

Magentoにいくつかの入力フィールドを持つフォームを作成しました。ただし、[送信]をクリックしても、Magentoはメールを送信しません。

Magentoで基本的なメールを送信するにはどうすればよいですか?


app / code / local /ディレクトリに別のモジュールを作成する必要がありますか。
ムニ

なぜ英語を母国語とせず、Magentoを使いこなすためのとんでもないタイプミスの前提条件を持った精巧なチュートリアルを書いていないのですか?
スペンサーウィリアムズ

1
ノー@SpencerWilliamsは、「それは牽引理由を持っている」
Ejaz

回答:


35

magentoでメールを送信するシンプルな機能

<?php
    public function sendMailAction() 
    {
        $html="
        put your html content here
        blah blah

        ";
        $mail = Mage::getModel('core/email');
        $mail->setToName('Your Name');
        $mail->setToEmail('Youe Email');
        $mail->setBody('Mail Text / Mail Content');
        $mail->setSubject('Mail Subject');
        $mail->setFromEmail('Sender Mail Id');
        $mail->setFromName("Msg to Show on Subject");
        $mail->setType('html');// You can use Html or text as Mail format
        $mail->setBodyHTML($html);  // your content or message

        try {
            $mail->send();
            Mage::getSingleton('core/session')->addSuccess('Your request has been sent');
            $this->_redirect('');
        }
        catch (Exception $e) {
            Mage::getSingleton('core/session')->addError('Unable to send.');
            $this->_redirect('');
        }
    }
?>

参照


どのディレクトリに上記のコードを配置する必要がありますか?
ムニ

コントローラーファイル、ヘルパー、またはブロックを必要な場所に追加できます。
QaisarSatti

ヘルパーやモデルを作成していなかったので、どのように使用できますか。説明してください
ムニ

その後、あなたのPHTMLファイルでこの機能を追加し、そこからメールを送信するには...
Qaisar Satti

変数を$ mail-> setToName($ name)として使用できますか?
ムニ

5

新しいテンプレートフォーム「トランザクションメール」を作成します。

hello {{var customerName}},
  You received test template. 
Thank you

新しいテンプレートを作成した後、そのIDをメモします

コントローラーアクションを作成する

public function sendEnquiry()
{
$customer = Mage::getSingleton('customer/session')->getCustomer();

$templateId = 8; // Enter you new template ID
$senderName = Mage::getStoreConfig('trans_email/ident_support/name');  //Get Sender Name from Store Email Addresses
$senderEmail = Mage::getStoreConfig('trans_email/ident_support/email');  //Get Sender Email Id from Store Email Addresses
$sender = array('name' => $senderName,
            'email' => $senderEmail);

// Set recepient information
$recepientEmail = $customer->getEmail();
$recepientName = $customer->getName();      

// Get Store ID     
$store = Mage::app()->getStore()->getId();

// Set variables that can be used in email template
$vars = array('customerName' => $customer->getName());  


// Send Transactional Email
Mage::getModel('core/email_template')
    ->sendTransactional($templateId, $sender, $recepientEmail, $recepientName, $vars, $storeId);

Mage::getSingleton('core/session')->addSuccess($this->__('We Will Contact You Very Soon.'));
}

管理者の「トランザクションメール」を使用して簡単なメールを送信できるようになりました。

your_form.phtmlをフォローする

<form action="<?php echo $this->getUrl("your_module_name/index/sendEnquiry")?>" id="discuss" method="post">

//Your form 

</form>

どのディレクトリにコントローラーファイルを保存する必要がある
Mouni

your_module / controller / IndexController.php createクラスと関連する関数にコントローラーを保存できます。
ハーディクビザ

モデルまたはヘルパーを作成する必要がある場合。Magentoが初めてのことを提案してください
Mouni

任意のモジュールコントローラーファイルに新しい関数を追加/作成できる
Hardik Visa

追加する必要がある機能
-Mouni

2

このコードを試して、それに応じて調整してください

$email_template  = Mage::getModel('core/email_template')
    ->loadDefault($template_id);

/* load template by id */
$email_template_variables = array(
    'customer_name' => $customer_name);

$sender_email = 'Info@yourCompany.com';
$sender_name =  'Your Friend at The Company';                          
$email_template->setSenderName($sender_name);
$email_template->setSenderEmail($sender_email); 

$email_template->send(
    $email_to, $customer_name,$email_template_variables
);

1

UTF-8メールの場合:

$mail = new Zend_Mail('utf-8');
$mail->setFrom($senderEmail, $senderName);
$mail->addTo($toEmail, $toName);
$mail->setSubject($subject);
$mail->setBodyHTML($html); // Or plain: $mail->setBodyText($text)
$mail->send();

0

基本(別のphpスクリプトで動作するはずです)。例外なく機能しましたが、メールを受け取りませんでした。そのため、SMTPの設定により多くの時間を費やしました。

// do not forget to include Mage.php before that
Mage::app();
// send email
$mail = Mage::getModel('core/email')
 ->setToEmail('<my email>')
 ->setBody('Body')
 ->setSubject('Subject:'.date("Y-m-d H:i:s"))
 ->setFromEmail('<from email>')
 ->setFromName('Magento Store Admin')
 ->setType('html');

$mail->send(); 

前提条件:

  1. localhostに設定されたMagentoメール設定(システム->構成->システム->メール送信設定

  2. SMTPが機能していることを確認します(確認できるlocalhostで、CentOSにtelnet "yum install telnet"をインストールする必要がある場合があります)

    telnet localhost 25
    MAIL FROM: <put from mail>
    RCPT TO: <put to mail>
    data: 
    Subject: <put your subject>
    <Put body here>
    . 
    QUIT
  3. 動作しない場合は、SMTPを構成します。CentOSで実行されているpostfixがありました

     ps aux | grep posfix

viで設定を編集しました。

     vi /etc/postfix/main.cf

myhostnameを設定するだけでうまくいきました

  1. PHPメール機能を試してください:

    // The message
    $message = "Line 1\r\nLine 2\r\nLine 3";
    // Send
    $headers = 'From: <from mail>' . "\r\n" .
    'Reply-To: <from mail>' . "\r\n" .
    'X-Mailer: PHP/' . phpversion();
    mail('<to mail>', 'My Subject', $message, $headers);    
    echo "<p>php mail sent 3</p>";
  2. 後置では、「mailq」と入力してメールキューを表示できます。

弊社のサイトを使用することにより、あなたは弊社のクッキーポリシーおよびプライバシーポリシーを読み、理解したものとみなされます。
Licensed under cc by-sa 3.0 with attribution required.