Can't embed variable in mail function php - php

I want to send email with different subjects. I have the following code that get mail subject and message from the database.
$sql = "SELECT mail_subject, mail_content FROM new_business WHERE business_name = '$biz_name'";
$query = mysqli_query($db, $sql);
while ($row = mysqli_fetch_array($query)) {
$subject = $row['mail_subject']; //Getting mail subject to send
$mail_content = $row['mail_content']; //Getting mail content
}
echo $subject; //checking if it gets subject and it surely does.
$to = 'utopian51#gmail.com';
mail($to, $subject, $mail_content);//Mail is not sent
If subject pulls data from database mail is not sent. I have checked several times including spam and also checked if subject gets any data from database before embedding it in mail function. It seems it gets the right data but the mail is not sent.
But when i hardcode the subject like this: $subject = 'this is a test subject'; , the mail is sent successfully. Why mail function does not let me embed variable that gets subject from database? I need to send the mails with different subject from database. Whats the solution?

There's many things which are confusing about the code you've written. You never check for error conditions in the mysqli_ calls nor the mail() call. You run a query which will discard all but the last row (if any). However the way to diagnose issues with mail() is not to watch your inbox. In more than 99% of cases the problem is with the interface between PHP and the MTA or with the MTA configuration itself.
You have provided no details of either.
I would recommend you use simply:
mail('utopian51#gmail.com', 'test', date('r'));
and start debugging your MTA.
Independently you can work on your bulk emailing script, simply writing to a file instead of calling mail() until both parts are working.

Related

PHPMailer sending email to wrong recipient

I have a situation where I sending an 2 different emails, one to a customer and one to a member after the member accepts a task. Within the function I am calling 2 functions, one that sends to the client and one is sent to the member. The what is happening is that the second email is being sent to both the member and the client, yet the client, the first email, only gets the email expected.
What I figured is happening is that because both of these functions are occurring at the same time, some how the addAddress() function is taking both email addresses over to the second email. My proof of this is I changed the order of the functions so the Member send first and the Client sent second and they both got the Client email where before the Client sent first and the Member sent second.
My question than is how do I make the first email break for a few seconds before the second email is sent to stop this from happening.
Here is the basic set up of the code.
function memberAccept()
{
// SQL to mark in DB that member accept task
$this->memberEmail($id)
$this->clientEmail($id)
}
function memberEmail($id)
{
// SQL to gather information about member inner join with task
$subject = 'Email';
$body = 'Email to member'
$this->sendEmail($member['email'], $subject, $body)
}
function clientEmail($id)
{
// SQL to gather information about client inner join with task
$subject = 'Email';
$body = 'Email to client'
$this->sendEmail($client['priemail'], $subject, $body)
}
sendEmail() is in another Class set up to use PHPMailer functions to send the email.
In your case, you must use the following function before adding new recipient for the mail:
$mailer->ClearAllRecipients( ); // clear all
Basically, the problem is that, when you add first recipient, mailer successfully delivers the mail, but the object is not destroyed yet. Hence, when the next mail is sent, member's email is still in the recipient, so alter your code to call this ClearAllRecipients function before adding the new recipient.

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).

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>");
}

Using PHP mail( )

I have a php mail script which works perfectly on a one host. However, when I attempt to use the same script on a network solutions host, the function returns true but no email ever sends.
//get mail function data
$case = $_POST['case'];
$to = addslashes(strip_tags($_POST['to']));
$message = addslashes(strip_tags($_POST['message']));
$subject = addslashes(strip_tags($_POST['subject']));
$message = addslashes(strip_tags($_POST['message']));
$from = "confirmation#website.co";
$headers = "From: $from\r\n";
//send email
if (mail($to,$subject,$message,$headers)){
//formatting for error message
$emailSent = "block";
$emailFailed = "none";
}
else //if the email fails to send
{
$emailSent = "none";
$emailFailed = "block";
}
?>
Does anyone know if different hosts require specific info in mail script?
This is a question for Network Solutions customer support. Sending mail from shared hosting servers is usually well locked down -- if they allow it at all, it's throttled. Also, calling mail just means the message was successfully passed to sendmail, not that the mail ever left the server. It could be sitting in a queue to be sent, it could have bounced for a million reasons beyond your control, etc.
Some hosts have this issue. On mine (Mosso), I had to adjust the last parameter (from) like so:
mail($to, $subject, $message, $headers, "-f".$from)
May not be the solution for NetSol but worth a try. I know some hosts disable the script and require using the smtp class to send mail.
This is assuming, of course, everything is correct with your DNS and MX records. If you're trying to send from an account that is different than the domain being sent from, some providers will automatically block that.
It maybe that they have not enabled php mail, but ask the host for specifics.
Looking at your code it is not cleaning the input sufficiently. take a look at the is_forbidden function here:
http://thedemosite.co.uk/phpformmailer/source_code_php_form_mailer_more_secure_than_cgi_form_mailers.php

which of this method is recomended - mail function

hay
now i want to send mail to 200 users
first way
$users = 'user1#,user2#,user3,etc';
foreach(explod(',',$users as $mail){
mail($mail,'','','');
}
or
mail(mail one,mail 2,mail3,mail4,etc)
i know the code is fully error
but i want the meaning
which is the best
mail with multi by spreate by ,
or looping the mail function with the one mail every time
Ideally, you should use an external piece of mailing software so you can send an e-mail to a list and it will handle individual recipients; this way, you can avoid looping mail calls (and queued sendmail requests) while not disclosing your mailing list.
Of the options you presented, it is best to send your email using a loop with individual calls to the mail function so as not to disclose your recipients.
Finally, maybe try something like this:
$recipients = array('user#example.com','admin#example.org',); // mail list
$bcc = join(',', $recipients);
mail(
'"Undisclosed Recipients" <no-reply#example.com>',
$subject,
$message,
"BCC: {$bcc}"
);
However, if you are using this, make sure whatever sendmail client you are using strips the BCC header before sending.

Categories