tutorial.html 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. <p>
  2. This snippet is for use with MODX Revolution. QuickEmail can be used to send email from inside another snippet, but its primary use is to see if your email is working, and if not, to help diagnose and solve email problems.
  3. </p>
  4. <h3>Installing QuickEmail for MODX Revolution</h3>
  5. <p>
  6. Go to System | Package Management on the main menu in the MODX Manager and click on the "Download Extras" button. That will take you to the Revolution Repository (AKA Web Transport Facility). Put QuickEmail in the search box and press Enter. Click on the "Download" button, and when it changes to "Downloaded," click on the "Finish" button. That should bring you back to your Package Management grid. Right click on QuickEmail and select "Install Package." The QuickEmail snippet should now be installed.</p>
  7. <h3>Using QuickEmail</h3>
  8. <p>
  9. This couldn't be much simpler. Create a document in the Manager and put the following code in the Resource Content field:
  10. </p>
  11. <pre><fixedpre>
  12. [[!QuickEmail]]
  13. </fixedpre></pre>
  14. <p>
  15. When you preview the document, you should receive an email at the address you specified when you installed MODX. You can go to System | System Settings the top menu and put emailsender
  16. in the search filter to check that address. If you don't receive an email, Change the snippet tag to look like this:
  17. </p>
  18. <pre><fixedpre>
  19. [[!QuickEmail? &debug=`1`]]
  20. </fixedpre></pre>
  21. </p>
  22. <p>
  23. Try previewing the document again and look for an error message near the bottom of the screen.
  24. </p>
  25. <p>
  26. <h3>Diagnosing Email Problems</h3>
  27. <p>If you see a message that says, "Could not instantiate mail function," it usually means that either your host has the mail() function turned off (many do) or that you
  28. are using a localhost install and there is no working mail server. In that case, one option is to use the MODX SMTP Systems Settings so that MODX will send the mail from an
  29. SMTP server (e.g., Gmail) where you have an account.</p>
  30. <p>Go to System | System Settings and type SMTP in the search filter, then click on the grid. Set the SMTP settings to the appropriate values for your SMTP account. The following settings will work for Gmail:</p>
  31. <pre><fixedpre>
  32. SMTP Authentication: Yes
  33. SMTP Connection Prefix: tls
  34. SMTP Hosts: smtp.gmail.com
  35. SMTP Password: yourGmailPassword
  36. SMTP Port: 465
  37. SMTP User: yourGmailUsername
  38. Use SMTP: Yes
  39. </fixedpre></pre>
  40. <p>Note that Gmail will change all the email from addresses to match your Gmail account and any settings you use in the tag will be ignored. You can have a separate account in Gmail for sending email from your site if you wish.</p>
  41. <p>
  42. Once you have set the SMTP settings, try previewing your page again. If there are SMTP errors, you will see a detailed account of the Server interaction. Look for any errors and correct the SMTP settings accordingly. A timeout usually means a problem with the port number or failure to set the SMTP prefix. Failure to connect to the host, usually means the SMTP hosts is incorrect or the SMTP prefix is not set correctly. If you've forgotten to set the SMTP authentication, it will remind you. If there is a problem with authentication, either the SMTP username or the SMTP password are incorrect &mdash; there's no way to tell which one is the problem.
  43. </p>
  44. <h3>Sending Email From Inside Another Snippet</h3>
  45. <p>
  46. You can use the following code to send email from inside another snippet, although it is just as easy to use the MODX mailer. The MODX mailer is also more efficient,
  47. because QuickEmail will do a bunch of processing and then call the MODX mailer. Don't try any of the code below until you've made sure your mail system is working using the method described above.
  48. </p>
  49. <pre><fixedpre>
  50. $props = array(
  51. 'debug' => '0',
  52. 'hideOutput' =&gt; '0',
  53. 'message' =&gt; 'Some Message',
  54. 'subject' =&gt; 'Some Subject',
  55. 'to' =&gt; 'someone@somewhere.com',
  56. 'fromName' =&gt; 'Some Name',
  57. 'emailSender' =&gt; 'someone@somewhere.com',
  58. 'replyTo' =&gt; 'someone@somewhere.com',
  59. 'html' =&gt; '1',
  60. 'failureMessage' =&gt; '&lt;br /&gt;&lt;h3 style=&quot;color:red&quot;&gt;Mail Failed&lt;/h3&gt;',
  61. 'successMessage' =&gt; '&lt;br /&gt;&lt;h3 style =&quot;color:green&quot;&gt;Mail reported successful&lt;/h3&gt;',
  62. 'errorHeader' =&gt; '&lt;br /&gt;Mail error:',
  63. 'smtpErrorHeader' =&gt; '&lt;br /&gt;SMTP server report:',
  64. );
  65. $output = $modx->runSnippet('QuickEmail',$props);
  66. return $output;
  67. </fixedpre></pre>
  68. <p>
  69. Once you have things working, you may want to set 'hideOutput' to '1'. As an alternative, you can modify the $output
  70. variable before returning it. If 'hideOutput' is set to '1', there will be no visible output at all from the snippet &mdash; it will just
  71. send the email.
  72. </p>
  73. <p>
  74. All of the properties are optional and have reasonable defaults so try
  75. just setting 'message', 'subject', and 'to', and see what you get.
  76. </p>
  77. <h3>Using The MODX Mailer to send email</h3>
  78. <p>Since I mentioned it above, here is how you send email using the MODX built-in email function:</p>
  79. <pre><fixedpre>
  80. $modx->getService('mail', 'mail.modPHPMailer');
  81. $modx->mail->set(modMail::MAIL_BODY, 'Some message');
  82. $modx->mail->set(modMail::MAIL_FROM, 'someone@somewhere.com');
  83. $modx->mail->set(modMail::MAIL_FROM_NAME, 'Your Name');
  84. $modx->mail->set(modMail::MAIL_SENDER, 'you@yourdomain.com');
  85. $modx->mail->set(modMail::MAIL_SUBJECT, 'Some Subject');
  86. $modx->mail->address('to', 'somone@somewhere.com', 'UserName');
  87. $modx->mail->address('reply-to', 'you@yourdomain.com');
  88. $modx->mail->setHTML(true);
  89. $sent = $modx->mail->send();
  90. if ($sent) {
  91. $output = 'Mail Sent';
  92. } else {
  93. $output = 'Mail Not Sent';
  94. }
  95. return $output;
  96. </fixedpre></pre>
  97. <p>&nbsp;</p>