PHP send mail to 1 to 10 recepients - php

I am sure I am overlooking something here, all other mail scripts are working fine except this one. I have an HTML form which collects up to 10 email addresses. all I need to do it to iterate through them and if they are filled, send email to the recipient.
$email0 = 'email#email.com';
$email1 = 'some#email1.com';
$email2 = 'some#email2.com';
$email3 = 'some#email3.com';
$email4 = 'some#email4.com';
....... up to 10.
$i=1;
while($i<=10)
{
$temp = 'email'.$i;
if(isset($$temp) && $$temp != '')
{
$subject="some subject";
$body = "email content";
$headers = "From: $email0 \r\n";
$headers .= "Reply-To: $email0 \r\n";
mail($$temp, $subject, $body, $headers);
}
$i++;
}
...OK I just tried a different way. I stuffed all the emails in one array and iterated through it. The same result, no emails received!!!! What am I missing ? :)
$recipients = array($email1,$email2,$email3,$email4,$email5,$email6,$email7,$email8,$email9,$email10);
foreach($recipients as $value)
{
if($value != '')
{
$subject2="some subject";
$body2 = "some content";
$headers2 = "From: $email0 \r\n";
$headers2 .= "Reply-To: $email0 \r\n";
mail($value, $subject2, $body2, $headers2);
}
}
At this point I am going to post the following:
It just so happened that all my previous attempts started to come
into may spam box (several hours latter!). So my suspicion seems to
be confirmed: when on GoDaddy shared hosting do not send more than
one email at the same time in short intervals (with the same content)
or it will end up labeled as spam!
SOLUTION:
As jmbertucci in the posts below suggested, the simplest solution is to use BCC: for all the recipients in one email. Since I had to personalize each email, I noticed that if I include the unique char. into the subject line for each email, it goes through with no problem as well. So my solution is adding the recipients' name into the subject line (which makes all emails unique) but any unique string should do the same (like date() etc.)

Did u try to check if your mail server is properly config or config it like this sample
// Please specify your Mail Server - Example: mail.example.com.
ini_set("SMTP","mail.example.com");
// Please specify an SMTP Number 25 and 8889 are valid SMTP Ports.
ini_set("smtp_port","25");
// Please specify the return address to use
ini_set('sendmail_from', 'example#YourDomain.com')
;
or check you php.ini file for this. heres a sample config.
[mail function]
; For Win32 only.
SMTP = localhost
smtp_port = 25
; For Win32 only.
;sendmail_from = me#example.com
; For Unix only. You may supply arguments as well (default: "sendmail -t -i").

Related

auto reply not working only for gmail accounts using php mail()

I have a contact form on my website which sends an email to my account and an auto-response to the users who fills the form. I could able to send an auto-reply to non-Gmail accounts but not to Gmail accounts, it's not even sent to spam. I want to know is anything missing in the code, or any settings have to be changed, let me know
code is working fine with non-Gmail accounts
<?php
$email_to = 'mailme#example.com'; //your email
$business = 'company name.,'; //business name
//$topic = $_POST['topic'];
$name = $_POST['name'];
$email_from = $_POST['email'];
$subject = $_POST['subject'];
$message = $_POST['message'];
$autoResponse = true; //set to false if you don't want to send an auto reply
$autoResponseSubject = "Demo Contact Form";
$autoResponseMessage = "Hi, thank you for contacting us, we will get back to you soon.";
$autoResponseHeaders = "From: $business <$email_to>\r\n";
$autoResponseHeaders .= "Reply-To: $business <$email_to>\r\n";
$headers = "From: $name <$email_from>\r\n";
$headers .= "Reply-To: $name <$email_from>\r\n";
if(#mail($email_to,$subject, $message, $headers)){
if($autoResponse === true){
mail($email_from, $autoResponseSubject, $autoResponseMessage, $autoResponseHeaders);
}
echo '1';
} else {
echo '0';
}
?>
I am not getting any errors.
Google, Microsoft, and the like, only accept email from mail servers that fulfill a number of requirements. These requirements are changing over time. This has mainly to do with preventing spam.
Things start with SPF, which is rather simple, but the normal site providing documentation has been down since feb 2019. Have a look at Wikipedia instead.
The next thing is DKIM. Without it mail certainly won't been accepted by GMail.
Then there is also DMARC.
After all of this there is still no guarantee that your mail will be accepted. Your IP could be blacklisted.
As you can probably guess by now, running your own mail server is a lot of work. I've stopped doing it years ago. I now use a third party service for this.

How to set the spf or 'sent via' in email header in php

When you receive a email (in Gmail at least), you see the following format at the top: 'John Doe johndoe#gmail.com via servername.com' I am wondering if there is a way to change the 'servername.com' after the "via" while in doing so keeping my defined headers in my PHP code below, any help would be great:
<?php
$to = 'johndoe#gmail.com';
$message = 'blah';
$subject = 'blahblah';
$headers = 'From: test#gmail.com';
mail($to,$subject,$message,$headers);
?>
That's set by your MTA. For Postfix I can tell you it's determined by the myorigin parameter in main.cf.

PHP Mail script problems

This is the first time I've used PHP at all and I'm having trouble implementing a mail form of all things, I can't seem to get this working. Below is my code, I'd be most appreciative if somebody could point me in the right direction in terms of debugging.
<?php
$job_number = $_POST['job_number'];
$completion_time = $_POST['completion_time'];
$email = $_POST['email'];
$formcontent = "From: $email \n \n Job Number: $job_number \n \n Completion Time: $completion_time \n";
$recipient = "data#rak.co.uk";
$subject = "Repeat Order";
$mailheader = "From: $email \r\n";
mail(
$recipient,
$subject,
$formcontent,
$mailheader
)
or die(
"
Error!
"
);
echo( "
<div style='font-size:24px; margin-top: 100px; text-align: center;'>
Thank You!
"
. " - " .
" <a href='home.html' style='color: #1ca03e;'>
Return Home
</a>
</div>
"
);
?>
Thank you,
Cameron
edit: Some more info, the server supports PHP mail scripts as it had one on there before (according to the friend I'm building this for), the error I've had while internal testing is that the mail is being sent but without any of the '$formcontent' content... Only the titles (aka: From:, Job Number:, Completion Time:)
edit edit: if it helps here is a staging server I have it up on at the moment (don't hate me for poor web-design... it's a work in progress) http://temp.fullaf.com/cameron/rak/repeat.html
You can get the swiftmailer package on their site here -> http://swiftmailer.org/
require_once 'swiftmailer/lib/swift_required.php';
function new_mail($subject, $content, $recipients, $from)
{
// Create the message
$message = Swift_Message::newInstance();
// Give the message a subject
$message->setSubject($subject);
// Set the From address with an associative array
$message->setFrom($from);
// Set the To addresses with an associative array
$message->setTo($recipients);
// Give it a body
$message->setBody($content);
$transport = Swift_MailTransport::newInstance();
$mailer = Swift_Mailer::newInstance($transport);
$result = $mailer->send($message);
}
$job_number = $_POST['job_number'];
$completion_time = $_POST['completion_time'];
$email = $_POST['email'];
$message = "From: $email \n \n Job Number: $job_number \n \n Completion Time: $completion_time \n";
$recipients = array('data#rak.co.uk' => 'Your name of choice');
$subject = "Repeat Order";
$from = array($email => 'Name of choice.');
new_mail($subject, $message, $recipients, $from);
I'm currently not in the position where I can access a ftp server to test this specific snippet but try it out. If there are any problems let me know.
Your code is working and sending email without any issues.
Check your email mail Spam box. Some times, the email will go to Spam box.
You are using this "data#rak.co.uk" email address as recipient email. So don't use the same email address for sender email address. Use another email address as sender email.
Make sure, Your email address "data#rak.co.uk" is receiving emails properly, when send email from other email accounts such as yahoo and gmail.
Please make sure, you have setup the mail server properly in your server.

using the PHP mail function

I am using the PHP mail function to send my users emails about new features and updates. I can send an email and message no problem. But I would like to control who the message is from, well more who the Email Client says it is from.
For Example:
<?php
$to = "someone#gmail.com";
$subject = "Test";
$msg = "Hello there!";
$headers = "From: someone#shareit.me";
mail($to,$subject,$msg, $headers);
?>
In the mail client (Ie. Gmail) the user will see:
Someone Test - Hello There
I would like to control the name of the sender via my headers. Is that possible?
So the email will be from someone#shareit.me but the name of the sender will be Shareit.
Thanks
Use the following headers: (link to docs)
$headers = "From: Shareit <someone#shareit.me>";
To use the From attribute inside of headers always remember to keep email addresses inside of these brackets: < and >
So now your email would be: <someone#shareit.me>
and your line would be: $headers = "From: <someone#shareit.me>"
or "From: Shareit <someone#shareit.me>"
$headers = "From: Shareit <someone#shareit.me>";
Try the above code.
Change the name from Shareit to the name of your choice.

Sending multiple CC's and BCCs with PHP PEAR MAIL

I have a project that I am working on at my job and I am using Pear's mailing. I need to use smtp because we need to be able to track everything from our mailserver. And users need to be able to log in before sending a company based email. We cannot use php's mail function fo this.
My problem is that I cant find any documentation on the net for sending CC and Bcc as well as sending multiple BCCs. It is very easy to do with php' mail funciton . All you do is add it to the $header variable like so
$headers .= 'Cc: birthdayarchive#example.com' . "\r\n";
$headers .= 'Bcc: birthdaycheck#example.com' . "\r\n";
This is my code for the php function where I use PEAR
function sender_mail($email,$subject,$mailmsg, $cc, $bcc){
include("Mail.php");
/* mail setup recipients, subject etc */
//DEFAULT VALUE OF FROM
$from = "noreply#addata.net";
//GET EMAIL OF USER
$result = mysql_query("SELECT email, email_pass FROM u_perinfo WHERE user_id = '$_SESSION[uid]'")
or die("There was an error when grabbing your email information");
if(mysql_num_rows($result) > 0){
$row = mysql_fetch_array($result);
if($row[0] != ''){
$from = $row[0];
}
$email_pass = $row[1];
}
$recipients = "$email";
$headers["From"] = "$from";
$headers["To"] = "$email";
$headers["Subject"] = $subject;
$headers["Cc"] = "$cc"; //Line added by Me to see if it works
$headers["Bcc"] = "$bcc"; //Line added by Me to see if it works
//$mailmsg = "Welcome to Addatareference.com! \r\n\r\nBelow is your unique login information. \r\n\r\n(Please do not share your login information.)$accountinfo";
/* SMTP server name, port, user/passwd */
$smtpinfo["host"] = "smtp.emailsrvr.com";
$smtpinfo["port"] = "25";
$smtpinfo["auth"] = true;
$smtpinfo["username"] = "$from";
$smtpinfo["password"] = "$email_pass";
/* Create the mail object using the Mail::factory method */
$mail_object =& Mail::factory("smtp", $smtpinfo);
/* Ok send mail */
$mail_object->send($recipients, $headers, $mailmsg);
}
I have been trying to find a solution to this with no real info coming back my way. If someone could help me out with this I would be greatly appreciated.
There is a problem with Bcc though when using the PEAR Mail function: Thunderbird displays the Bcc header, so the recipient is not hidden, which is the whole point of Bcc. It is also displayed in the To: header list (since you have to include the Bcc list in recipients).
Edit: SOLVED - I found from another site that the solution is just to include the Bcc list in the $recipients, and not in any of the headers. It works!
Thus:
$bcc = "foo#example.com";
$to = "bar#example.com";
$headers = array(..other stuff.., 'To' => $to, ..rest of header stuff); // No Bcc header!
$recipients = $to.", ".$bcc;
$mail = $smtp->send($recipients, $headers, $message);
Edit #2: Acknowledging my source - http://raamdev.com/2008/adding-cc-recipients-with-pear-mail/
Have you tried to add multiple addresses "," separated?
$headers['Cc'] = 'cc#example.com, bb#example.com, dd#ex.com';
This might work, according to line 218 in the source.
You just need to add all to, cc, bcc in $recipients variable. Header decides where to send.

Categories