PHP attach audio file in email - php

I'm using the following SMTP mail code to send audio attachment:
<?php
session_start();
$title = $_POST['title'];
$first_name = $_POST['name'];
$last_name = $_POST['lastname'];
$email_from = $_POST['email'];
$scaptcha = strtolower($_POST['scaptcha']);
if ($scaptcha != $_SESSION['captcha']) {
echo 'You have entered wrong captcha';
exit(0);
}
require('./class.phpmailer.php');
function clean_string($string) {
$bad = array("content-type", "bcc:", "to:", "cc:", "href");
return str_replace($bad, "", $string);
}
$email_message = "";
$email_message .= "Title: " . clean_string($title) . "\n";
$email_message .= "First Name: " . clean_string($first_name) . "\n";
$email_message .= "Last Name: " . clean_string($last_name) . "\n";
$email_message .= "Email: " . clean_string($email_from) . "\n";
$allowedExts = array("mp3","wav","dss");
$temp = explode(".", $_FILES["file"]["name"]);
$extension = end($temp);
if ((($_FILES["file"]["type"] == "audio/mpeg")) && in_array($extension, $allowedExts)) {
if ($_FILES["file"]["error"] > 0) {
echo "<script>alert('Error: " . $_FILES["file"]["error"] . "')</script>";
} else {
$d = 'Audio/Uploads/';
$de = $d . basename($_FILES['file']['name']);
move_uploaded_file($_FILES["file"]["tmp_name"], $de);
$fileName = $_FILES['file']['name'];
$filePath = $_FILES['file']['tmp_name'];
}
} else {
echo "<script>alert('Invalid file')</script>";
}
$headers = 'From: ' . $email_from . "\r\n" .
'Reply-To: ' . $email_from . "\r\n" .
'X-Mailer: PHP/' . phpversion();
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->SMTPDebug = 0;
$mail->Debugoutput = 'html';
$mail->Host = "smtp.gmail.com";
$mail->Port = 25;
$mail->SMTPAuth = true;
$mail->Username = "saro17.ams#gmail.com";
$mail->Password = "*****";
$mail->SetFrom($email_from, $first_name . ' ' . $last_name);
//$mail->AddReplyTo('replyto#example.com','First Last');
$mail->AddAddress('saro17.ams#gmail.com', 'Saravana');
$mail->Subject = 'New audio file received';
$mail->MsgHTML($email_message);
$mail->AltBody = 'This is a plain-text message body';
$mail->AddAttachment($_FILES['file']['tmp_name'], $_FILES['file']['name']);
if (!$mail->Send()) {
echo "<script>alert('Mailer Error: " . $mail->ErrorInfo . "')</script>";
} else {
echo "<script>alert('Your request has been submitted. We will contact you soon.')</script>";
Header('Location: contact.php');
}
?>
Please help me to fix this. I have been trying this for more than a week. Still I didn't get it. I have tried the PHP mailer also. That also not working.
UPDATE: I'm getting the following error:
Mailer Error: The following From address failed: saro17.ams#gmail.com : Called MAIL FROM without being connected,

send the audio file link in message instead of inline attachment.
$mail->AddAttachment method use for inline attachment.
due to the file encryption and file size , maximum server not allow to send inline attachment of audio, video or zip files.

Well..It's really very easy to attach anything while using PHPMailer.Here is the code :
PHPMailer Link : https://github.com/PHPMailer/PHPMailer
You can add attachments like :
$mail->addAttachment('/var/tmp/file.tar.gz'); // Add attachments
While here is full code :
<?php
require 'PHPMailerAutoload.php';
$mail = new PHPMailer;
//$mail->SMTPDebug = 3; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp1.example.com;smtp2.example.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'user#example.com'; // SMTP username
$mail->Password = 'secret'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587; // TCP port to connect to
$mail->setFrom('from#example.com', 'Mailer');
$mail->addAddress('joe#example.net', 'Joe User'); // Add a recipient
$mail->addAddress('ellen#example.com'); // Name is optional
$mail->addReplyTo('info#example.com', 'Information');
$mail->addCC('cc#example.com');
$mail->addBCC('bcc#example.com');
$mail->addAttachment('/var/tmp/file.tar.gz'); // Add attachments
$mail->addAttachment('/tmp/image.jpg', 'new.jpg'); // Optional name
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'Here is the subject';
$mail->Body = 'This is the HTML message body <b>in bold!</b>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}

Related

How to submit a contact form in wordpress using phpmailer and where to redirect it

where to put this configuration code
session_start();
require 'phpmailer/class.phpmailer.php';
require 'phpmailer/class.smtp.php';
$result="";
if(isset($_POST['submit'])){
$to = "username";
$from = "username";
$name = $_POST['name'];
$email = $_POST['email'];
$mobile = $_POST['mobile'];
$company = $_POST['company'];
$subject = $_POST['subject'];
$message = $_POST['msg'];
$msg = 'Full Name : '. $name . " " . "\n\n<br>" . 'Corporate Email : ' . $_POST['email']."\n\n<br>"
. 'Mobile : ' . $_POST['mobile']."\n\n<br>" . 'Company : ' . $_POST['company'] . "\n\n<br>" .
'Subject
: ' . $_POST['subject'] . "\n\n<br>" . 'Message : ' . $_POST['msg'];
$mail = new PHPMailer(true); // Passing `true` enables exceptions
try {
//Server settings
$mail->SMTPDebug = 1; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp.rediffmailpro.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'username'; // SMTP username
$mail->Password = 'pass'; // SMTP password
$mail->SMTPSecure = 'ssl'; // Enable TLS encryption`ssl` also accepted
$mail->Port = 465; // TCP port to connect to
//Recipients
$mail->setFrom($email, $name);
$mail->addAddress('username', 'Seema User'); // Add a recipient
// Name is optional
$mail->addReplyTo('username', 'Seema');
//Content
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = $_post['subject'];
$mail->Body = $msg;
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
if(!$mail->send())
{
$result="Something went wrong......";
}
else{
$result="Your inquiry has been submitted, our team will get in touch with you shortly.";
}
} catch (Exception $e) {
echo 'Message could not be sent. Mailer Error: ', $mail->ErrorInfo;
}
Normally we include it in that same file or can make another file to control it but wordpress has it's own rules to control it. Can anyone explain how to use phpmailer code in wordpress step by step.
I would be greatful to you if you will help me.

How to change email sender email in PHP Mailer

Currentlt it shows the mail on which phpmailer SMTP is registered.
$email='abc#email.com';
$subjectRsc="Any";
$message='Welcome';
phpmail($email, $subjectRsc, $message);
My phpmailer function:
$mail = new PHPMailer;
$mail->isSMTP();
$mail->SMTPDebug = 0;
$mail->SMTPAuth = true;
$mail->Debugoutput = 'html';
$mail->Host = 'smptp';
$mail->Port = 465; // or 587
$mail->SMTPSecure = 'ssl'; // secure transfer enabled REQUIRED for Gmail
$mail->SMTPAuth = true;
$mail->Username = PHP_MAILER_EMAIL;
$mail->Password = PHP_MAILER_PASSWORD;
$mail->AddReplyTo(REPLY_EMAIL, 'ABC');
$mail->SetFrom(FROM_EMAIL, 'ABC');
$mail->Subject = $subject;
$address = $to;
$mail->AddAddress($address);
$mail->MsgHTML($message);
//send the message, check for errors
if (!$mail->send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
return true;
}
This is how I am sending mails to the users I want to show the specific mail of my website to be displayed in the mails not that on which smtp server is registered.
You should try this :
$mail = new PHPMailer;
$mail->isSMTP();
$mail->SMTPDebug = 0;
$mail->SMTPAuth = true;
$mail->Debugoutput = 'html';
$mail->Host = 'smptp';
$mail->Port = 465; // or 587
$mail->SMTPSecure = 'ssl'; // secure transfer enabled REQUIRED for Gmail
$mail->SMTPAuth = true;
$mail->Username = PHP_MAILER_EMAIL;
$mail->Password = PHP_MAILER_PASSWORD;
$mail->addReplyTo('myemail#example.com', 'ABC'); //<-- this is the line i changed
$mail->From= "myemail#example.com"; //<-- this is the line i changed
$mail->Subject = $subject;
$address = $to;
$mail->AddAddress($address);
$mail->MsgHTML($message);
//send the message, check for errors
if (!$mail->send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
return true;
}
According to phpmailer documentation this is the field to add the sender's email.
please try using this code :
<?php
require_once "vendor/autoload.php";
//PHPMailer Object
$mail = new PHPMailer;
//From email address and name
$mail->From = "from#yourdomain.com";
$mail->FromName = "Full Name";
//To address and name
$mail->addAddress("test#gmail.com", "Test");
$mail->addAddress("test1#gmail.com");
//Address to which recipient will reply
//if you want to add CC and BCC
$mail->addCC("cc#example.com");
$mail->addBCC("bcc#example.com");
//Send HTML or Plain Text email
$mail->isHTML(true);
$mail->Subject = "Subject Text";
$mail->Body = "<i>Mail body in HTML</i>";
if(!$mail->send())
{
echo "Mailer Error: " . $mail->ErrorInfo;
}
else
{
echo "Message has been sent successfully";
}
?>
Please try using this code:
<?php
$to = "test#gmail.com";
$subject = "This is subject";
$message = "<b>This is HTML message.</b>";
$message .= "<h1>This is headline.</h1>";
$header = "From:test1#gmail.com \r\n";
$header .= "Cc:test2#gmail \r\n";
$header .= "MIME-Version: 1.0\r\n";
$header .= "Content-type: text/html\r\n";
$retval = mail ($to,$subject,$message,$header);
if( $retval == true ) {
echo "Message sent successfully...";
}else {
echo "Message could not be sent...";
}
?>

PHP Mailer languages encoding issues: It accepts only English

I am using php mailer at my website contact form. When i receive a message in greek language, i don't receive the text as typed in the contact form. In class.phpmailer.php file line 59 the encoding is public $CharSet = 'iso-8859-1'; Is there a way to make my text appear correctly as typed in the contact form?
Languages comonly supported by ISO/IEC 8859-1 can be found here
I have also tried german and albanian languages but i also have the same problem. I can only receive english, if the user types another language on some words i receive "chinese".
I get this message:
The code:
<?php
require_once('phpmailer/class.phpmailer.php');
// if(isset($_POST['g-recaptcha-response'])){
if (empty($_POST['Email'])) {
$_POST['Email'] = "-";
}
if (empty($_POST['Name'])) {
$_POST['Name'] = "-";
}
if (empty($_POST['Subject'])) {
$_POST['Subject'] = " ";
}
if (empty($_POST['message'])) {
$_POST['message'] = "-";
}
$mymail = smtpmailer("example#gmail.com", $_POST['Email'], $_POST['Name'],
$_POST['Subject'], $_POST['message']);
function smtpmailer($to, $from, $from_name, $subject, $body)
{
$mail = new PHPMailer;
$mail->isSMTP();
$mail->Debugoutput = 'html';
$mail->Host = 'smtp.gmail.com';
$mail->Port = 587;
$mail->SMTPSecure = 'tls';
$mail->SMTPAuth = true;
$mail->Username = 'example#gmail.com';
$mail->Password = 'pass';
$mail->SetFrom($from, $from_name);
$mail->Subject = " Contact form ~Name: $from_name ~ subject: $subject ";
$mail->Body = " You have received a new message
from $from_name, here are the details:\n\n_____
___________________\n" . "\nDear $from_name,\n
Your enquiry had been received on " . date("D j F ") . "
\nINFORMATION SUBMITTED: " . "\n\nName: $from_name\n\nEmail: $from
\nSubject: $subject\n\nMessage: $body \n\nTo:
$to\nDate: " . date("d/m/y") . "\nWebsite: " . "\n____________
__________________"; //end body
$mail->AddAddress($to);
//send the message, check for errors
if (!$mail->send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Well done $from_name, your message has been sent!\n
We will reply to the following email: $from" . "\nYour Message: $body";
}
} //end function smtpmailer
//}
?>
In your example output, the char count amplification suggests that you're receiving data from your form in UTF-8, but are then telling PHPMailer (by default) that it's ISO-8859-1, which results in the kind of corruption you're seeing.
You should be using UTF-8 everywhere. In PHPMailer you do it like this:
$mail->CharSet = 'UTF-8';
Then you need to be sure that every step of your processing supports UTF-8 as well.

PHP SMTP Custom contact form

I have this form, that send fine locally, but when i upload it to hostgator i get the following error message
Mailer Error: Could not instantiate mail function.
my php code is
<?php
if(isset($_POST['submit'])){
require 'PHPMailer/PHPMailerAutoload.php';
// Send mail
$mail = new PHPMailer();
// Data received from POST request
$name = stripcslashes($_POST['tbName']);
$emailAddr = stripcslashes($_POST['tbEmail']);
$company = stripcslashes($_POST['tbCompany']);
$comment = stripcslashes($_POST['taMessage']);
$subject = stripcslashes($_POST['tbSubject']);
// SMTP Configuration
$mail->SMTPAuth = true;
$mail->Host = "gator3209.hostgator.com"; // SMTP server
$mail->Username = "****#*****.com";
$mail->Password = "***********";
$mail->SMTPSecure = 'tls';
$mail->Port = 25;
$mail->AddAddress('****#*****.com');
$mail->From = "****#*****.com";
$mail->FromName = "Website Contact Form - " . $name;
$mail->Subject = $subject;
$mail->Body = 'This is the HTML message body <b>in bold!</b>';
$mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
$mail->MsgHTML("Name:" . $name . "<br /><br />Email:" . $emailAddr. "<br /><br />Company:" . $company. "<br /><br />Subject:" . $subject. "<br /><br />" . $comment);
$message = NULL;
if(!$mail->Send()) {
$message = "Mailer Error: " . $mail->ErrorInfo;
} else {
$message = "Message sent!";
}
}
?>
I have spoken to my host and they said its not within there support to deal with these things, but said my port and host are correct.
So now I'm rather confused. is there anything obvious I'm missing?
Just to let you know if anyone finds this, my problem was i was missing $mail->IsSMTP(); from my config.
the SMTP config section should be as follows
<?php
if(isset($_POST['submit'])){
require 'PHPMailer/PHPMailerAutoload.php';
// Send mail
$mail = new PHPMailer();
// Data received from POST request
$name = stripcslashes($_POST['tbName']);
$emailAddr = stripcslashes($_POST['tbEmail']);
$company = stripcslashes($_POST['tbCompany']);
$comment = stripcslashes($_POST['taMessage']);
$subject = stripcslashes($_POST['tbSubject']);
// SMTP Configuration
$mail->SMTPAuth = true;
$mail->IsSMTP();
$mail->Host = "gator3209.hostgator.com"; // SMTP server
$mail->Username = "****#*****.com";
$mail->Password = "***********";
$mail->SMTPSecure = 'tls';
$mail->Port = 25;
$mail->AddAddress('****#*****.com');
$mail->From = "****#*****.com";
$mail->FromName = "Website Contact Form - " . $name;
$mail->Subject = $subject;
$mail->Body = 'This is the HTML message body <b>in bold!</b>';
$mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
$mail->MsgHTML("Name:" . $name . "<br /><br />Email:" . $emailAddr. "<br /><br />Company:" . $company. "<br /><br />Subject:" . $subject. "<br /><br />" . $comment);
$message = NULL;
if(!$mail->Send()) {
$message = "Mailer Error: " . $mail->ErrorInfo;
} else {
$message = "Message sent!";
}
}
?>

PHPMailer error(s)

function register_contact ($person = array()) {
$nogood = false;
foreach ($person as $val) {
if (strlen($val)==0) {
$nogood = true;
$status = "There was an error sending the registration please fill in all fields";
}
}
if (!$nogood) {
require_once("class.phpmailer.php");
$message = "New request for Fox In Touch Recipient:.\r\n\r\n";
$message .= "Forename: " . $person['fname'];
$message .= "\r\nSurname: " . $person['sname'];
$message .= "\r\nEmail: " . $person['email'];
$message .= "\r\nJob Title: " . $person['job'];
$message .= "\r\nCompany: " . $person['company'];
$message .= "\r\n\r\nFox In Touch.";
$mail = new PHPMailer();
$mail->IsSMTP(); // send via SMTP
$mail->Host = "ahost"; // SMTP servers
$mail->SMTPAuth = true; // turn on SMTP authentication
$mail->Username = "name"; // SMTP username
$mail->Password = "pass"; // SMTP password
//$mail->Post = 587;
$mail->From = "foxintouch#bionic-comms.co.uk";
$mail->FromName = "Fox In Touch";
//$mail->AddAddress("foxlicensing.europe#fox.com", "Fox Licensing");
$mail->AddAddress("andrew#yahoo.co.uk", "Andrew");
$mail->AddReplyTo("foxintouch#bionic-comms.co.uk","Information");
$mail->IsHTML(false); // send as HTML
$mail->Subject = "Contact request for Fox In Touch!";
$mail->Body = $message;
if(!$mail->Send()) {
$nogood = true;
$status = "Message was not sent <p>";
$status .= "Mailer Error: " . $mail->ErrorInfo;
} else {
$status = "Thank you! Your message has been sent to 20th Century Fox. Submit another?";
}
}
return array('email_failed'=>$nogood, 'status'=>$status);
}
The above code keeps giving me the error, "Mailer Error: Language string failed to load: recipients_failedandrew#yahoo.co.uk". I have tried changing the AddAddress(). The smtp connection settings are correct, as this was the last error i had! Any help would be much appreciated. Thanks
It sounds like you have two problems.
1) Your language file isn't being loaded - see installation
2) The recipient is being rejected - errr double check the SMTP settings and the recipient address

Categories