Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics

Dashboard
Notifications
Mark all as read
Q&A

PHP emails are sent when $message is a string, but not when its an array

+3
−0

I host my website on a CentOS-Bash, PHP and MySQL environment; my local email client is engined by Roundcube. I don't know almost anything about PHP nuances.

While my web domain registration is done by one company, my DNS records hosting, websites hosting and website emails hosting are all done by a second company.


I can send emails to myself (to my local email client) via running in terminal php example_1.php with:

<?php
    $to = "[email protected]";
    $subject = "Email Test";
    $message = "PHP's mail function test";

    mail($to, $subject, $message);
?>

I can't send emails to myself (to my local email client) via running in terminal php example_2.php with:

<?php
    $to = "[email protected]";
    $subject = "New email message";
    $message = array (
            $name = $_POST["name"] . "Name:" . "\r\n",
            $email = $_POST["email"] . "Email:" . "\r\n",
            $phone = $_POST["phone"] . "Phone:" . "\r\n",
            $topic = $_POST["topic"] . "Topic:" . "\r\n",
            $date = $_POST["date"] . "Date:" . "\r\n",
            $time = $_POST["time"] . "Time:" . "\r\n",
            $notes = $_POST["notes"] . "Notes:" . "\r\n"
    );

    mail($to, $subject, $message);
?>

What might cause messages by the second example pattern not to reach my email box?

Why does this post require moderator attention?
You might want to add some details to your flag.
Why should this post be closed?

0 comments

2 answers

+3
−0

As explained by manassehkatz, the message (aka the body) is a text in PHP (and many other programming languages).

If your e-mails have a certain structure, you should create a function that takes that structure (i.e. your array) and generate a string from it. This is a good idea also from an architectural point of view because separating body construction from the actual e-mail sending functionality is preferred.

Also, you should take care of the message format (plain text vs. HTML) as it might involve some changes (e.g. using <p>s or <br>s instead of newlines).

Why does this post require moderator attention?
You might want to add some details to your flag.

2 comments

Thank you Alexei, about your last suggestion about plain text vs. HTML ; did you mean to an header such as header("Content-Type: text/html; charset=UTF-8");? JohnDoea‭ 16 days ago

@JohnDoea Yes, I think that is the way to define the message content type. Alexei‭ 15 days ago

+3
−2

From the PHP mail manual

mail ( string $to , string $subject , string $message , array|string $additional_headers = [] , string $additional_params = "" ) : bool

PHP manual syntax (typical for many languages) lists the type of each function parameter prior to the name of the parameter. Since parameters are normally passed as $variables, that syntax is used for the names, though you can use any variable names that you want, and for most (there are some occasional exceptions) PHP function parameters you can use constants as well - e.g., a "quoted string" in place of a string.

Specifically, if a parameter is listed with a single type such as string then the parameter can only be of that type. Due to the way PHP works, using an incorrect type can result in any of:

  • Automatic "useful" conversion - e.g., numeric to string
  • Warning message
  • Error message
  • Conversion to some value that is not useful in the particular situation

If multiple types are listed, such as array|string, then each of those types is possible and they will each be handled appropriately. In the example of headers, you can either pass an array of strings or a string containing a single header or a string with multiple header strings separated by newline characters.

An interesting twist to the usual type insensitivity of PHP is the implode function which historically has allowed:

implode ( string $separator , array $array ) : string

or

implode ( array $array , string $separator ) : string

where the parameter sequence was determined by type, since the function requires exactly one array and exactly one string.

Back to the problem at hand:

The message is always, and only, a string. That string can be anything from one blob of plain text to multiple sections that include HTML, plain text and attachments. More elaborate functions take all those other parts and turn them into one giant string.

The additional_headers can be a string or an array. That is due to the way headers work. They are sent as a string, but they are processed as key/value pairs, each of which is handled as a string. So an array of strings works for headers. It doesn't work for messages.

In your example, just put it all together as one string.

$message = "Name: " . $_POST["name"] . "\r\n"
     . "Email: " . $_POST["email"] . "\r\n"
     . "Phone: " . $_POST["phone"] . "\r\n"
     . "Topic: " . $_POST["topic"] . "\r\n"
     . "Date: " . $_POST["date"] . "\r\n"
     . "Time: " . $_POST["time"] . "\r\n"
     . "Notes: " . $_POST["notes"] . "\r\n";
Why does this post require moderator attention?
You might want to add some details to your flag.

2 comments

As someone who generally doesn't work with PHP I already read in that manual page and didn't figure anything like "The message is always, and only, a string"; perhaps figuring it requires some specific background knowledge in programming that until reading the essential part of your answer I lacked; perhaps it's some kind of an "oral tradition" how to understand it which I also lacked ; the bottom line, I think "RTFM" isn't suitable here and brings bad energies to the discourse. JohnDoea‭ 16 days ago

Point made (and Alexei edited) about RTFM - it is an extremely standard acronym among programmers, but I can see the concern. The key is the actual argument list - I will elaborate. manassehkatz‭ 10 days ago

Sign up to answer this question »