But the actual email ‘From’ header doesn’t have the email address, only the name:
If I remove the string from the $mh->from($email_from); the header includes the email address. The replyTo does the same thing: it has an email only without the name.
Has anyone tried sending emails programmatically in v9 yet? That code worked in v8. Now I don’t get any emails although the log says they get sent.
Could anyone check the Mail Service function? I don’t understand how it works. Here’s the PHP online console: PHP Tryit Editor v1.2
The code is from concrete/src/Mail/Service.php generateEmailStrings()
First it sets a ‘from’ property array with a string of email addresses and a name in from($email, $name = null).
Second it sends email with sendMail() where it sets the $fromStr = $this->generateEmailStrings([$this->from]);.
Third the generateEmailStrings makes up a string combining email+name. So I’ve pasted that code in the PHP console and it shows it does some weird stuff which I can’t understand. Hope I’m wrong. Hope you can prove or disprove that.
When $i is 0, i.e. $arr[0], the $v is the first array element, i.e. the email address. When $i is 1, i.e. $arr[1], the $v is the second array element, i.e. the name.
Now see the rest of the loop where it uses $v[0] and $v[1] - these are the 1st and 2nd letters of the email and name strings, NOT elements of the array.
So with $i = 0, $v[0] = ‘e’, $v[1] = ‘m’. With $i = 1, $v[0] = ‘M’, $v[1] = ‘y’.
I think I see where the issue might be. It’s not the loop, the loop and generateEmailStrings are actually fine. As long as the $attr is an array of array. But the $this-> from is a simple array. That’s why I think it breaks.
I think this:
public function from($email, $name = null)
{
$this->from = [$email, $name];
}
should be changed for this:
public function from($email, $name = null)
{
$this->from[] = [$email, $name];
}
I think either the documentation (Working with the Mail-Service) is correct but the code is wrong. Or the documentation is wrong AND the code doesn’t work.
It doesn’t help the code. Regardless of the documentation I just can’t figure out how it could work given my test at the very top. And even sending the array instead of function parameters doesn’t work.
Ok, I created a test (and believe I found a bug in the process). I have tested this with my own email addresses and Gmail (names have been changed…)
$ms = Core::make('mail');
// $ms->setTesting(true);
$ms->setSubject('Testing email.');
$ms->setBody('This is some mail.');
$ms->setBodyHTML('<p>This is some HTML mail.</p>');
// Bug: passing in [email, name] here causes an exception in concrete/vendor/zendframework/zend-mail/src/Address.php :41
// adding $email to the exception message, shows that the constructor think $name is an email address !!!
$ms->from(['j@j.com']); // do NOT pass a name
$ms->replyto('j@gmail.com', 'Website Reply');
$ms->to('s@s.com', 'Somebody S.');
$ms->to('a@a.com, b@b.com');
try {
$ms->sendMail();
}
catch (exception $e) {
echo $e->getMessage();
}
if (!isset($e)) {
echo 'Mail sent.';
} else {
echo 'Mail NOT sent!';
}
There seems to be some confusion in this thread, hopefully this will help.
The mail service works in v9. We have active production sites running v9. If emails didn’t work we’d hear about it.
I just tested the mail service by running a “forgot password” on my local develop branch site. I received an email just fine.
Perhaps there’s something a bit strange with the email logging functionality – LOGGING being the keyword. When you say something about emails not working people are going to be a bit more concerned than when you say the LOGGING functionality isn’t working.
In these cases it’s always good to test using something that you’re pretty sure works, rather than any custom code you’ve developed (which you might be less sure about, since you just wrote it.) We have a “Test Mail Settings” Dashboard page in the System and Settings section of the Dashboard that can be very useful for testing your SMTP settings, and other email related functionality (logging, etc…)
That being said, I see no indication that email logging isn’t working. My forgot password test works just fine and appears in the logs. There is a bit of work being done in the logging to log any email parts that you have, so I’m not exactly sure why from email address appears as just the string in the plain text portion, but I don’t think it’s indicative of anything not working the way it’s supposed to.
When looking at the docs, I think you might be getting confused between the from method and from property. Ignore the property. The property is not what you want. The method is what you want. The method takes a string for the from email address, and an option name to go along with it.
The array approach for things like to() and replyto() is for a very simple reason – you can have multiple recipients of an email and multiple people on the replyto, but you can only have a single “from” email/name on an email. So. don’t use an email when setting the from().