Email in loop sends the same file using Email class in Codeigniter - php

Hey I'm using Codeigniter's Email helpers, and expiriencing a wierd issue. I have the following code :
$path = mpdf_create_billing($html);
$this->load->library('email');
$this->email->set_newline("\r\n");
$this->email->from('no-reply#foo.com');
$this->email->to("foo#gmail.com");
$this->email->subject('Invoice for '.date('d/m/y'));
$this->email->message($message);
$this->email->attach($path);
if($this->email->send())
echo "Email Sent Successfully to $email with the file $path<br>";
else
echo "Should be sending email to $email , but i didn't<br>";
Now this code is inside a foreach loop,twice in this case. mpdf_create_billing returns path to a PDF file. now this code echos 2 different file paths, but the email is the same and in both loop runs and both of the emails contain the same file, though the file paths are different.
Anyone knows how to resolve it? this is what outputs for me :
Email Sent Successfully to foo#foo.com with the file
/path/to/pdf/Invoice_1368452801.82065190eec1c85eb.pdf
Email Sent Successfully to foo#foo.com with the file
/path/to/pdf/Invoice_1368452804.53475190eec482917.pdf
Could this be a problem with my SMTP server that send the emails? I tried it on 2 mail accounts, and same result.

Perhaps you should clear $this->email?
From the CodeIgniter docs:
$this->email->clear()
Initializes all the email variables to an empty state. This function
is intended for use if you run the email sending function in a loop,
permitting the data to be reset between cycles.
foreach ($list as $name => $address)
{
$this->email->clear();
$this->email->to($address);
$this->email->from('your#example.com');
$this->email->subject('Here is your info '.$name);
$this->email->message('Hi '.$name.' Here is the info you requested.');
$this->email->send();
}
If you set the parameter to TRUE any attachments will be cleared as
well:
$this->email->clear(TRUE);
Looks to me this is what you are doing?
Link CI3: https://www.codeigniter.com/user_guide/libraries/email.html
Link CI2: https://www.codeigniter.com/userguide2/libraries/email.html

Related

How to check mail is sent or not in laravel?

I'm currently working on laravel mail. I've sending email structure with attachment like this:
$document = Document::find($request->document_id);
Mail::send('email.senddocs', array('project'=>$document->project->name,'type'=>$document->documentType->type), function($message) use($request,$document){
$file_name = public_path().'/documents/'.$document->project->p_key.'/'.$document->file_name;
$message->from('us#example.com', 'DDMS');
$message->to($request->email);
$message->attach($file_name);
});
I've already visited here. But, the process over there always returning success.
What I actually want is to know if mail is send or not. In my case, Mail sending can fail due to fake email like akdjbaksdjf#jhbasdhadfs.com or by some other errors occurred.
How can I know mail is sent or not ?
Any help is appreciated
This questions is asked several times here:
Laravel 5 - check if mail is send
You can use the failure method for this:
if( count(Mail::failures()) > 0 ) {
foreach(Mail::failures as $email_address) {
echo "$email_address <br />";
}
} else {
echo "Mail sent successfully!";
}
This only checks if a email was send. You can not handle not existing email adresses with this method.
You can use mailgun for this problem for example.
Another way is to use a php class which connects to a smtp server, and will check.
PHP class:
https://www.phpclasses.org/package/6650-PHP-Check-if-an-e-mail-is-valid-using-SMTP.html
Some Informationen of checking email adresses: How to check if an email address exists without sending an email?

PHP / Laravel 5 - Check if email is successfully received by recipient

How do I check if the email is RECEIVED by the recipient? In Laravel 5, I try using this method from this question:
$status = Mail::send('blah', $data, function($message) {
// callback function
});
if($status) {
// email is successfully sent
}
else {
// email is not sent
}
But even if the email is invalid, I still got $status = 1 after the code execution. I also have tried Mail::failures() which contain an array of failed attempt. But it always return 0, which mean all email is succesfully sent.
Is there any other way that I can check whether in Laravel?
OR
is it possible to check if the email is valid using PHP? Since the email is invalid, there is no point to send it.
To check if someone has received put this in the header:
Disposition-Notification-To:<xxx.xxx#example.com>
But this option is not very reliable it is better to check if your email was read like so:
<img src="http://yourdomain.com/received?read=<email of `receiver>">
This method also does not always work.
For example if the Email client doesn't download the images automatically.
I hope this helped anyway and was what you were searching for.

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.

How to save an Email attachment as an Email message?

I am pretty new when it comes to handling emails in php.
Basically I need to read a pop3 mailbox and save the messages to a database and the attachments to a folder. Then mail that same mail (slightly altered) back to another email recipient.
I thought I got this all working, then I realized when the attachment is another mail message I only receive the body as text (after base64_decode).
Is it possible to save an attached email to a file and then send it as attachment in another message?
Any help will be appreciated.
Here is some code to put it into perspective :
// This is the IMAP connection: imap_open("{"."$host:$port/pop3$ssl"."}$folder",$user,$pass,OP_SILENT);
$mailConnection = $this->Maildata->mail_login($host,$port,$user,$pass,$folder,$ssl);
// This retrieves the list of mails in the current folder
$messageList = $this->Maildata->mail_list($mailConnection,$message);
foreach ($messageList as $messageId => $messageListData) {
// This reads the mail object into an array
$mailMessage = $this->Maildata->mail_mime_to_array($mailConnection,$messageId,true);
// This loops through each mail
foreach ($mailMessage as $tmpId => $tmpData) {
// Excluded save to database since it is unrelated
// This checks if item is an attachment and saves it.
if (isset($tmpData['is_attachment']) && $tmpData['is_attachment'] === true) {
$tmpName = date('Y-m-d H:i:s', strtotime('now'));
$this->Maildata->mail_attachment_save($tmpData['data'], $tmpName .$tmpData['name']);
}
}
}
P.S. This all works, but imap_fetchbody($mailConnection , $messageId, $prefix); doesn't seem to send all the data I need.
Yes.
You can save attached mail messages as a file, but it will not be in a format that Microsoft Office Outlook will understand. Interestingly enough if you save the file as a .eml then windows live mail will handle the message perfectly.
No webmail service handles attached mail items as outlook and windlows live mail does and gmail ignores them completely.

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