MODx emails not working - php

On my server, sending emails with MODx (version 2.2) fails. It would probably work with SMTP, but in principle it should work without SMTP as well, since scripts like the one below work.
Script:
<?php
$to = "someone#example.com";
$subject = "Test mail";
$message = "Hello! This is a simple email message.";
$from = "someonelse#example.com";
$headers = "From:" . $from;
mail($to,$subject,$message,$headers);
echo "Mail Sent.";
?>
Can you help me find the reason for this misbehaviour and/or solve this problem?
Edit
To answer the questions below:
I don't try to send any fancy emails to site users. I am just trying very basic stuff like resetting login and passwords via email.
I tried quickemail for debugging. The output is:
System Settings (used if property is missing):
emailsender System Setting: xxx#xxx.xx
site_name System Setting: MODX Revolution
Properties (from parameters, property set, or snippet default properties:
Tpl chunk name:
subject:
to:
fromName:
replyTo:
emailSender:
allowHtml: 1
message:
Final Values (actually used when sending email):
subject: Default Subject
to: xxx#xxx.xx
fromName: xxx#xxx.xx
replyTo: xxx#xxx.xx
emailSender: xxx#xxx.xx
allowHtml: 1
Message Body: Default Message
Send Failed
Mailer error info: Could not instantiate mail function.
Server Debug Information:
I think I already do use MOD-Mail with the core installation. Isn't that right?
My mailserver uses qmail. I just tried the script above without a $from value, and it worked. Buts still, MODx cannot send emails.

You will need to check that the default settings used by MODx match those of your servers PHP mail() configuration.
The PHP mail class MODx uses is at core/model/modx/mail/phpmailer/class.phpmailer.php. If they do not match I do not recommend changing the class, instead use SMTP which can be enabled and configured within the MODx Manager under System > System Settings
Info: By default MODx sends email using /usr/sbin/sendmail -oi -f. It is pretty common but wont work on all web servers.

When you are using the mail-function, you are using the native (standard) PHP mail-class.
Modx revo has it's own, but you are not using the class at all. This is installed by default so it should be working. Read this wiki-article for usage of the class: http://rtfm.modx.com/display/revolution20/modMail
However, if sending email does not work, this has nothing to do with Modx, and as far as I can see, your code is valid. This is rather a server/PHP-setup question.

Related

Why won't I send an email thru Wordpress when I can send it thru a PHP script?

I've migrated a Wordpress server running version 4.9.8 from Ubuntu to CentOS and emails from Wordpress don't seem to be sending properly. I'm testing sending emails by creating new users associated with various email addresses.
I've installed Postfix and configured it to match as closely as possible with the original configuration. I say this because there are some slight differences in how Postfix is implemented.
I'm able to send emails to various addresses through the command-line and thru a PHP script hosted on the same server (php-fpm) as Wordpress.
However, when I try to send emails (by creating a new user in wp-admin) to the same addresses in Wordpress they don't reach the recipient. I've checked the Wordpress configuration files and I'm not seeing anything overriding the default mail operations. There are no new entries in /var/log/maillog when email is triggered via my PHP script:
<?php
// the message
$msg = "First line of text\nSecond line of text";
// use wordwrap() if lines are longer than 70 characters
$msg = wordwrap($msg,70);
// send email
mail("me#{mydomain}","My subject",$msg);
?>
There are also no entries after triggering an email from Wordpress.
EDIT: I turned on debugging to a log file and added a function to catch wp_mail errors. This is the error that I'm seeing when an attempt is made:
(
[wp_mail_failed] => Array
(
[0] => Invalid address: (setFrom) wordpress#{mydomain}
)
)
The From email address is the same as it was in original server.
EDIT2:
I've added some more custom code to functions.php to set the sender and I've tried a couple of different sender email addresses - both of which are valid email addresses. All of them are failing with this "invalid address" error. What's weird is PHP's mail function sends the email as wordpress#{mydomain} and that works fine.
My initial solution was to change class-phpmaiiler.php directly to 'noregex' but thanks to #diondesigns on the Wordpress forums I found out that I could add a line to my theme's functions.php:
add_filter( 'wp_mail_from', function($from){PHPMailer::$validator = 'noregex'; return $from;} );
Now emails send fine.
UPDATE:
I wanted to add that the main issue here was that PHP was compiled with PCRE2 - not PCRE. That broke the validateAddress in PHPMailer. After moving to a version of PHP with PCRE compiled I am no longer seeing the issue. I recommend this path over the workaround.

Symfony 1.4: email configuration

I'm trying to simply send an email from my server running Symfony 1.4.
I have the usual basic mail server setup, with the server name mail.tixxit.com.au and port 25. I have this configuration in my factories.xml:
mailer:
param:
transport:
class: Swift_SmtpTransport
param:
host: mail.tixxit.com.au
port: 25
encryption: ssl # Not sure on this but have tried "tls" too
username: myaccount#tixxit.com.au
password: mypassword
Then I have a basic bit of send code in one of my actions:
$this->getMailer()->composeAndSend(
"myaccount#tixxit.com.au",
"anotheraccount#gmail.com",
"Message title",
"Message content"
);
From my what I've seen mentioned in other documentation, this should be all I need to do to send email from my server using Symfony in PHP, but this is not working. The send code does not give any errors, I can hit the mail.tixxit.com.au server from my web server fine, my credentials are definitely correct, and I have confirmed with support from my email hosting company that this should work and no configuration is required on their side to allow this.
But my mail isn't being sent. I have tried a whole lot of different settings in factories.xml but nothing works. It appears to send, but nothing ever arrives at anotheraccount#gmail.com. I have tried this on my local machine and my web server and get the same result.
What am I missing here? What Symfony/PHP/server/mail account settings do I need to actually make this work? Is there some fundamental other configuration that I am supposed to have set before the Symfony stuff that will allow me to send?
I couldn't find an answer to this. Nothing seemed to work. I gave up and used the natural Swift classes e.g.:
$transport = Swift_SmtpTransport::newInstance('mail.tixxit.com.au', 25)
->setUsername('myaccount#tixxit.com.au')->setPassword('mypassword');
$mailer = Swift_Mailer::newInstance($transport);
$message = Swift_Message::newInstance("Message title")
->setFrom(array('myaccount#tixxit.com.au' => 'App'))
->setTo(array('anotheraccount#gmail.com'));
$message->setBody("Message content.");
if ($mailer->send($message, $errors)) {
$success = true;
}

php mail() giving me a "no disk" error

Hey I am trying to send an email from a php script. When I try to do so, I get a pop up box that is titled "mailtodisk.exe - No Disk". In the body of the error message, it says, "There is no disk in the drive. Please insert a disk into drive \Device\Harddisk1\DR1".
I have tried to figure this out but to no avail. I am doing this from localhost.
Here is my script that is supposed to send the email:
<?php
$to = $_POST['email1'];
$subject = "Test mail";
$message = "I just sent you an email!";
$from = "ULSRL#louisiana.edu";
$headers = "From:" . $from;
if( mail($to, $subject, $message, $headers) )
{
echo ("Mail Sent.");
}
else
{
echo ("Mail could not be sent!");
}
?>
Any help is greatly appreciated! Thanks :)
I am guessing you are using XAMPP?
If so you will need to modify the php.ini located in your XAMPP installion, look for the following lines:
; XAMPP: Comment out this if you want to work with fakemail for forwarding to your mailbox (sendmail.exe in the sendmail folder)
;sendmail_path = "\"C:\xampp\sendmail\sendmail.exe\" -t"
; XAMPP: Comment out this if you want to work with mailToDisk, It writes all mails in the C:\xampp\mailoutput folder
sendmail_path = "C:\mailtodisk\mailtodisk.exe"
mail cannot be delivered in PHP even SMTP server is running and PHP mail() returns true
This should also help:
http://blog.joergboesche.de/xampp-sendmail-php-mailversand-fuer-windows-konfigurieren#xampp_180_sendmail
You are trying this solution in Localhost. So you can get this error. Try in live work. You will definitely succeed.
For XAMPP, I have the same message it shows about 4 times providing options
{cancel}, {try again}, {continue}
No matter what I choose it replied 4 times and instead of showing the next page it showed the blank page.
My XAMPP version saves emails to the C:\xampp\mailoutput and this part was working.
What I did was: I commented out the following line is PHP.ini
;sendmail_path="C:\xampp\mailtodisk\mailtodisk.exe"
Now it does not save the files in C:\xampp\mailoutput
BUT it does not give me this annoying error and the next page loads fine.
Root cause of the problem is that mailtodisk.exe tries to reach non existing drive on your Windows machine, which cause this annoying message. This can be considered as a bug in mailtodisk actually, so you can simply don't use it as other replies here suggest.
This problem is caused usually because of USB sockets attached to your computer, for example for reading SD cards, which are currently empty.
If you prefer walking on the wild side, you can tweak your registry and suppress this message. See video demonstration here https://www.youtube.com/watch?v=Aj7-pLaAq2c

php redirect email to file

I am using php5.
Are there some settings or a simple php.ini directive that would redirect all the emails to a folder?
I want on the development machine to have all the emails generated by the system not sent to the actual receiver but put in a folder.
Thanks.
I used to have some code like this (kinda pseudocode):
define ('DEBUG', true);
function send_email($to, $subject, $body) {
if (DEBUG) {
file_put_contents('some_folder/' . $to . date('dmY-His') . '.html', $body);
}else{
// Actual code to send email
}
}
But i agree with others, it's easier/better to setup an development email account to receive those emails.
I don't think you will be able to do something like this. Mails are sent by a mail server so it must be your mail server that writes them to a file instead of sending them.
Why not simply send it to a special development email?
Sample:
define('DEBUG', true);
if(DEBUG)
{
// Override recipient
$recipient = 'development#domain.tld';
}
// Send mail...
No settings that I'm aware of in PHP itself. However, if you're using Postfix on your development server, here's a recipe I cooked up to redirect all outbound email to a single (local) address:
/etc/postfix/main.cf: (add this to the existing file, don't replace everything)
virtual_alias_maps = regexp:/etc/postfix/virtual
/etc/postfix/virtual:
/.*/ duskwuff#localhost
You can configure your mail server to accept SMTP messages as normal, but make it unable to forward them onto another mail server. If your mail server supports it, make it redirect all messages to a postmaster account, or any other address of your choice.
This means that PHP will behave as normal, everything will appear to work straight away with the message, but it just won't go to the 'intended' recipient.
It also means that you can inspect headers (pretty much as they would normally appear), to support debugging.
There are many ways to do this. Basically, you need to define the sendmail command in your php.ini to point to a program or script which will save the mail locally.
One solution is this:
Catch emails with php script
Another is this:
Mail catcher

cannot initiate mail function in joomla

I am getting this error in Joomla while sending the mail.
I am not getting this error every time. But some times its shows me "cannot initiate mail function".
Any solution for this?
That could be any number of things, but a general list of things to check would be first, your Joomla config:
Admin panel > Global Configuration > Server > Mail Settings > Mailer
Make sure that's set to use the PHP mail function. If it is, try making a script called test.php and putting it in the root of your site (where the index.php file is for Joomla). Make that file something like this:
<?php
$to = "you#youremailaddress.com";
if( mail( $to , 'This is a test message.' , 'Is this working?' ) ) {
echo 'Email sent.';
} else {
echo 'Email failed to send.';
}
?>
Make sure you change the $to = line to your email address.
Now go to that script: http://www.yourjoomlasite.com/test.php
You should see the text 'Email sent.' in your browser and then receive an email to that address you entered. If not, then you should contact your hosting provider and ask them to upgrade to the latest PHP version and/or resolve the mail() function issue for you. That is the most raw implementation of sending mail via PHP and if that fails to work then it's got to be an issue with your host.
If you are working in the local server, the mail will not initiate. So host your website in server and try it.
If you have any questions, let me know
If you are using WAMP or similar on your own machine and are getting this message then you need to install a mail server. This is the best solution on Windows.
http://www.toolheap.com/test-mail-server-tool/

Categories