I have a script I run to send emails with attachments.
On my old server it worked perfectly, But since moving a few days back. The script no longer works. As soon as it is supposed to send a mail, it bombs out.
I did find this error in the error log file,How can I locate the problem?
[08-Jul-2015 12:39:17 UTC] PHP Fatal error: Call to undefined function mime_content_type() in /home/username/public_html/editor/cronjob.php on line 79
My code more or less looks like the below:
function sendmailwithattachment($from,$to,$subject,$body,$host,$username,$password,$body_id,$port) {
$query = "SELECT * FROM `email_attachments` where `email_id`='$body_id';";
if($result = mysqli_query($this->link, $query)){
if($result->num_rows!=0){
$headers = array ('From' => $from, 'To' => $to, 'Subject' => $subject);
$crlf = "\n";
$mime = new Mail_mime($crlf);
$mime->setHTMLBody($body);
while ($row = mysqli_fetch_array($result,1)) {
$attachment=$row['attachment'];
$mime->addAttachment($attachment, mime_content_type($attachment));
}
$body = $mime->get();
$headers = $mime->headers($headers);
} else {
$headers = array ('From' => $from, 'To' => $to, 'Subject' => $subject,'Content-type' => 'text/html;charset=iso-8859-1');
$body = $body;
}
}
$smtp = Mail::factory('smtp',
array ('host' => $host,
'port' => $port,
'auth' => true,
'username' => $username,
'password' => $password));
$mail=$smtp->send($to, $headers, $body);
if (PEAR::isError($mail)) {
echo "<p>Message not sent:".$mail->getMessage()."</p>";
return $mail->getMessage();
} else {
//echo "<p>Message successfully sent!</p>";
return 'sent';
}
}
It looks like mime_content_type() is deprecated anyway, try replacing it with Fileinfo functions as they mention in the docs. It might solve your problem in the process (though hard to say, because we don't know what your code looks like).
Related
I am trying to send an email from my wordpress website using the function wp_mail and I am getting this error:
Warning: trim() expects parameter 1 to be string, array given in /var/www/html/wp-includes/class-phpmailer.php on line 973 0
I don't really understand why I am getting it.
This is my current code:
// Contact form Ajax
add_action('wp_ajax_nopriv_submit_contact_form', 'submit_contact_form');
function submit_contact_form(){
if(isset($_POST['email'])) {
$email = $_POST['email'];
$email_to = "info#yyy.com";
$host = "ssl://smtp.gmail.com:465";
$username = 'myEmail#gmail.uk';
$password = 'mypassword';
$email_subject = "You have a new email from $email via yyy.com website";
$message = $_POST['text'];
$headers = array ('From' => $email, 'To' => $email_to,'Subject' => $email_subject);
/*$smtp = Mail::factory('smtp',
array ('host' => $host,
'auth' => true,
'username' => $username,
'password' => $password));*/
//$mail = $smtp->send($email_to, $headers, $message);
wp_mail( $email_to, $headers, $message );
/*if (PEAR::isError($mail)) {
echo($mail->getMessage());
} else {
echo("Message successfully sent!\n");
}*/
}
}
error_reporting(E_ALL);
ini_set("display_errors", 1);
I installed a package called "PostFix" which is responsible for sending emails from an Ubuntu server (which is the servers linux I use), and it solved the problem!
I have a PHP function to send emails,
function sendemail($email_to,$email_from,$email_subject,$email_body,$email_replyto,$cc)
{
if(filter_var($email_to, FILTER_VALIDATE_EMAIL))
{
require_once "/usr/local/lib/php/Mail.php";
$from = $email_from;
$to = $email_to;
$subject = $email_subject;
$body = $email_body;
$host = "mail.domain.co.uk";
$username = "sending#domain.co.uk";
$password = "********";
$headers = array ('From' => $from,
'To' => $to,
'Cc' => $cc,
'Subject' => $subject,
'Content-type' => 'text/html');
$smtp = Mail::factory('smtp',
array ('host' => $host,
'auth' => true,
'username' => $username,
'password' => $password));
$rec = $to.', '.$cc;
$mail = $smtp->send($rec, $headers, $body, $cc);
}
}
when i call the function, sometimes there is no $cc variable so i get a warning saying that Missing argument 6 for sendemail(),
whats the best way to stop the warning if $cc is not valid?
If you wrote that function, you can make the 6th parameter optional:
function sendemail($email_to, $email_from, $email_subject, $email_body, $email_replyto, $cc = null) {
if ($cc !== null) {
// add cc headers, e.g.
// $headers['Cc'] = $cc;
}
}
You will then have the option to omit this parameter:
sendemail("to#example.com", "from#example.com", "subject", "body", "replyto#example.com");
sendemail("to#example.com", "from#example.com", "subject", "body", "replyto#example.com", "cc#example.com");
Use this
function sendemail($email_to,$email_from,$email_subject,$email_body,$email_replyto,$cc = "")
Try this,
function sendemail($email_to,$email_from,$email_subject,$email_body,$email_replyto,$cc=NULL)
put $cc = NULL.
So you will not get warning if there is no $cc .
If you are able to change the send email function:
function sendemail ($email_to, $email_from, $email_subject, $email_body, $email_replyto, $cc=null) { }
Just make sure that the function body itself will not have problem with a null $cc.
I am using pear to send email using the code below, on the first time through I get this error every time:
"Failed to set sender: aa#bb.com [SMTP: Invalid response code received
from server (code: -1, response: )]"
If I debug and set the execution point back to 'Re-run from here' after the send, it works fine - almost like something is not initialised properly first time round - anyone have any ideas on this?
require_once "Mail.php";
function SendEMail() {
$from = "Sender Sender <sender#example.com>";
$to = "AA BB <aa#bb.com>";
$subject = "Test";
$body = "This is a test";
$host = "myhost";
$headers = array ('From' => $from,
'To' => $to,
'Subject' => $subject);
//Re-run from here
$smtp =& Mail::factory('smtp',
array ('host' => $host,
'auth' => false,
'username' => '',
'password' => ''));
$mail = $smtp->send($to, $headers, $body);
if (PEAR::isError($mail)) {
$s = $mail->getMessage();
}
}
Use wireshark to find out which response you really get from the server. Maybe the server has activated greylisting, so that you can't send a mail the first time.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to send emails with PHP using the PEAR Mail package with attachment
I want to send an email with PHP using a generated attachment (not a file on the server), I was curious about the syntax.
<?php
#require_once "Mail.php";
$from = "foo#me.com>";
$to = "blah#me.com>";
$subject = "Hi!";
$body = "Attached is the file\n\n";
$attachment = header( 'Content-Type: text/csv' );
$attachment = $attachment.header( 'Content-Disposition: attachment;filename=example.csv');
$attachment = 'transactionid,valid,fname,lname,email,studentid,status
"654","1","First Name","Last Name","Email#me.com","1000000"';
$body = $body.$attachment;
$headers = array ('From' => $from,
'To' => $to,
'Subject' => $subject);
$smtp = #Mail::factory('smtp', array('host' => $host,
'port' => $port,
'auth' => true,
'username' => $mail_username,
'password' => $mail_password));
$mail = #$smtp->send($to, $headers, $body);
if (#PEAR::isError($mail)) {
echo("<p>" . $mail->getMessage() . "</p>");
} else {
echo("<p>Message successfully sent!</p>");
}
?>
EDIT: Please note, i know there are many examples of attaching a file from the server in an email, but I am asking about attaching a generated csv file (that is NOT on the server) to an email.
Start with How to send emails with PHP using the PEAR Mail package with attachment
Then replace the following:
if ($mime->addAttachment($file,'application/pdf')){
with
$file = 'transactionid,valid,fname,lname,email,studentid,status
"654","1","First Name","Last Name","Email#me.com","1000000"';
if ($mime->addAttachment($file,'text/csv', 'example.csv', false)){
see documentation at http://pear.php.net/manual/en/package.mail.mail-mime.addattachment.php
I am sending emails successfully using following code. But now I want to attach a text file (example: test.txt) with email. Any Idea?
require_once "Mail.php";
$from = "Usman <from#example.com>";
$to = "Naveed <to#example.com>";
$subject = "subject";
$body = "";
$host = "smtp.gmail.com";
$username = "username";
$password = "password";
$headers = array ('From' => $from,
'To' => $to,
'Subject' => $subject);
$smtp = Mail::factory( 'smtp', array('host' => $host,
'auth' => true,
'username' => $username,
'password' => $password ) );
$mail = $smtp->send( $to, $headers, $body );
if ( PEAR::isError($mail) ) {
echo( "<p>" . $mail->getMessage() . "</p>" );
} else {
echo( "<p>Message successfully sent!</p>" );
}
Found this code as one of the first hits of the google://pear mail attachment search.
include('Mail.php');
include('Mail/mime.php');
$text = 'Text version of email';
$html = '<html><body>HTML version of email</body></html>';
$file = './files/example.zip';
$hdrs = array(
'From' => 'someone#domain.pl',
'To' => 'someone#domain.pl',
'Subject' => 'Test mime message'
);
$mime = new Mail_mime();
$mime->setTXTBody($text);
$mime->setHTMLBody($html);
$mime->addAttachment($file,'application/octet-stream');
$body = $mime->get();
$hdrs = $mime->headers($hdrs);
$mail =& Mail::factory('mail', $params);
$mail->send('mail#domain.pl', $hdrs, $body);
If you additionally make use of the PHP PEAR Mail_Mime module it provides the appropriate handling and encoding to incorporate attachments as part of your email.
here is the code you're looking for:
<?php
require_once "Mail.php"; // PEAR Mail package
require_once ('Mail/mime.php'); // PEAR Mail_Mime packge
$from = "Robert Davis <robertdavis#pobox.com>";
$to = "Sam Hill <sam.hill#aol.com>";
$subject = 'Test mime message with an attachment';
$headers = array ('From' => $from,'To' => $to, 'Subject' => $subject);
$text = 'Text version of email';// text and html versions of email.
$html = '<html><body>HTML version of email. <strong>This should be bold</strong></body> </html>';
$file = './sample.txt'; // attachment
$crlf = "\n";
$mime = new Mail_mime($crlf);
$mime->setTXTBody($text);
$mime->setHTMLBody($html);
$mime->addAttachment($file, 'text/plain');
//do not ever try to call these lines in reverse order
$body = $mime->get();
$headers = $mime->headers($headers);
$host = "sasl.smtp.pobox.com";
$username = "robertdavis#pobox.com";
$password = "Kdu48Adi3";
$smtp = Mail::factory('smtp', array ('host' => $host, 'auth' => true,
'username' => $username,'password' => $password));
$mail = $smtp->send($to, $headers, $body);
if (PEAR::isError($mail)) {
echo("<p>" . $mail->getMessage() . "</p>");
}
else {
echo("<p>Message successfully sent!</p>");
}
?>
Sending email with PHP always feels a little bit like a struggle. If you are able to use them I would recommend one of these two mail libraries for PHP:
PHP Mailer
Swiftmailer
You seem to be using the PEAR Mail package.
Take a look at the Mail_Mine object which does what you're trying to do and has an easy way of adding attachments (simply call addAttachments).
http://pear.php.net/manual/en/package.mail.mail-mime.php