'Phpmailer, errors when try to send [closed]
php is not my cup of tea, but unfortunately I need to create a mail form with attachment to my website.
I fallowed this tutorial https://www.youtube.com/watch?v=ydQT6Bt4zIk&t=33s, but got some errors. It's looks like that my input values are not decelerated. I used ' $_POST['name of html input '];' to get it.
project: http://mork.webd.pl/uploads/mail-form/mail-form.7z
list of errors:
Warning: Undefined array key "email" in C:\xampp2\htdocs\vomo\send.php on line 15
Warning: Undefined array key "email" in C:\xampp2\htdocs\vomo\send.php on line 57
PHPMailer\PHPMailer\Exception: Message body empty in C:\xampp2\htdocs\vomo\vendor\phpmailer\phpmailer\src\PHPMailer.php:1580 Stack trace: #0 C:\xampp2\htdocs\vomo\vendor\phpmailer\phpmailer\src\PHPMailer.php(1488): PHPMailer\PHPMailer\PHPMailer->preSend() #1 C:\xampp2\htdocs\vomo\send.php(58): PHPMailer\PHPMailer\PHPMailer->send() #2 {main}
my php code:
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;
require 'vendor/autoload.php';
$mail = new PHPMailer(true);
$name = $_POST['name-of'];
$email = $_POST['email'];
$subject = $_POST['phone'];
$message = $_POST['mess-content'];
try {
$mail->SMTPDebug = 10;
$mail->isSMTP();
$mail->Mailer="smtp";
$mail->Host=gethostbyname('smtp.gmail.com');
$mail->SMTPAuth = true;
$mail->Username ='[email protected]';
$mail->Password="xxxx";
$mail->SMTPSecure = 'tls';
$mail->Port=465;
$mail->setFrom("[email protected]", $_POST['name-of']);
$mail->addAddress("[email protected]");
//attachment
if(array_key_exists('attachment',$_FILES)) {
$img_name = $_FILES['attachment']['name'];
$upload = tempnam(sys_get_temp_dir(), hash('sha256' , $_FILES['attachment']['name'] ));
$uploadFile = $_SERVER['DOCUMENT_ROOT'].'/Images/'.$img_name;
if(move_uploaded_file( $_FILES['attachment']['tmp-name'], $uploadFile)){
$mail->addAttachment($uploadFile, "My Attachment");
}
}
$mail -> Subject ='Zapytanie ze strony www';
$mail->Body = "<h3>Name : $name <br>Email : $email <br>Message : $message</h3>";
$mail->send();
header("Location: sent.php");
exit();
}
catch(Exception $e) {
echo $e;
}
my HTML code of form
<label for="fname">Imię i Nazwisko <span class="star">*</span></label>
<input type="text" id="name" name="name" placeholder="Podaj imię i nazwisko..." pattern="^([A-ZŻŹĆĄŚĘŁÓŃ][a-zżźćńółęąś{3,}]+)(\s|-|_)+([A-ZŻŹĆĄŚĘŁÓŃ][a-zżźćńółęąś]+)$" required>
<label for="fname">e-mail <span class="star">*</span></label>
<input type="mail" id="mailadress" name="mail" placeholder="wprowadź adres email..." pattern="^([^\x00-\x20\x22\x28\x29\x2c\x2e\x3a-\x3c\x3e\x40\x5b-\x5d\x7f-\xff]+|\x22([^\x0d\x22\x5c\x80-\xff]|\x5c[\x00-\x7f])*\x22)(\x2e([^\x00-\x20\x22\x28\x29\x2c\x2e\x3a-\x3c\x3e\x40\x5b-\x5d\x7f-\xff]+|\x22([^\x0d\x22\x5c\x80-\xff]|\x5c[\x00-\x7f])*\x22))*\x40([^\x00-\x20\x22\x28\x29\x2c\x2e\x3a-\x3c\x3e\x40\x5b-\x5d\x7f-\xff]+|\x5b([^\x0d\x5b-\x5d\x80-\xff]|\x5c[\x00-\x7f])*\x5d)(\x2e([^\x00-\x20\x22\x28\x29\x2c\x2e\x3a-\x3c\x3e\x40\x5b-\x5d\x7f-\xff]+|\x5b([^\x0d\x5b-\x5d\x80-\xff]|\x5c[\x00-\x7f])*\x5d))*(\.\w{2,})+$" required>
<label for="text">Telefon</label>
<div class="form-info">wprowadź numer telfonu w formie +xx xxx xxx xxx</div>
<input type="text" id="phone-number" name="phone" placeholder="podaj nr telefonu..." pattern="/^[+]?\d{1,3}\s?(-)?[0-9]{3}(\s)?[0-9]{3,4}(\s)?[0-9]{3}(\s)?$/">
<label for="subject">Treść wiadomości:<span class="star">*</span></label>
<div class="form-info">pole musi zawierać minimum 10 znaków</div>
<textarea id="message" name="mess-content" placeholder="Treść wiadomości..." style="height:300px" pattern=".{10,}" required></textarea>
<br>
'<div class="alert alert-success"></div>
<div class="error-message "></div>
<div class="buttons-container">
<input type="submit" id="send" value="Wyślij">
<input id="upload" name="attachment" id="upload-file" type="file" >
</div>
</form>
</div>
</div>
Solution 1:[1]
Like Jeroen said, the name of your input email is not the same in your php code. Change your email input name into this.
<input type="mail" id="mailadress" name="email" placeholder="wprowad? adres email..." pattern="^([^\x00-\x20\x22\x28\x29\x2c\x2e\x3a-\x3c\x3e\x40\x5b-\x5d\x7f-\xff]+|\x22([^\x0d\x22\x5c\x80-\xff]|\x5c[\x00-\x7f])*\x22)(\x2e([^\x00-\x20\x22\x28\x29\x2c\x2e\x3a-\x3c\x3e\x40\x5b-\x5d\x7f-\xff]+|\x22([^\x0d\x22\x5c\x80-\xff]|\x5c[\x00-\x7f])*\x22))*\x40([^\x00-\x20\x22\x28\x29\x2c\x2e\x3a-\x3c\x3e\x40\x5b-\x5d\x7f-\xff]+|\x5b([^\x0d\x5b-\x5d\x80-\xff]|\x5c[\x00-\x7f])*\x5d)(\x2e([^\x00-\x20\x22\x28\x29\x2c\x2e\x3a-\x3c\x3e\x40\x5b-\x5d\x7f-\xff]+|\x5b([^\x0d\x5b-\x5d\x80-\xff]|\x5c[\x00-\x7f])*\x5d))*(\.\w{2,})+$" required>
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Ainz |
