I'm trying to configure Swift Mailer to send form data to my email account
In short: I have a website, using which I am going to sell some stuff in my local city
There are 3 simple forms that appear when 'Buy' button is active
The point is to send data, filled in these forms directly to my email account,
Being a newbie to PHP, I have tried for several days to make this simple script work, but it ended up a failure
here is the code of mail.php:
<?php
$name=$_POST["name"];
$email=$_POST["email"];
$phone=$_POST["phone"];
if (isset ($name))
{
$name = substr($name,0,30);
if (empty($name))
{
echo "<center><b>Invalid name<p>";
echo "<a href=index.php>Come back and fill up the form properly</a>";
exit;
}
}
else
{
$name = "Invalid name";
}
if (isset ($email))
{
$email = substr($email,0,30); //Не может быть более 20 символов
if (empty($email))
{
echo "<center><b>Email Invalid!<p>";
echo "<a href=index.php>Try again</a>";
exit;
}
}
else
{
$email = "Invalid";
}
if (isset ($phone))
{
$phone = substr($phone,0,15);
if (empty($phone))
{
echo "<center><b>Empty message!!!<p>";
echo "<a href=index.php>Try again</a>";
exit;
}
}
else
{
$phone = "invalid";
}
$i = "invalid";
if ($name == $i AND $email == $i AND $phone == $i)
{
echo "Error! No data was transferred to the script";
exit;
}
$to = "zakaz#playpad2.ru";
$subject = "New order ";
$message = "Name:$name::::::::::Email:$email::::::::::Phone No:$phone:::::::::;
require_once 'lib/swift_required.php';
$transport = Swift_SmtpTransport::newInstance('smtp.yandex.ru', 465, 'ssl')
->setUsername('zakaz#playpad2.ru')
->setPassword('jhfy;tdjtujdyj');
$mailer = Swift_Mailer::newInstance($transport);
// Create a message
$message = Swift_Message::newInstance('Wonderful Subject')
->setFrom(array('kirabidzu#gmail.com' => 'PlayPad 2 Kids tablet order'))
->setTo(array('zakaz#playpad2.ru', 'zakaz#playpad2.ru' => 'A name'))
->setBody('Name:$name::::::::::Email:$email::::::::::Phone No:$phone:::::::::');
// Send the message
$numSent = $mailer->send($message);
if (!$numSent) {
print "Can't send a message";
}
echo "<center><b>Thank you<a href=index.php>Proceed</a> to continue...>";
exit;
?>"
here is a form, located on the main page:
<form name="form" action="mail.php" method="POST"><b>Name here </b>
<br> <input type="text" size="70%" name="name" maxlength="45"> <br>
<b>Phone No </b>
<br> <input type="text" size="70%" name="phone" maxlength="15"> <br>
<b>E-Mail </b>
<br> <input type="text" size="70% " name="email" maxlength="45"> <br>
<input type="submit"><div class="button"></div>
Please help!
Changed smtp server port, and a new error appears:
PHP Fatal error: Uncaught exception 'Swift_TransportException' with message 'Expected response code 250 but got code "553", with message "553 5.7.1 Sender address rejected: not owned by auth user.\r\n"'
A can't understand why do I need "From" field if the message is sent from the site?? The person just fills up the form and I wnat to get an email with the data he or she filled in? What should I write in the "From" field?
UPD: Finally emails are sent!!! Just made 'From' and 'To' email addresses equal
Thanks to all of you for help
Looks like you are missing a closing double quote on this line:
$message = "Name:$name::::::::::Email:$email::::::::::Phone No:$phone:::::::::;
Should be:
$message = "Name:$name::::::::::Email:$email::::::::::Phone No:$phone:::::::::;";
Also, you shouldn't have posted your mail account information. It's time to change your account's password.
Check this. Your using The SMTP server of smtp.yandex.ru
$transport = Swift_SmtpTransport::newInstance('smtp.yandex.ru', 465, 'ssl')
And the sender address is from gmail
->setFrom(array('kirabidzu#gmail.com' => 'PlayPad 2 Kids tablet order'))
There are servers that check that the sender address correspond to a really user. And this looks like your problem. To solve the error I think that you have two options.
a) you can set the smtp of gmail:
$transport = Swift_SmtpTransport::newInstance('smtp.gmail.com', 465, 'ssl');
b) take a really sender address of yandex.ru
I hoope that this help you.
Related
I'm trying to send a mail through PHP mailer, but I'm getting this error. anyone know how to fix it, it tried multiple times but failed
Something went wrong :(
PHPMailer\PHPMailer\Exception: SMTP Error: The following recipients failed: mohamedanushkar#gmail.com: "Your IP: 68.65.121.178 : Your domain gmail.com is not allowed in header From" in /home/sevnowsc/howmuch.sevnstaging.website/wp/PHPMailer-master/src/PHPMailer.php:1820 Stack trace: #0 /home/sevnowsc/howmuch.sevnstaging.website/wp/PHPMailer-master/src/PHPMailer.php(1513): PHPMailer\PHPMailer\PHPMailer->smtpSend('Date: Thu, 8 Oc...', '\r\n\r...') #1 /home/sevnowsc/howmuch.sevnstaging.website/wp/PHPMailer-master/src/PHPMailer.php(1352): PHPMailer\PHPMailer\PHPMailer->postSend() #2 /home/sevnowsc/howmuch.sevnstaging.website/wp/php/form.php(75): PHPMailer\PHPMailer\PHPMailer->send() #3 {main}
this is my PHP code. thank you in advance
<?php
$errorMSG = "";
// FIRST NAME
if (empty($_POST["name"])) {
$errorMSG = "Name is required ";
} else {
$name = $_POST["name"];
}
// EMAIL
if (empty($_POST["email"])) {
$errorMSG .= "Email is required ";
} else {
$email = $_POST["email"];
}
$message = $_POST["message"];
$EmailTo = "mohamedanushkar#gmail.com";
$reference_id = uniqid();
$Subject = "Howmuch" . $reference_id;
$Body = "<html>
<head>
<title></title>
</head>
<body>
<div style='border: 5px solid #141348; padding:20px;'>
<h2>Person's details</h2>
<p> Full Name: $name </p>
<p>Email: $email </p>
<p>Address: $message</p>
</body>
</html>";
use PHPMailer\PHPMailer\Exception;
use PHPMailer\PHPMailer\PHPMailer;
require '../PHPMailer-master/src/Exception.php';
require '../PHPMailer-master/src/PHPMailer.php';
require '../PHPMailer-master/src/SMTP.php';
$mail = new PHPMailer(true);
try {
//Server settings
$mail->SMTPDebug = -1;
$mail->isSMTP();
$mail->Host = 'sevnstaging.website';
$mail->SMTPAuth = true;
$mail->Username = 'info#sevnstaging.website';
$mail->Password = 'xxxxxxxxxx';
$mail->SMTPSecure = 'ssl';
$mail->Port = 465;
//Recipients
$mail->setFrom('mohamedanushkar#gmail.com','How much');
$mail->addAddress($EmailTo);
$mail->addReplyTo($email);
// $mail->addCC($email);
//Content
$mail->isHTML(true);
$mail->Subject = $Subject;
$mail->Body = $Body;
$mail->AltBody = '';
$mail->send();
echo "<script type='text/javascript'>window.location.href = '../thank-you.php';</script>";
} catch (Exception $e) {
if ($errorMSG == "") {
echo "Something went wrong :( <br>";
echo $e;
} else {
echo $errorMSG;
}
}
?>
This is because the mail server at sevnstaging.website is not permitted to send email from gmail.com addresses, as it says:
Your domain gmail.com is not allowed in header From
Gmail.com has this SPF record:
v=spf1 include:_netblocks.google.com include:_netblocks2.google.com include:_netblocks3.google.com ~all
The netblocks this refers to do not include the server at sevnstaging.website, so this would result in delivery problems. The rejection itself may be happening for three reasons:
sevnstaging.website may only allow you to use a from address from its own domain, or using your username (info#sevnstaging.website)
sevnstaging.website may check the SPF record at gmail.com to see whether it is permitted to send from a gmail.com address, see a failure, and reject your sending attempt
It may simply deny sending using gmail.com from addresses via its own heuristics.
Generally speaking, if you want to send form a gmail.com address, you need to send through a gmail.com mail server.
Also bear in mind that gmail is relatively relaxed about this - other domains (such as all Yahoo domains) are far stricter.
This question already has answers here:
How do you make sure email you send programmatically is not automatically marked as spam?
(24 answers)
Closed 4 years ago.
I have a simple website set up on godaddy, decided to add emails straight to me instead of through godaddy, started using the phpmailer and I only receive emails from users submitting on my website form if they send an email with gmail. All other emails get ignored. Wonder if it's a godaddy issue.
$connect = mysqli_connect('localhost', 'pf', '*********', 'bs-portf');
if(mysqli_connect_errno()){
echo "Failed to connect" . mysqli_connect_error();
}
if(mysqli_ping($connect)){
// echo "we are connected";
}else{
echo "Error: ". mysqli_error($connect);
die("Connection failed");
}
?>
<?php
if(isset($_POST['submit'])){
require 'phpmailer/PHPMailerAutoload.php';
$mail = new PHPmailer();
$name = $_POST['name'];
$email = $_POST['email'];
$message = nl2br($_POST['message']); //nl2br() new lines in text message to break tags
$mail->Host='smtp.gmail.com';
// $mail->isSMTP();
$mail->Port=465;
$mail->SMTPAuth=true;
$mail->SMTPsecure='ssl';
$mail->Username='bradv#gmail.com';
$mail->Password='**********';
$mail->setFrom($email, $name);
$mail->addAddress('bradv#gmail.com');
$mail->addReplyTo('info#example.com', 'Information');
$mail->isHTML(true);
$mail->Subject='Form Submission';
$mail->Body= "test email body 1";
// $mail->Body='<p align=center>Name :'.$name. '<br>Email: '.$email.
'<br>Message '.$message. '</p>';
// echo "submit worked";
if($mail->send()){
echo 'mail is sent';
// $result ="Something went wrong. Please try again.";
}else{
echo 'email failed';
// $result="Thanks " .$name. " for contacting us. We'll get back to you
soon!";
}
Check the spam folder. If you send an email using one address as login but sets the From address as another, Gmail will think its spam.
Set from address with the same email as the login and replyto as the user's email.
I have some PHP code that I'm using to send email to a specific e-mail address. However, I'd like to include a couple more e-mail addresses in the PHP for when it sends it.
when i tried it showing the following
ERROR:Mailer Error: You must provide at least one recipient email address.
code
include "class.phpmailer.php"; // include the class file name
$mail = new PHPMailer(); // create a new object
$mail->IsSMTP(); // enable SMTP
$mail->SMTPDebug = 1; // debugging: 1 = errors and messages, 2 = messages only
$mail->SMTPAuth = true; // authentication enabled
//$mail->SMTPSecure = 'ssl'; // secure transfer enabled REQUIRED for GMail
$mail->Host = "mail.authsmtp.com";
$mail->Port = "25"; // or 587
$mail->IsHTML(true);
$mail->Username = "xxxxx";
$mail->Password = "xxxxx";
$mail->SetFrom("cpn#xxxxxxx.com");
$mail->Subject = $sub1;
$mail->Body = $text_mail;
$mail->AddAddress("xxxxxxx#gmail.com;aral#xxxxxx.com;akhader#xxxxxx.com");
if(!$mail->Send()){
echo "Mailer Error: " . $mail->ErrorInfo;
}
else{
echo "Message has been sent";
}
any one guide me how to do it
Change this line
$mail->AddAddress("xxxxxxx#gmail.com;aral#xxxxxx.com;akhader#xxxxxx.com");
to this:
$mail->AddAddress("xxxxxxx#gmail.com");
$mail->AddAddress("aral#xxxxxx.com");
$mail->AddAddress("akhader#xxxxxx.com");
You can run that function as many times as you like until you've got all the addresses you need.
See Here for more info
It seems that you are miss using the AddAdress method. You should pass every mail separatly like that :
$mail->AddAddress("xxxxxxx#gmail.com");
$mail->AddAddress("aral#xxxxxx.com");
$mail->AddAddress("akhader#xxxxxx.com");
See PHPMailer AddAddress() for more details.
If you want to send email to multiple address, You need to call the AddAddress() function for each and every Email address. First parameter is EMAIL address, Second one is Recipient name and it is optional.
$mail->AddAddress("xxxxxxx#gmail.com", "XXXXXXXX");
$mail->AddAddress("aral#xxxxxx.com", "Aral");
Try this
$mail->AddAddress("xxxxxxx#gmail.com");
$mail->AddAddress("aral#xxxxxx.com");
$mail->AddAddress("akhader#xxxxxx.com");
Take a look here PHPMailer AddAddress()
and keep an eye of your line:
$mail->AddAddress("xxxxxxx#gmail.com;aral#xxxxxx.com;akhader#xxxxxx.com");
Instead of:
$mail->AddAddress("xxxxxxx#gmail.com;aral#xxxxxx.com;akhader#xxxxxx.com");
You should use
$mail->AddAddress('xxxxxxx#gmail.com', '1');
$mail->AddAddress('aral#xxxxxx.com', '2');
$mail->AddAddress('akhader#xxxxxx.com', '3');
Or use CC copies:
$mail->AddCC('xxxxxxx#gmail.com', '1');
$mail->AddCC('aral#xxxxxx.com', '2');
$mail->AddCC('akhader#xxxxxx.com', '3');
Try this.... This may help you
<html>
<body>
<?php
if (isset($_REQUEST['email']))
//if "email" is filled out, send email
{
//send email
$email = $_REQUEST['email'] ;
$subject = $_REQUEST['subject'] ;
$message = $_REQUEST['message'] ;
mail("chauhanamrish32#gmail.com,amrinderpuri1990#gmail.com,amrinder_puri#yahoo.com,amrindersinghpuri13#gmail.com", $subject,$message, "From:" . $email);
echo "Thank you for using our mail form";
}
else
//if "email" is not filled out, display the form
{
echo "<form method='post' action='mail.php'>
Email: <input name='email' type='text'><br>
Subject: <input name='subject' type='text'><br>
Message:<br>
<textarea name='message' rows='15' cols='40'>
</textarea><br>
<input type='submit'>
</form>";
}
?>
Someone built this site for me and I have no idea about php and which part I should fix :S
Basically I have this single text entry form on my site which users can enter in their email address to that form and once they send submit I'll get the email address they input in.
The dev used phpmailer and at the moment when someone submitted their email address, I don't get any of their email address. The only entry/ email content I get is from "me" and a content of "Thank you. We'll update you as soon as possible."
This is the sendmail.php on the root folder:
<?php
$from = "ask#michaelfrieda2014.com";
if (isset($_REQUEST['email']) && $_REQUEST['email'] != '')
{
$email = $_REQUEST['email'];
sendMailSingup($from, $email);
sendMailSingup($from, "ask#michaelfrieda2014.com");
}
function sendMailSingup($from, $to)
{
include_once('phpmailer/class.phpmailer.php');
$mail = new PHPMailer();
$body = "Thank you. We'll update you a soon as possible.";
$mail->From = "$from";
$mail->FromName = "";
$mail->Subject = "Thank you!";
$mail->AltBody = "Thank you!"; // optional, comment out and test
$mail->MsgHTML($body);
$mail->AddAddress("$to", "");
//$mail->AddAttachment("images/phpmailer.gif"); // attachment
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
#echo "Message sent!";
echo 1;
}
}
How do I make it so that I get the important information? (Their email address sent to my email). Am I missing something?
Thanks!
Below this line...
$body = "Thank you. We'll update you a soon as possible.";
add:
$body .= "<br><br>Sent from: $to";
(note the .= -- this will append to the $body variable)
If you want to fix the reply-to address, add this line:
$mail->AddReplyTo($to, $to);
before:
$mail->From = "$from";
i have one contact form, when user submit all value will send(email) to admin.But now i want to do when user submit admin will receive the email and user also will receive an email but with different body.
here my previous code :
<?php
if(md5($verif_box).'a4xn' == $_COOKIE['tntcon']){
$name= $_POST["name"];
$email= $_POST["email"];
$phone= $_POST["phone"];
$company= $_POST["company"];
$message= $_POST["message"];
require_once('lib/class.phpmailer.php');
$mail = new PHPMailer(); // defaults to using php "mail()"
$mail->AddReplyTo("admin#gmail.com","I Concept");
$mail->SetFrom('admin#gmail.com', 'I Concept');
$mail->AddReplyTo("admin#gmail.com","I Concept");
$address = "admin#gmail.com";
$mail->AddAddress($address, "I Concept");
$mail->Subject = "MY - Request a Quote";
$mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
$mail->Body = "<strong>Request a Quote from I Concept Malaysia Website</strong><br><br>
Name : $name<br>
Email : $email<br>
Phone : $phone<br>
Company : $company<br>
Enquiry : $message<br> <br>
Thank You!<br>
";
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!<br>";
}
}
?>
Try the following. Didn't test but you basically need to get another PHPMailer object going and set the body and to information separately.
$address = "admin#gmail.com";
$mail->Subject = "MY - Request a Quote";
// keeps the current $mail settings and creates new object
$mail2 = clone $mail;
// mail to admin
$mail->AddAddress($address, "I Concept");
$mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
$mail->Body = "<strong>Request a Quote from I Concept Malaysia Website</strong><br><br>
Name : $name<br>
Email : $email<br>
Phone : $phone<br>
Company : $company<br>
Enquiry : $message<br> <br>
Thank You!<br>";
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!<br>";
}
// now send to user.
$mail2->AddAddress($email, $name);
$mail2->AltBody = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
$mail2->Body = "Separate email body for user filling form out.";
if(!$mail2->Send()) {
echo "Mailer Error: " . $mail2->ErrorInfo;
} else {
echo "Message sent!<br>";
}
Cloning the PHPmailer object is not necessary. Just use the ClearAllRecipients method that is built into PHPmailer before changing the body and sending the second email.
I'm sure you can't send different bodies in one SMTP call. However you can just send the first email and initiate a new PHPMailer.