Multiple recipients with mail() in $to or $additinional_headers? - php

When I use mail() I can specify the recipients in the parameter $to or in the $additional_headers. I think $to cannot be empty, so there must be at least one recipient. Is that correct? If further recipients should be added, does it make a difference if it is appended to $to or if is set with "To: " entries in $additional_headers?

You need to use below code to send an email for multiple recipients:
$email_to = "abct#another.com,some#other.com,yet#another.net";
If you need to add emails as CC or BCC, add the following part in the variable you use as for your header :
$headers .= "CC: sombodyelse#noplace.com".PHP_EOL;
$headers .= "BCC: hidden#special.com".PHP_EOL;

Related

php html mail not working on cpanel server [duplicate]

This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 5 years ago.
I have a code to send html mail with php on cpanel but doesn't send without any errors. I have tested plain text mail and that is working fine, but once I add the header like below, it doesn't work.
//send html mail
ini_set('sendmail_from', 'info#toludimaokojie.com');
$headers = "From: info#toludimaokojie.com\r\n"."X-Mailer: php";
$headers .= "Content-type: text/html\r\n";
$html = '<html>
<body>
<h2>Result Analysis for Test With Reference Number:'.$reference.'</h2>
<br/>
<p><b>Personality: '.$personality_mail.'</b></p>
<br/>
<p><b>MBQ Score: '.$resultTotal.'</b></p>
<br/>
<ul>'.$analyseData.'</ul>
<pre>Mail Sent on '.date("l, F Y H:i:sa").'</pre>
</body>
</html>';
mail("olaegbesamuel#gmail.com", "MBQ TEST ANALYSIS", $html, $headers);
This doesn't work. Please help, i guess am doing everything correctly here. I have tested without html and confirmed that mail is working
You have defined $headers with all mandatory email parameters but look at your $header variable, there are two things need to be changed,
1) You have used $header and $headers both so use either of them and append them to one variable.
2) your 2nd line of $header variable is missing . to append the previous header values, so the corrected code should be:
$headers = "From: info#toludimaokojie.com\r\n"."X-Mailer: php";
$headers .= "From: $from\r\n";
$headers .= "Content-type: text/html\r\n";
As you missed . operator in 2nd line, it will remove From header and so it would not be sending email.
Try after changing these lines.

Mail Multiple users with php

I normally mail single user on php but what I dont know if its possible to fetch all emails from the users table and bundle it in one variable ($to) and then mail it together with
mail($to, $subject, $message, $headers);
Can anyone help me with the syntax on how to mail multiple users? I am new to php and mysqli.
Thanks.
PHP send mail to multiple email addresses
$recipients = array(
"youremailaddress#yourdomain.com",
// more emails
);
$email_to = implode(',', $recipients); // your email address
$email_subject = "Contact Form Message"; // email subject line
$thankyou = "thankyou.htm"; // thank you page
mail($email_to, $email_subject, $thankyou);
Note that you can use a custom message in place of $thankyou if you just want a standard message instead of html.

PHP: Line Breaks on email sending not working

The following PHP code works perfectly, but it is not doing line breaks for some reason.
PHP:
$headers = "MIME-Version: 1.0\n";
$headers .= "Content-type: text/html; charset=utf-8\n";
$headers .= "From: '".$title."' <".$store_email."> \n";
$subject = "New Payment Received";
//MESSAGE
$message = "New payment was successfully recieved through paypal payment terminal:";
$message .= "\r\n\nFrom ".$paypal->pp_data['payer_email'];
$message .= "\r\nPaid: ".$paypal->pp_data['payment_gross']." ".$paypal->pp_data['mc_currency'];
$message .= "\r\nDate: ".date('d/m/Y');
$message .= "\r\nTime: ".date('g:i A');
mail($admin_email,$subject,$message,$headers);
Any wonder what's wrong? Thanks in advance.
You're sending HTML e-mail. Line breaks have no meaning in HTML, you'll need <br /> tags.
The direct answer ceejayoz gives is correct and to the point in that the html element <br> is needed because it is a html email.
The bigger issue is that not all email is readable in html (example: user doesn't allow html emails). Anyone sending email should send it in 2 parts. One being a html formatted message and the other "alternative" in plain text. In that way the recipient will be able to read the email regardless of email reader.
The \r\n line break works in plain text alternative part and in html<br> or other elements as needed to format.
Doing this will avoid the next question. Recipients are complaining my emails are blank.

How to make from-name in email appear with a space using PHP?

I am currently sending text email in PHP and doing something like this:
$from = "from: Hike Attendance Update#comehike.com";
$to_email_address = 'some email';
$subject = 'Some subject';
$contents = 'Blah';
mail($to_email_address, $subject, $contents, $from);
When I send it, it appears as sent from Hike.Attendance.Update which doesn't look very good. How can I make it appear with spaces instead of dots?
Thanks,
Alex
Try:
$from = "from: \"Hike Attendance\" <Update#comehike.com>";
I think that's specified in some RFC for email, namely RFC 5322.
When specifying the actual name, put the email address in <> chars:
$from = "from: Hike Attendance <Update#comehike.com>";

Additional text in the email body

I'm building a simple order system and want to send an email after the form is submitted. My PHP code looks similar to this:
$name=$_POST["orderName"];
$company=$_POST["orderCompany"];
$email=$_POST["orderEmail"];
$phone=$_POST["orderPhone"];
$headers = "From: $email\r\n" .
$item1=$_POST["orderItem1"];
$qty1=$_POST["orderQty1"];
$item2=$_POST["orderItem2"];
$qty2=$_POST["orderQty2"];
$item3=$_POST["orderItem3"];
$qty3=$_POST["orderQty3"];
$date = date("l, F j Y, G:i") ;
$message="Message sent: $date \n\n
Name: $name\n
Company: $company\n
Email: $email\n
Phone: $phone\n\n
Order:\n
$item1 \tx$qty1\n
$item2 \tx$qty2\n
$item3 \tx$qty3\n";
mail("sales#company.com", "Order", $message, $headers);
That works fine, except in the body of the email I get the value of $item1 string at the very beginning, before the "Message sent..." - just like I added it to the $message (which I don't as far as I can see).
Where you have this:
$headers = "From: $email\r\n" .
you want this instead:
$headers = "From: $email\r\n";
Otherwise, you're concatenating whatever comes on the next line (which happens to be the definition for $item1) to the end of $headers. Although that's not technically valid (i.e., the content is part of the message headers and not body), most e-mail clients will effectively shrug and show it anyway.
Please, please, please add some sanitizing to your POST variables before going with this in production.
Let's see here:
$email=$_POST["orderEmail"];
$headers = "From: $email\r\n";
mail("sales#company.com", "Order", $message, $headers);
I could send a POST request where "orderEmail" contains:
"helo#helo.lv\r\n
From: viagra#farmacety.net\r\n
BCC: victim1#domain1.com, victom2#domain3.com"
etc. and your harmless form would work great for me sending spam to the whole world. This site suggects:
if ( ereg( "[\r\n]", $name ) || ereg( "[\r\n]", $email ) ) {
[... direct user to an error page and quit ...]
}

Categories