How can i send mails through the php script??
I am trying to do somthing like this:
for($k=0;$k<=$x->length-1;$k++)
{
for($l=0;$l<=$j-1;$l++)
{
if($y->item($k)->nodeValue==$JobNoArr[$l] && $AcceptanceDateArr[$l]=='0000-00-00')
{
//echo $v->item($k)->nodeValue ;
$email = $v->item($k)->nodeValue . ",";
$to = $email;
$subject = "My subject";
$txt = "Hello world!";
$headers = "From: webmaster#example.com" . "\r\n" .
"CC: someother.valid#email.adr";
mail($to,$subject,$txt,$headers);
}
}
}
Please help me in this issue.
Best
Zeeshan
I strongly advise against sending mail using PHP's mail() function. Composing valid emails and delivering them successfully is trickier than it seems at first glance. There is encoding, putting parts together, validation & sanitation, error reporting (more than bool mail(...)), support for authentication and on and on ... The mail() function does not offer any of these things.
Try SwiftMailer or similar. You can configure it to use PHP's mail() function and so much more. I highly recommend it.
You're trying too much all at once. Try going one step at a time. First send a simple email, with hard-coded parameters to get that working, then troubleshoot it within the context of your nested loops.
the problem is mail function is very unreliable, especially when sending large amount of emails.
i would recommend looking into PHPmailer library (uses direct SMTP connection):
http://phpmailer.codeworxtech.com/
The code (the inner most block) looks correct. Make sure your environment is setup correctly.
http://ca3.php.net/manual/en/mail.setup.php
first do
echo $result = mail($to,$subject,$txt,$headers);
and see what u get , error ?
I recommend to use a class such phpMailer
why you have comma in the end of ths line ?
$email = $v->item($k)->nodeValue . ",";
you send to one mail every time.
php-s mail function uses sendmail as a MTA, so if some mails go through and some not, I would look at sendmail's log for errors.
If you are on a shared web host, or your home computer, the main domain for the server will be something like
server.your-isp-or-host.com
The spam filter will then see the email claiming to be from
yourdomain.com
when it really came from the first address, and would then delete it.
This would explain the hit-and-miss nature of your error.
If you are on a dedicated server, or a static IP pointing to your home computer with properly set up DNS, the above does not apply.
if it works sending emails to gmail then it should work fine sending emails to yahoo too
you may find that the issue is not the sending of the emails, but maybe yahoo is marking them as spam or blocking them at the gateway
i notice you are appending a comma to the end of the email address, what is the point of that ?
there may be other problems, are your loops correct, are they covering all the cases you expected.
are you sending thousands of emails ? can your mta handle the rate at which you are putting emails into the queue
is your script hitting max_execution time and stopping ?
Related
I have a php file that I use to send newsletters. Recently I moved to a new server and they use PEAR Mail instead of the default PHP mail to send mails and I had to update my script to function. But it's still not working. I get the TXT version not the HTML version.
If I manually enter the html codes inside the setHTMLBody() it works but when I replace it with my ob_start $output_string variable it doesn't work.
Here is my script;
ob_start();
include "URL/To/File.php";
$output_string = ob_get_contents();
ob_end_clean();
$headers['From'] = 'from#email.com';
$headers['Subject'] = 'Newsletter Subject';
require_once('Mail.php');
require_once('Mail/mime.php');
$message = new Mail_mime();
$message->setTXTBody("Your client doesn't support HTML.");
$message->setHTMLBody(''.$output_string.'');
$mail =& Mail::factory('mail');
$result = $mail->send('myemailaddress#gmail.com', $message->headers($headers), $message->get());
if (PEAR::isError($result)) {
echo("<span>" . $result->getMessage() . "</span>");
} else {
echo("<span style='color: #f7941c; font-weight: bold'>Congratulations!
Your mail has been sent successfully</span>");
}
how do I correctly input the line below correctly? It's not working as is right now.
$message->setHTMLBody(''.$output_string.'');
So I'm cold on this subject right now (working on mobile) though let's see if I can help you out. So I looked up the setHTMLBody function. It's a little fuzzy on the type that the expected parameters should be. In PHP you can get the type using gettype($example) (like console.log(typeof example); in JavaScript though PHP is generally more forgiving about types (calculating a number that has a string type will work in PHP, not JavaScript)).
The name of the function implies that it should make this part of the email HTML. Now of all the modules I've built on my web platform email has been the most challenging not because it's inherently complex though because it's very subjective. In example some servers might expect you to serve an <html> element, others a <body> element and others won't care if you omit it (and I'm not sure what if any specifications declare what is "proper" here). I've not intentionally worked with compressing data in emails (just output in web mail though it's technical context is lost at that point). Long story straight here: the client's user agent (browser, email application, etc) should be handling the compression, not you.
PHP ob stuff is a bit convoluted. I dislike the same function/method being used for both compression and being able to capture and do find/replace with the output before sending it to a client. I think you're using it for compression though you could also be using it to replace bits of code for whatever reason. In this case your best bet for troubleshooting (presuming that your ob should work, most likely for replacing bits of code) is to use the string and test it outside of this environment. When I test cron jobs I always test them in normal environments first (though keep in mind cron jobs run in a much more limited environment so for debugging there I just have print_r($_SERVER) send me information via email).
So I think your ob code is messing up the parser setHTMLBody() function. Break your code down until you have working bits and then add your necessary and increasingly complex bits to it until you hit a problem and then because you know exactly what you just added you'll be able to single out the issue much easier.
I'd need further clarification though I can edit this answer later. Let me know where you're at, I always check notifications even if it takes a day.
I have a few dozen tools that I use when I develop. I'm not sure if this tool will validate though it may help you somehow since you are working on email. https://www.mail-tester.com/ helped me address some issues related to email (it's not related to this issue).
I've been searching a lot, but can't really find what i exactly need.
Im running a shop-online using XAMPP, and what i want is send emails to the customers with their order but using a specific function.
What this fuction does, is to hide some characters (for security purpose) on the email sent to customers. So i've made the function (looking on internet), but i want to test it now.
This is my function (if it's wrong, i'd really appreciate some help):
<?php
function xtc_hide_iban ($iban) {
$length = strlen($iban);
$lchars = substr($iban,0 ,5);
$rchars = substr($iban, -5);
$iban_hidden = $lchars.str_repeat('*',$length-10).$rchars;
return $iban_hidden;
}
?>
I think it's pretty obvious what i try to do, but i will still explain it:
Get the $iban from customers, and show only the first and last 5
characters when the email is sent e.G
Your IBAN is 'DE123************56789'
So, for now i can send emails from Mercury mail server to 'root#localhost',account i made on thunderbird, (it's the only account that worked for me, because any other with the same server, like 'anyname#localhost' didn't work or couldn't be created, and those who were create with imap before couldn't access to the inbox "could not connected to server, connection refuse", anyway this works with POP3)
Following what i looked before, is that somehow and somewhere i can put a *.php on Mercury folder so i will get a template of how to send the mails (headers, subject,body,etc).
My main question is how and where to do that? make a test php file to make sure my code is doing what i want to do
Thanks in advance
I am using PHP's mail() function to send emails from my application. Is there any way to see the emails that have been sent if I SSH into the server? I need the actual body of the emails not just a record of the action. This is probably a longshot but any help would be appreciated.
EDIT: I should have been clearer. This is for messages already sent in the past. I can definitely take steps to log or otherwise report sent messages in the future but is there a way to retrieve messages that have already been sent?
The mail function doesn't append mails to a Sent mailbox in the server, so I'd say no: as it is you can't find the body of emails sent through PHP.
However you might create your own mail function to log somewhere the content you're sending.
Also there are 2 lines of config in php.ini which makes me think that you could append it to an existing mailbox with the right config.
;sendmail_path =
; Force the addition of the specified parameters to be passed as extra parameters
; to the sendmail binary. These parameters will always replace the value of
; the 5th parameter to mail(), even in safe mode.
;mail.force_extra_parameters =
I usually add an argument to my mail function to indicate that I want to write a record of the email along with the content into an HTML file. That way, when I am testing, I can browse to a temp folder and see what the email would have looked like. Here's some mock code:
function send_email($to=false,$message=false,$from=false,$test=false) {
... do stuff to send email ...
if ($test == true) {
$out = 'Mail to: '.$to.'<br />From: '.$from.'<br />Time: '.date('m/d/Y h:i', time()).'<hr />'.$message;
// use a random string for a file name
$fname = random_string(20);
.. write the $out var to a file named $fname ...
}
}
If you have access to the email address that this is being sent to, then you can use PHPs IMAP functions.
http://www.php.net/manual/en/book.imap.php
I'm using Zend_Mail and the following code to send my email messages.
$mail = new Zend_Mail('UTF-8');
$mail ->setBodyText($plainBody)
->setBodyHtml($htmlBody)
->setSubject($subject)
->setFrom(FROM_ADDR, FROM_NAME)
->addTo($email, $name )
->addHeader(MY_HEADER, serialize( array( 'foo' => 'bar' ) ) )
;
I need to check the spam rating for the prepared message and I'd like to do it using SpamAssassin.
I thought to create a file with the contents and running something such as exec('spamc $filename'), but how to get the file content with the full MIME body?
I noticed that there's a _buildBody() function in Zend_Mail_Abstract class (library/Zend/Mail/Transport/Abstract.php) that's return that, but that's a protected function.
Thanks
If you want to use SpamAssasin, then run your email message through spamc:
http://spamassassin.apache.org/full/3.1.x/doc/spamc.html
Spamc is the client half of the spamc/spamd pair. It should be used in
place of spamassassin in scripts to process mail. It will read the
mail from STDIN, and spool it to its connection to spamd, then read
the result back and print it to STDOUT. Spamc has extremely low
overhead in loading, so it should be much faster to load than the
whole spamassassin program.
You can do use in PHP by:
Writing the message into a temporary file and running shell_exec('spamc < message.tmp'), or
Running the command with proc_open() then send message via STDIN.
I am assuming you want to simulate a spam check on the recipient's end. That's an interesting idea, but note that the results it will give you will be far from 100% realistic. After all, it's the sending process that adds much of the vital information that helps determine whether an E-Mail is spam (e.g. the sender IP and routes.)
Anyway, to do this, you will probably have to implement a custom Zend_Mail_Transport class, based on the example of Zend_Mail_Transport_Smtp. Any data that transport class sends to the SMTP server, you would have to re-route to a text file. As far as I can see at a cursory glance, it's going to be a bit of work but not impossible.
how do I add a CC mail address in PHPMailer running on a Linux server?
AddCC method only works on Windows: http://phpmailer.worxware.com/index.php?pg=methods
I tried with this method but the mail never arrives... I also tried with $mail->addCustomHeader('CC: mymail#mail.com') without success.
Thank you.
I had problems with addCC with PHPMailer when there was already a recipient present. In order to fix this, the safemode of the mail function within PHPMailer had to be removed due to the shared server not allowing it. If you turn on errors you will find where the problem is coming from.
First answer here for reference.
PHPmailer multiple recipients error
I have added some lines of code in my application. And I call it tricky things. You can add cc in your code as below :
if($ccList != "") {
$ccRecipients = explode(",",$ccList);
foreach($ccRecipients as $ccRecipient) {
$mailer->AddCC($ccRecipient);
//$mailer->AddAddress($ccRecipient);
}
$mailer->AddCustomHeader("Cc: $ccList");
}
You can change the code to meet your appication needs.
I hope it's working for you :)
Thanks.