Class MailerPHP strange behaviour (looping array of address) - php

I am using Class MailerPHP to send emails on the website. Actually is working perfect as it have to work but there is some strange stuff that i can not figure out why is like this.
1. I have an array of address that i am sending emails, the array is like this:
$email = new sendEmail();
$_ADMIN_EMAIL = array('first_email', 'second_email', 'third_email');
$email->setEmail($_ADMIN_EMAIL);
The problem is when is sending emails, is sending 3 emails:
1. sending to first_email
2. after sending to first_email and second_email
3. and at the end sending to first_email, second_email and third_email
I would like to send only one to all 3 and do not send 3 times email, i do not understand why is sending like this.
2. And second problem is i am using my Google account to connect to the SMTP to send emails, and i do not know why in the TO field of the email is showing my gmail address that i connect to the SMTP, i setup which address to show there and is showing + my gmail account:
I have this configuration only and i didn't put anywhere else my gmail account only for SMTP connection:
public $Username = 'my_gmail_account_to connect_to_smtp';
public $Password = 'my_gmail_account_password';
The result of this when i get emails is first email that i setup to show for reply + my_gmail_account_to connect_to_smtp, i need to take out my_gmail_account_to connect_to_smtp from the email that the user will not see it.
If you need for information about the code i use please let me know i will put.
All the code that i send email:
$_ADMIN_EMAIL = array("first_email", "second_email", "third_email");
$email2 = new sendEmail();
$email2->setSubject("Subject");
$email2->setUserName($_USER['name']);
$email2->setMT4Account($_USER['login']);
$email2->setDate($_USER['date']);
$email2->setAmount($_USER['amount']);
$email2->setCurrency($_USER['currency']);
$email2->setRegisteredBankAccount($_USER['type']);
$email2->setBankAccountName($_USER['card']);
$email2->setTemplate('template_for_the_email');
$email2->setEmail($_ADMIN_EMAIL);
echo $email2->send();

Try this:
$_ADMIN_EMAIL = array('first_email', 'second_email', 'third_email');
foreach($_ADMIN_EMAIL as $ademail)
{
$email->setEmail($ademail);
}

Related

PHP mailer form issue when sending to Office365 account

My client findspace.co.uk is name registered and hosted with Fasthosts. However since the summer they migrated their email to Office365 and since then the PHP mailer form at findspace.co.uk/pages/arrange.php is returning the error
We're sorry - there was a problem sending this email - please try again.
I've tried changing the smtp email host from fasthosts to Office 365:
<?php include_once("../class/class.phpmailer.php"); ?>
<?php
/********************************* INITIALISE PAGE SETTINGS ******************************************/
$page_title = "Contact Us";
$website_name = "Findspace";
$mail_host = "smtp.office365.com"; // email host
$mail_emailsource = "kp#findspace.co.uk"; // from email address
$mail_emailrecipient = "kp#findspace.co.uk"; // recipient
$mail_username = "contact#findspace.co.uk";
$mail_password = "MYPASSWORD";
But it's still not working. I'm no php expert and I can't figure out where things are going wrong. I wonder if it's an Office365 exchange account filtering issue?
Hi Len - I can't seem to find the bit that does the initializing - me just being a bit thick I think. The form has worked for 5 years up until my client changed email hosting to Office365 in the summer so I'm not certain it's the actual form - or perhaps it's the information i'm using for the hostname etc.
The $mail_host setting for that domain is:
findspace-co-uk.mail.protection.outlook.com
The IP is:
213.199.154.87
http://mxlookup.online-domain-tools.com/
I think you should take a look at this page and compare it to how you are initializing phpmailer on your end.:
http://phpmailer.worxware.com/?pg=examplebsmtp

500 error after PHP mail()

UPDATE
I was able to get the hosting info from my client and I contacted support, apparently there's an issue with the hosts mail function at the moment and they are working on a resolution. Will wait to see if that's the cause of this problem and will report back.
END UPDATE
I am trying to set up a simple contact form that will send an email. I have the form action set to the below PHP file.
The email gets sent, but the user experience ends with a 500 error instead of sending the user to the confirmation page.
If I comment out the mail() part, then the form redirects the user to the confirmation page successfully, but of course no email gets sent.
The website is hosted on GoDaddy, and I don't have access to the hosting account, though I can try to get it if I need it.
Here's the PHP code:
<?php
$name = $_POST['name'];
$address = $_POST['address'];
$city = $_POST['CITY'];
$state = $_POST['state'];
$zip = $_POST['zip'];
$email = $_POST['email'];
$howdidyouhear = $_POST['hear_about'];
$notifyshow = $_POST['notify_shows'];
$notifyonline = $_POST['notify_online'];
$interest_jewelry = $_POST['Interest_jewelry'];
$interest_prints = $_POST['interest_prints'];
$interest_folkart = $_POST['interest_folkart'];
$interest_indian = $_POST['interest_indian'];
$interest_closeouts = $_POST['interest_closeouts'];
$interest_other = $_POST['interest_other'];
$interest_other_text = $_POST['interest_other_text'];
$spamvalid = $_POST['validate'];
$honeypot = $_POST['website'];
//Spammer Handling
if ($honeypot!=null){echo 'You have been flagged as a spammer, please go away!'; exit;}
if ($spamvalid != '357'){
echo "
<script>
function goBack() {
window.history.back()
}
</script>
You didn't enter the correct number at the bottom of the form. Please try again.<br><button onclick='goBack()'>Go Back</button>";
exit;
}
//START EMAIL
//Body
$mailbody="Name: {$name}\n\nAddress: {$address}\n\nCity: {$city}\n\nState: {$state}\n\nZip: {$zip}\n\nEmail: {$email}\n\nHow did you hear about us?: {$howdidyouhear}\n\nWould you like to be notified when we will be doing a show in your area?: {$notifyshow}\n\nWould you like to receive email notifications of special sales and online events?: {$notifyonline}\n\nWhat brought you to mishuganah.com?: {$interest_jewelry} {$interest_prints} {$interest_folkart} {$interest_indian} {$interest_closeouts} {$interest_other}: {$interest_other_text}\n\n";
//Send Email
mail('matt.rodela#gmail.com','New submission from Mishuganah.com', $mailbody, "From:{$email}\r\n" );
header("Location: http://".$_SERVER["HTTP_HOST"]."/mailing_list/confirmation_page.htm");
?>
I am a relative novice with PHP, so please explain your solutions fully. Thanks!
Use phpMailer instead of php mail() function below you will find reasons not to use built in php mail function
In some cases, mails send via PHP mail() did not receive the recipients although it was send by WB without any error message. The most common reasons for that issue are listed below.
wrong format of mail header or content (e.g. differences in line break between Windows/Unix)
sendmail not installed or configured on your server (php.ini)
the mail provider of the recipeint does not allow mails send by PHP mail(); common spam protection
Errors in the format of header or content can cause that mails are treated as SPAM. In the best case, such mails are transfered to the spam folder of your recipient inbox or send back to the sender. In the worst case, such mails are deleted without any comment. If sendmail is not installed or not configured, no mails can be send at all.
It is common practice by free mail provider such as GMX, to reject mails send via the PHP function mail(). Very often such mails are deleted without any information of the recipient.
So it turns out it was an issue on GoDaddy's end and it has been resolved. The form is working now. Apparently there was nothing wrong with the code.
Thanks for the suggestions folks, I learned some stuff (going to sanatize and filter my inputs now).

How to not make PHPmailer send an email with multiple 'to' addresses?

I have a table in my database which contains emails to be sent. A script runs periodically as a cron job to read the data from table and dispatch the emails via the SMTP server. I am using PHPMailer for the work here. The script runs a loop for sending emails until all the emails in the table have been sent.
The problem is that most consecutive emails being sent have a common subject. So when the loop runs, somehow all the emails that get sent have a long 'to' list. So basically if 5 email entries (with different to addresses) have the same subject and same content, then it ends up sending only 1 email with 5 addresses (one of each email entry) in the 'to' field. This reveals the email address of every person to everyone else on the list. This is undesirable and the emails must be sent to only that one person for whom it is meant.
I don't know what's at play here. Any suggestions?
Are you doing something like this?
$mailer = new PHPMailer();
while($row = fetch_from_db()) {
$mailer->AddAddress($row['email']);
$mailer->send();
}
If so, you need to do a
$mailer->ClearAllRecipients()
after you send each email, so you start out with a "fresh" To: list.
while($row = fetch_from_db()) {
$mailer->AddAddress($row['email']);
$mailer->send();
$mailer->ClearAllRecipients()
}
1st, get the contacts from your db
$contacts = 'GET CONTACTS ARRAY FROM DB'
if(!empty($contacts)){
foreach ($contacts as $crt_contact){
$emails[] = $crt_contact->email ;
}
}
2nd use the following to create the "to" field
$to = implode(',', array_unique($emails));
Use the $to to send in mail() function

Sending an email in php - swiftmailer

I've been searching for days if not weeks on how to automatically send a simple e-mail in php after a user completes my form. I've tried Pear, PHP mail, Swiftmailer, changed my php.ini, tried different servers and I'm going mad from none of it working. I have not successfully sent one e-mail yet. I've searched endlessly but I still have no idea what to do and why nothing is working.
At the moment I'm using Swiftmailer (second time round). I set up a test page with code:
<?php
require_once 'swift/lib/swift_required.php';
// CREATE TRANSPORT CONFIG
$transport = Swift_MailTransport::newInstance();
// CREATE MSG
$message = Swift_Message::newInstance();
// SET PRIORITY TO HIGH
$message->setPriority(2);
// SUBJECT
$message->setSubject('Subject');
// FROM
$message->setFrom(array('example#btopenworld.com'));
// TO
$message->setTo(array('example#googlemail.com'));
// EMAIL BODY
$message->setBody('Test');
// SEND
$mailer = Swift_Mailer::newInstance($transport);
$mailer->send($message);
if (!$mailer->send($message, $failures)) {
echo "Failures:";
print_r($failures);
}
?>
Looking at a similar post someone suggested the last part of the code to see errors and my error is:
Failures:Array ( [0] => example#googlemail.com )
No matter what e-mail I change it to (all using e-mails I have, so real e-mails) it doesn't work. If anyone has any help or suggestions it would be hugely appreciated.
example#btopenworld.com
Unless you are using a btopenworld server to send the E-Mail from, this is not going to work. The E-Mails from address needs to be associated with the server you are sending the message from. You can put the BT address into the reply-to header.
Also, SwiftMailer should have a "debug" switch telling you exactly what goes wrong, but I can't find it in the docs right now. Here is a logger plugin for Swiftmailer that should help if all else fails.

having a php page send an email

This question should have a simple, simple answer, but I just can't seem to get it working. Here's the scenario:
I created a php page -> this one: http://adianl.ca/pages/member_application.php. Once the form is completed, it proceeds to http://adianl.ca/pages/member_application_action.php, puts the data into a MySQL db, & thanks the user for their interest. Anyway, the form works perfectly, except for one little thing: whenever someone fills out that form, I want an email to be sent to sbeattie#adianl.ca, informing them that the form was filled out, & the email would include the form components. The problem is, I can NOT get an email to be sent to that address, or any address truth be told. Having a php page send an email should be a simple thing to do, but it's really baffling me.
Can anyone help me with this? This particular problem has been troubling me since yesterday, & if anyone can help me with this...man, thank you soooooo much.
JP
$mail->IsSMTP(); // telling the class to use SMTP
$mail->Host = "mail.adianl.ca"; // SMTP server $mail->From = "webadmin#adianl.ca";
$mail->FromName = "Web Administration [ADIANL]";
$mail->AddAddress("sbeattie#adianl.ca");
$mail->AddCC("justinwparsons#gmail.com"); the #messageBody variable is just a string
If you want to have the email sent using the server's sendmail client, you can use mail.
If you want it to use another mail server, there are extensions to connect to an SMTP server. I use PHPMailer.
If mail doesn't work, it could be that the server is not set up to send email, or it could be that the mail server is rejecting emails sent from php, amongst other reasons.
this code can also be used to email in php so have a look, you can find many more examples of emailing in php look around
?php
$to = "recipient#example.com";
$subject = "Hi!";
$body = "Hi,\n\nHow are you?";
if (mail($to, $subject, $body)) {
echo("<p>Message successfully sent!</p>");
} else {
echo("<p>Message delivery failed...</p>");
}

Categories