Customize Body of SMTP Mail Script - php

I have a basic smtp mail script that works great but the only thing that i need is to customize body of mail. I tried to define and insert values into the script but somewhere the code gives me error. Where I'm doing wrong?
<?
require("class.phpmailer.php");
$mail = new PHPMailer();
$mail->IsSMTP(); // send via SMTP
$mail->Host = "mail.host.com"; // SMTP servers
$mail->SMTPAuth = true; // turn on SMTP authentication
$mail->Username = "info#host.com"; // SMTP username
$mail->Password = "123456"; // SMTP password
$mail->From = "info#host.com";
$mail->Fromname = "John Doe";
$mail->AddAddress("info#gmail.com","John Doe");
$mail->Subject = $_POST['content'];
$mail->Body = (".$arrival_date.", ".$arrival_pickup_location.", ".$arrival_dropoff_location.", ".$arrival_flight_number.", ".$arrival_fligth_time.", ".$arrival_dropoff_adress.", ".$phone_number.", ".$reservation_name.", ".$email_adress.", ".$country.", ".$additional_requests.");
$arrival_date = $_POST['arrival_date'];
$arrival_pickup_location = $_POST['arrival_pickup_location'];
$arrival_dropoff_location = $_POST['arrival_dropoff_location'];
$arrival_flight_number = $_POST['arrival_flight_number'];
$arrival_fligth_time = $_POST['arrival_fligth_time'];
$arrival_dropoff_adress = $_POST['arrival_dropoff_adress'];
$reservation_name = $_POST['reservation_name'];
$phone_number = $_POST['phone_number'];
$email_adress = $_POST['email_adress'];
$passenger = $_POST['passenger'];
$country = $_POST['country'];
$additional_requests = $_POST['additional_requests'];
if(!$mail->Send())
{
echo "Mesagge not delivered <p>";
echo "Mailer Error: " . $mail->ErrorInfo;
exit;
}
echo "Message sent";
?>

You're setting the variables after you're trying to set the $mail->Body object with them.
Try:
<?php
$arrival_date = $_POST['arrival_date'];
$arrival_pickup_location = $_POST['arrival_pickup_location'];
$arrival_dropoff_location = $_POST['arrival_dropoff_location'];
$arrival_flight_number = $_POST['arrival_flight_number'];
$arrival_fligth_time = $_POST['arrival_fligth_time'];
$arrival_dropoff_adress = $_POST['arrival_dropoff_adress'];
$reservation_name = $_POST['reservation_name'];
$phone_number = $_POST['phone_number'];
$email_adress = $_POST['email_adress'];
$passenger = $_POST['passenger'];
$country = $_POST['country'];
$additional_requests = $_POST['additional_requests'];
$message = '<ul><li>' . $arrival_date . "</li>";
$message .= '<li>' . $arrival_pickup_location . "</li>";
$message .= '<li>' . $arrival_dropoff_location . "</li>";
$message .= '<li>' . $arrival_flight_number . "</li>";
$message .= '<li>' . $arrival_fligth_time . "</li>";
$message .= '<li>' . $arrival_dropoff_adress . "</li>";
$message .= '<li>' . $phone_number . "</li>";
$message .= '<li>' . $reservation_name . "</li>";
$message .= '<li>' . $email_adress . "</li>";
$message .= '<li>' . $country . "</li>";
$message .= '<li>' . $additional_requests . "</li></ul>";
require("class.phpmailer.php");
$mail = new PHPMailer();
$mail->IsSMTP(); // send via SMTP
$mail->Host = "mail.host.com"; // SMTP servers
$mail->SMTPAuth = true; // turn on SMTP authentication
$mail->Username = "info#host.com"; // SMTP username
$mail->Password = "123456"; // SMTP password
$mail->IsHTML(true);
$mail->From = "info#host.com";
$mail->Fromname = "John Doe";
$mail->AddAddress("info#gmail.com","John Doe");
$mail->Subject = $_POST['content'];
$mail->Body = $message;
if(!$mail->Send())
{
echo "Mesagge not delivered <p>";
echo "Mailer Error: " . $mail->ErrorInfo;
exit;
}
echo "Message sent";
?>
Also, I don't think that using $mail->AddAddress = ("info#gmail.com","John Doe"); would work, not sure ...
.. perhaps it does though! The way above in my answer definitely should work though xD!

What's the error that you're getting? Where did it start producing the error?
$mail->AddAddress("info#gmail.com","John Doe") doesn't have an "=" sign.
so it should read:
$mail->AddAddress = ("info#gmail.com","John Doe");
also, you can add html to the body so it reads a little nicer:
$mail->Body = "<html><body>";
$mail->Body .= "<table><tbody>";
$mail->Body .= "<tr><td>".$arrival_date."</td></tr>";
$mail->Body .= "</tbody></table>";
$mail->Body .= "</body></html>";

Related

send email on localhost with PHP

I want to send an email on localhost but don't really know how to do it.
I tried some different ways but it doesn't work.
I used PHPMailer https://github.com/PHPMailer/PHPMailer/tree/5.2-stable as the mailserver but I think thats maybe wrong implemented or so.
Don't know if it's important but I use MAMP.
This is what I currently have:
<?php
if (isset($_POST['submit'])) {
require("PHPMailer/PHPMailerAutoload.php");
ini_set("SMTP","ssl://smtp.gmail.com");
ini_set("smtp_port","465");
$mail = new PHPMailer();
$mail->SMTPAuth = true;
$mail->Host = "smtp.gmail.com";
$mail->SMTPSecure = "ssl";
$mail->SMTPAuth = true;
$mail->Username = "mail account";
$mail->Password = "password for account";
$mail->Port = "465";
$mail->setFrom('receiver mail', 'TEST');
$mail->addReplyTo('receiver mail', 'TEST');
$mail->addAddress('recipient mail');
$mail->Port = "465";
$mail->isHTML(true);
$mail->Subject = "test";
// get text from input fields
$email = $_POST['email'];
$name = $_POST['name'];
$address = $_POST['address'];
$city = $_POST['city'];
$number = $_POST['number'];
$textarea = $_POST['textarea'];
$bodyContent =
"<p>Name: " . $name . "</p>
<p>E-Mail: " . $email . "</p>
<p>Telefonnummer: " . $number . "</p>
<p>Adresse: " . $address . $city . "</p>
<p>Anliegen: " . $textarea . "</p>";
$mail->Body = $bodyContent;
if (!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error' . $mail->ErrorInfo;
} else {
echo 'Message has been sent.';
}
}
?>
Comment out the following two lines :
// ini_set("SMTP","ssl://smtp.gmail.com");
// ini_set("smtp_port","465");
And add the following under the line with $mail = new PHPMailer();
$mail->isSMTP();
And it will work, I have tried it on my laptop on XAMPP.
Okay, i finally found the solution. I updated my code to this
<?php
if (isset($_POST['sendButton'])) {
require("PHPMailer/PHPMailerAutoload.php");
require 'PHPMailer/class.phpmailer.php';
$mail = new PHPMailer();
$mail->isSMTP();
$mail->SMTPAuth = true;
$mail->Host = "smtp.gmail.com";
$mail->SMTPSecure = "tls";
$mail->SMTPAuth = true;
$mail->Username = "secret";
$mail->Password = "secret";
$subject = utf8_decode('test');
$mail->setFrom('secret', $subject);
$mail->addReplyTo('secret', $subject);
$mail->addAddress('secret');
$mail->Subject = utf8_decode('test');
$mail->Port = "587";
$mail->isHTML(true);
$email = $_POST['email'];
$name = $_POST['name'];
$address = $_POST['address'];
$city = $_POST['city'];
$number = $_POST['number'];
$sendText = $_POST['sendText'];
$bodyContent =
"<p>Name: " . $name . "</p>
<p>E-Mail: " . $email . "</p>
<p>Telefonnummer: " . $number . "</p>
<p>Adresse: " . $address . ' ' . $city . "</p>
<p>Anliegen: " . $sendText . "</p>";
$mail->Body = $bodyContent;
}
?>
Besides I had to go to myaccount.google.com -> "Sign-in & security" -> "Apps with account access", and turn "Allow less secure apps" to "ON"
Now everything is fine.
Thank you for your help guys

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!";
}
}
?>

PHP attach audio file in email

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';
}

How to send multiple emails in PHP and MySQL via phpmailer

I have a problem. When i click send button, just zxc#hotmail.com receives the message.
How to change it? I'm use Gmail SMTP send out.
There are 2 records in the database:
Here is my code:
include "phpmailer/class.phpmailer.php";
$host = "localhost";
$user = "root";
$pass = "root";
$db = "mailTest";
mysql_connect($host, $user, $pass);
mysql_select_db($db);
$query = "SELECT email FROM list";
$recordset = mysql_query($query);
$row_recordset = mysql_fetch_assoc($recordset);
$tota_row_recordset = mysql_num_rows($recordset);
$msg = strip_tags($_POST['msg']);
$mail= new PHPMailer(); //建立新物件
while($row = mysql_fetch_array($recordset))
{
$to = $row['email'];
$mail->AddAddress($to, "Test Message");
}
$mail->IsSMTP();
$mail->SMTPAuth = true;
$mail->SMTPSecure = "ssl";
$mail->Host = "smtp.gmail.com";
$mail->Port = 465;
$mail->CharSet = "big5";
$mail->Subject = "Test Message";
$mail->Username = "xxxxxx";
$mail->Password = "xxxxxx";
$mail->Body = "$msg";
$mail->IsHTML(true);
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
header('Location: index.php');
}
With this loop you overwrite the recipient with each iteration, so only the last one stays to receive the email:
while($row = mysql_fetch_array($recordset))
{
$to = $row['email'];
}
$mail = new PHPMailer();
Change your code to
$mail = new PHPMailer(); // create object FIRST
while($row = mysql_fetch_array($recordset))
{
$to = $row['email'];
$mail->AddAddress($to, "Test Message"); // add each DB entry to list of recipients
}
try this below:
while($row = mysql_fetch_array($recordset))
{
$to = $row['email'];
$mail= new PHPMailer();
$mail->IsSMTP();
$mail->SMTPAuth = true;
$mail->SMTPSecure = "ssl";
$mail->Host = "smtp.gmail.com";
$mail->Port = 465;
$mail->CharSet = "big5";
$mail->Subject = "Test Message";
$mail->Username = "xxxxxx";
$mail->Password = "xxxxxx";
$mail->Body = "$msg";
$mail->IsHTML(true);
$mail->AddAddress($to, "Test Message");
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
}
}
if(isset($_POST['sbmit']))
{
for($i=0;$i<count($_POST['email_address']);$i++)
{
$to = $_POST['email_address'][$i];
echo $to."<br />";
$subject = 'Thank you for Subscribing';
$message = $_POST['news'];
$headers = 'From: dhakalneil#gmail.com' . "\r\n" .
'Reply-To: xyz#hotmail.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
echo $_POST['email_address'][$i]."<br />";
ini_set("SMTP","smtp.ntc.net.np" );
ini_set('sendmail_from', 'dhakalneil#gmail.com');
if( mail($to,$subject,$message,$headers))
{
echo "Mail has been sent to admin<br />";
}
else
{
echo "Mail could not been 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